@billjs/event-emitter

中文文档
1.0.3发布于 2018/12/13MITTypeScriptCommonJS零依赖近 30 天下载 5334

A simple and lightweight EventEmitter by TypeScript for Node.js or Browsers.

原始使用文档
文档目录15 节
  1. Installation
  2. Overview
  3. API
  4. EventEmitter
  5. globalEvent
  6. on
  7. once
  8. off
  9. offAll
  10. fire
  11. has
  12. getHandlers
  13. EventHandler
  14. Event
  15. License

event-emitter Travis-ci Status GitHub license typescript | javascript | node.js Npm Version

A simple and lightweight EventEmitter by TypeScript for Node.js or Browsers.

Installation

Installation is done using the npm install command:

npm install -S @billjs/event-emitter

Overview

API

EventEmitter

It's a class for managing events. It can be extended to provide event functionality for other classes or object.

  • class - ES6 Class

There have two ways to use it.

One way:

const emitter = new EventEmitter();
emitter.on('event', evt => {
  console.log(evt);
});

Another way:

class Store extends EventEmitter {
  set(key: string, value: any) {
    this.fire(`change:${key}`, value);
  }
}

globalEvent

A instance of EventEmitter for global, you can use it directly as an event emitter at global scope.

// the code maybe in `a.ts`
globalEvent.on('event', evt => {
  console.log(evt);
});
// the code maybe in `b.ts`
globalEvent.fire('event', 'test data');

on

Listen on a new event by type and handler. If listen on, the true is returned, otherwise the false. The handler will not be listen if it is a duplicate.

  • type (string) - event type, it must be a unique string.
  • handler (EventHandler) - event handler, when if the same handler is passed, listen it by only once.
  • return (boolean) If listen on, the true is returned, otherwise the false.
const emitter = new EventEmitter();
emitter.on('event', evt => {
  console.log(evt);
});

once

Listen on an once event by type and handler. When the event is fired, that will be listen off immediately and automatically. The handler will not be listen if it is a duplicate.

  • type (string) - event type, it must be a unique string.
  • handler (EventHandler) - event handler, when if the same handler is passed, listened by only once.
  • return (boolean) If listened on, the true is returned, otherwise the false.
const emitter = new EventEmitter();
emitter.once('event', evt => {
  console.log(evt);
});

off

Listen off an event by type and handler. Or listen off events by type, when if only type argument is passed. Or listen off all events, when if no arguments are passed.

  • [type] (string optional) event type
  • [handler] (EventHandler optional) event handler

Listen off the specified event.

const emitter = new EventEmitter();
emitter.off('event', evt => undefined);

Listen off events by type.

const emitter = new EventEmitter();
emitter.off('event');

Listen off all events, it does samething as offAll method.

const emitter = new EventEmitter();
emitter.off();

offAll

Listen off all events, that means every event will be emptied.

const emitter = new EventEmitter();
emitter.offAll();

fire

Fire the specified event, and you can to pass a data. When fired, every handler attached to that event will be executed. But, if it's an once event, listen off it immediately after called handler.

  • type (string) - event type
  • [data] (any) - event data
const emitter = new EventEmitter();
emitter.fire('event', 'test data');

has

Check whether the specified event has been listen on. Or check whether the events by type has been listen on, when if only type argument is passed.

  • type (string) - event type
  • [handler] (EventHandler optional) - event handler
  • return (boolean) If the event is listen on, the true is returned, otherwise the false.
const emitter = new EventEmitter();
if (emitter.has('event')) {
  console.log('has `event`');
}

getHandlers

Get the handlers for the specified event type.

const emitter = new EventEmitter();
const handlers = emitter.getHandlers('event');
console.log(handlers);

EventHandler

The event handler, that is a normal function.

  • evt (Event) - event object, that will be pass to event handler as it argument.

Event

Event object.

  • type (string) - event type
  • data (any) - event data
  • timestamp (number) - the timestamp when event fired
  • once (boolean) - it is an once event, that meaning listen off after event fired

License

MIT License

版本与发布记录

@billjs/event-emitter 共发布 4 个版本,首次发布于 2018/12/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2018/12/137 年前
1.0.22018/12/117 年前
1.0.12018/12/117 年前
1.0.02018/12/117 年前

关于 @billjs/event-emitter 的常见问题

@billjs/event-emitter 是什么?

A simple and lightweight EventEmitter by TypeScript for Node.js or Browsers.

如何安装 @billjs/event-emitter?

在项目目录运行 npm install @billjs/event-emitter,或使用 pnpm add / yarn add / bun add。

@billjs/event-emitter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@billjs/event-emitter 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@billjs/event-emitter 可以商用吗?

@billjs/event-emitter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@billjs/event-emitter 的下载量有多少?

根据 npm 官方下载统计,@billjs/event-emitter 在 2026/10/08 – 2026/11/06 这 30 天内被下载 5,334 次,日均约 178 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@billjs/event-emitter 还在维护吗?

最新版本 1.0.3 发布于 2018/12/13(7 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/12/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。