tseep号称是”世界上最快的事件发射器“,在“经典API事件发射器”方面,比eventemitter3快12倍。
基于事件映射的 emit 方法的全类型参数完全实现 NodeJS.EventEmitter 类型和标准,提供接口最快的pure-js EventEmitter使用eventemitter3测试进行全面测试基准emit-multiple-listeners:tseepx40,569,711ops/sec<---EventEmitter1x4,498,223ops/secEventEmitter2x4,536,296ops/secEventEmitter3x5,852,395ops/secfastemitterx6,127,215ops/secevent-emitterx3,449,595ops/seccontra/emitterx2,186,002ops/sectseex5,231,167ops/secemitixx6,549,983ops/secFastestis['tseep']基准测试安装和使用npmitseep简单用法:import{EventEmitter}from"tseep";constevents=newEventEmitter<{foo:(a:number,b:string)=>void;}>();//foo'sargumentsisfullytypecheckedevents.emit("foo",123,"helloworld");APIEventEmitter<T> ,其中 T 延伸 {[eventName]:Callsignature} 。
EventEmitter.emit 的args基于事件映射完全类型化。
!! __proto__ 事件名称受限制(类型保护存在)!!
//Listener=(...args:any[])=>Promise<any>|void//EventMapextends{[eventin(string|symbol)]:Listener}classEventEmitter<EventMap>{readonlymaxListeners:number;readonly_eventsCount:number;emit(event:EventKey,...args:ArgsN<EventMap[EventKey]>):boolean;on(event:EventKey,listener:EventMap[EventKey]):this;once(event:EventKey,listener:EventMap[EventKey]):this;addListener(event:EventKey,listener:EventMap[EventKey],argsNum?:ArgsNum<EventMap[EventKey]>):this;removeListener(event:EventKey,listener:EventMap[EventKey]):this;hasListeners(event:EventKey):boolean;prependListener(event:EventKey,listener:EventMap[EventKey]):this;prependOnceListener(event:EventKey,listener:EventMap[EventKey]):this;off(event:EventKey,listener:EventMap[EventKey]):this;removeAllListeners(event?:EventKey):this;setMaxListeners(n:number):this;getMaxListeners():number;listeners(event:EventKey):EventMap[EventKey][];rawListeners(event:EventKey):EventMap[EventKey][];eventNames():Array<string|symbol>;listenerCount(type:EventKey):number;}
评论