3h-event 中文文档教程
3h-event
类型良好的事件发射器。
Links
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.
Links
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) {
// ...
}
更多