EventEmitter

源码

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/event-emitter

说明

模拟实现EventEmitter, 源码地址:

https://github.com/ddzy/ts-utility-plugins/tree/master/src/ddzy/utility/algorithm/event-emitter

用法

index.ts
const event = new utilityAlgorithm.EventEmitter({});
// handleOn
const r1 = event.handleOn('click', function() {
console.log('123');
});
const r2 = event.handleOn('click', function() {
console.log('456');
});
// handleEmit
event.emit('click');
// handleRemove
event.handleRemove(r1).handleRemove(r2);
// getEvents
const events = event.events;

API

Name

Value

Description

handleOn

(type: string, handler: Function) => { type: string, index: number }

放置事件, 返回当前handler 的信息, 可用于remove

handleRemove

(options: { type: string, index: number }) => EventEmitter

移除事件

handleEmit

(type: string) => EventEmitter

发射事件

events

*

返回当前事件列表