3h-event 中文文档教程

发布于 3年前 浏览 29 项目主页 更新于 3年前

3h-event

类型良好的事件发射器。

Example

import { Event, EventEmitter } from '3h-event';

const EVENT_B = Symbol('name of event b');

type EventA = Event<'name_of_event_a', DataTypeOfEventA>;
type EventB = Event<EVENT_B, DataTypeOfEventB>;

interface MyEvents {
    name_of_event_a: EventA;
    [EVENT_B]: EventB;
}

const eventEmitter = new EventEmitter<MyEvent>();

eventEmitter.on('name_of_event_a', (event) => {
    event.stop();
    // ...
});

eventEmitter.on(EVENT_B, (event) => {
    event.cancel();
    // ...
});

const eventA: EventA = new Event({
    name: 'name_of_event_a',
    data: dataA,
    stoppable: true,
});

eventEmitter.emit(eventA);

if (eventA.stopped) {
    // ...
}

const eventB: EventB = new Event({
    name: EVENT_B,
    data: dataB,
    cancelable: true,
});

eventEmitter.emit(eventB);

if (eventB.canceled) {
    // ...
}

3h-event

A well-typed event emitter.

Example

import { Event, EventEmitter } from '3h-event';

const EVENT_B = Symbol('name of event b');

type EventA = Event<'name_of_event_a', DataTypeOfEventA>;
type EventB = Event<EVENT_B, DataTypeOfEventB>;

interface MyEvents {
    name_of_event_a: EventA;
    [EVENT_B]: EventB;
}

const eventEmitter = new EventEmitter<MyEvent>();

eventEmitter.on('name_of_event_a', (event) => {
    event.stop();
    // ...
});

eventEmitter.on(EVENT_B, (event) => {
    event.cancel();
    // ...
});

const eventA: EventA = new Event({
    name: 'name_of_event_a',
    data: dataA,
    stoppable: true,
});

eventEmitter.emit(eventA);

if (eventA.stopped) {
    // ...
}

const eventB: EventB = new Event({
    name: EVENT_B,
    data: dataB,
    cancelable: true,
});

eventEmitter.emit(eventB);

if (eventB.canceled) {
    // ...
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文