1 |
- class Event{on(t,s,e){"function"==typeof s?(this._stores=this._stores||{},(this._stores[t]=this._stores[t]||[]).push({cb:s,ctx:e})):console.error("listener must be a function")}emit(t){this._stores=this._stores||{};let s,e=this._stores[t];if(e){e=e.slice(0),s=[].slice.call(arguments,1),s[0]={eventCode:t,data:s[0]};for(let t=0,o=e.length;t<o;t++)e[t].cb.apply(e[t].ctx,s)}}off(t,s){if(this._stores=this._stores||{},!arguments.length)return void(this._stores={});const e=this._stores[t];if(!e)return;if(1===arguments.length)return void delete this._stores[t];let o;for(let t=0,i=e.length;t<i;t++)if(o=e[t].cb,o===s){e.splice(t,1);break}}}module.exports=Event;
|