@d-fischer/typed-event-emitter

中文文档
3.3.3发布于 2023/12/10MITTypeScriptESM + CJS近 30 天下载 85.3万

Alternative event emitter for JavaScript and TypeScript.

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Internal events
  4. JavaScript

@d-fischer/typed-event-emitter

This module provides an alternative API to the well known event emitting interfaces used in the browser (DOM) or node.js. Instead of accepting arbitrary strings as the event name, this module forces you to register your events in your class. Consequently, the style of binding and emitting events differs a little, ensuring already at binding time that the events actually exist.

Install

yarn add @d-fischer/typed-event-emitter
# or
npm install @d-fischer/typed-event-emitter

Usage

import { EventEmitter } from '@d-fischer/typed-event-emitter';

class MyClass extends EventEmitter {
    // register an event
    onValueChanged = this.registerEvent<[newValue: number]>();

    private _value: number;

    constructor(value: number) {
        // initialize EventEmitter
        super();

        this._value = value;
    }

    get value() {
        return this._value;
    }

    set value(value: number) {
        this._value = value;
        // emit the event to all listeners
        this.emit(this.onValueChanged, this._value);
    }
}

const instance = new MyClass();

// listen to event
instance.onValueChanged(newValue => {
    console.log('Value changed', newValue);
});

// invoke setter
instance.value = 27;

First, the EventEmitter is loaded from the module. Any class that shall emit events, must extend that EventEmitter. If your class has its own constructor, make sure to call super().

Any events your class shall be able to emit must be registered in the form:

onFooBar = this.registerEvent<[...argTypes]>();

Where onFooBar can be any name (it doesn't need to begin with on) and argTypes must be a tuple representing the arguments of the function the listeners must have. With this, you can see the signature your function must have when you're about to bind a listener to that event.

To emit an event (only possible from within your event emitter), you have to call this.emit(this.onFooBar, ...), where this.onFooBar is the event to emit and ... any number of parameters, that will be passed to the listeners.

Internal events

You can also declare internal events as well as internal listeners. The main advantage over just using regular listeners appears when you write a library for other people to use. If an end user decides to clear all events using instance.removeListener() without any parameter or with only the event binder to clear out a specific event, internal listeners will survive that.

import { EventEmitter } from '@d-fischer/typed-event-emitter';

class MyClass extends EventEmitter {
    // register an event
    onValueChanged = this.registerEvent<[newValue: number]>();

    private _value: number;

    constructor(value: number) {
        // initialize EventEmitter
        super();

        this._value = value;
        
        this.addInternalListener(this.onValueChanged, newValue => {
            console.log('Value changed (internal)', newValue);
        });
    }

    get value() {
        return this._value;
    }

    set value(value: number) {
        this._value = value;
        // emit the event to all listeners
        this.emit(this.onValueChanged, this._value);
    }
}

const instance = new MyClass();

// listen to event
instance.onValueChanged(newValue => {
    console.log('Value changed (external)', newValue);
});

// invoke setter, shows internal & external logs
instance.value = 27;

// clear listeners for event
instance.removeListener(instance.onValueChanged);

// invoke setter, still shows internal log
instance.value = 42;

JavaScript

Your JavaScript host (i.e., your browser, node.js, etc.) should support classes and inheritance in order to work correctly. The code shown above can also be written in JavaScript (node.js):

const { EventEmitter } = require('@d-fischer/typed-event-emitter');

class MyClass extends EventEmitter {
    constructor(value) {
        // initialize EventEmitter
        super();

        /* newValue: number */
        this.onValueChanged = this.registerEvent();

        this._value = value;
    }

    get value() {
        return this._value;
    }

    set value(value) {
        this._value = value;
        this.emit(this.onValueChanged, this._value);
    }
}

let instance = new MyClass();
instance.onValueChanged(newValue => {
    console.log(`Value changed: ${newValue}`);
});

instance.value = 27;

Node that the events are registered explicitly within the constructor. Make sure to initialize them after calling super().

版本与发布记录

@d-fischer/typed-event-emitter 共发布 15 个版本,首次发布于 2019/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.3.3latest2023/12/102 年前
3.3.22023/05/223 年前
3.3.12023/04/103 年前
3.3.02022/09/274 年前
4.0.02022/09/274 年前
3.2.32021/08/305 年前

关于 @d-fischer/typed-event-emitter 的常见问题

@d-fischer/typed-event-emitter 是什么?

Alternative event emitter for JavaScript and TypeScript.

如何安装 @d-fischer/typed-event-emitter?

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

@d-fischer/typed-event-emitter 支持 TypeScript 吗?

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

@d-fischer/typed-event-emitter 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@d-fischer/typed-event-emitter 可以商用吗?

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

@d-fischer/typed-event-emitter 的下载量有多少?

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

@d-fischer/typed-event-emitter 还在维护吗?

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