@reverse/emitter
Advanced tools
+26
-25
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const classify_1 = require("@reverse/utils/classify"); | ||
| exports.Emitter = classify_1.classify(function Emitter() { | ||
| const listenerMap = new Map(); | ||
| this.addListener = function (event, callback) { | ||
| if (!listenerMap.has(event)) { | ||
| listenerMap.set(event, new Set()); | ||
| class Emitter { | ||
| constructor() { | ||
| this.listenerMap = new Map(); | ||
| } | ||
| addListener(event, callback) { return this.on(event, callback); } | ||
| removeListener(event, callback) { return this.off(event, callback); } | ||
| on(event, callback) { | ||
| const listeners = this.listenerMap.get(event); | ||
| if (listeners) { | ||
| listeners.add(callback); | ||
| } | ||
| listenerMap.get(event).add(callback); | ||
| }; | ||
| this.removeListener = function (event, callback) { | ||
| if (!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| off(event, callback) { | ||
| const listeners = this.listenerMap.get(event); | ||
| if (listeners) { | ||
| listeners.delete(callback); | ||
| } | ||
| listenerMap.get(event).delete(callback); | ||
| }; | ||
| this.removeAllListeners = function (event) { | ||
| if (!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| removeAllListener(event) { | ||
| this.listenerMap.delete(event); | ||
| } | ||
| emit(event, ...args) { | ||
| const listeners = this.listenerMap.get(event); | ||
| if (listeners) { | ||
| listeners.forEach((callback) => callback(...args)); | ||
| } | ||
| listenerMap.get(event).clear(); | ||
| }; | ||
| this.emit = function (event, ...args) { | ||
| if (!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| listenerMap.get(event).forEach((callback) => callback(...args)); | ||
| }; | ||
| }); | ||
| } | ||
| } | ||
| exports.Emitter = Emitter; | ||
| //# sourceMappingURL=Emitter.js.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"Emitter.js","sourceRoot":"","sources":["Emitter.ts"],"names":[],"mappings":";;AAAA,sDAAmD;AAUtC,QAAA,OAAO,GAAG,mBAAQ,CAAsB,SAAS,OAAO;IACnE,MAAM,WA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C,WAAW,CAAC,KAAa,EAAE,QAAkB,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IACnF,cAAc,CAAC,KAAa,EAAE,QAAkB,IAAI,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAE,CAAC;IAExF,EAAE,CAAC,KAAa,EAAE,QAAkB;QAClC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,SAAS,EAAE;YACb,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SACzB;IACH,CAAC;IACD,GAAG,CAAC,KAAa,EAAE,QAAkB;QACnC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,SAAS,EAAE;YACb,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;SAC5B;IACH,CAAC;IAED,iBAAiB,CAAC,KAAa;QAC7B,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IACD,IAAI,CAAC,KAAa,EAAE,GAAG,IAAW;QAChC,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,SAAS,EAAE;YACb,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;SACpD;IACH,CAAC;CACF;AA5BD,0BA4BC"} |
+3
-3
| { | ||
| "name": "@reverse/emitter", | ||
| "version": "1.0.10", | ||
| "version": "2.0.0", | ||
| "description": "Event Emitters", | ||
| "dependencies": { | ||
| "@reverse/utils": "^1.3.2" | ||
| "@reverse/utils": "^1.3.3" | ||
| }, | ||
@@ -11,3 +11,3 @@ "publishConfig": { | ||
| }, | ||
| "gitHead": "0e8cbd6557245075943f0f0919c71e5f783559c5" | ||
| "gitHead": "2d36d5593906776c3ef8b76272ef8abc323033d7" | ||
| } |
+20
-16
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const classify_1 = require("@reverse/utils/classify"); | ||
| exports.SimpleEmitter = classify_1.classify(function SimpleEmitter() { | ||
| const listeners = new Set(); | ||
| this.addListener = function (callback) { | ||
| listeners.add(callback); | ||
| }; | ||
| this.removeListener = function (callback) { | ||
| listeners.delete(callback); | ||
| }; | ||
| this.removeAllListeners = function () { | ||
| listeners.clear(); | ||
| }; | ||
| this.emit = function (...args) { | ||
| listeners.forEach((callback) => callback(...args)); | ||
| }; | ||
| }); | ||
| class SimpleEmitter { | ||
| constructor() { | ||
| this.listeners = new Set(); | ||
| } | ||
| addListener(callback) { return this.on(callback); } | ||
| removeListener(callback) { return this.off(callback); } | ||
| on(callback) { | ||
| this.listeners.add(callback); | ||
| } | ||
| off(callback) { | ||
| this.listeners.delete(callback); | ||
| } | ||
| removeAllListener() { | ||
| this.listeners.clear(); | ||
| } | ||
| emit(...args) { | ||
| this.listeners.forEach((callback) => callback(...args)); | ||
| } | ||
| } | ||
| exports.SimpleEmitter = SimpleEmitter; | ||
| //# sourceMappingURL=SimpleEmitter.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"SimpleEmitter.js","sourceRoot":"","sources":["SimpleEmitter.ts"],"names":[],"mappings":";;AAAA,sDAAmD;AAUtC,QAAA,aAAa,GAAG,mBAAQ,CAA4B,SAAS,aAAa;IAErF,MAAM,SAAS,GAAG,IAAI,GAAG,EAAY,CAAC;IAEtC,IAAI,CAAC,WAAW,GAAG,UAAU,QAAkB;QAC7C,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC,CAAC;IACF,IAAI,CAAC,cAAc,GAAG,UAAU,QAAkB;QAChD,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7B,CAAC,CAAC;IACF,IAAI,CAAC,kBAAkB,GAAG;QACxB,SAAS,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC,CAAC;IACF,IAAI,CAAC,IAAI,GAAG,UAAS,GAAG,IAAU;QAChC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACrD,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"} | ||
| {"version":3,"file":"SimpleEmitter.js","sourceRoot":"","sources":["SimpleEmitter.ts"],"names":[],"mappings":";;AAAA,MAAa,aAAa;IAA1B;QACU,cAAS,GAAG,IAAI,GAAG,EAA2B,CAAC;IAkBzD,CAAC;IAhBC,WAAW,CAAC,QAAiC,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5E,cAAc,CAAC,QAAiC,IAAI,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEhF,EAAE,CAAC,QAAiC;QAClC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IACD,GAAG,CAAC,QAAiC;QACnC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAClC,CAAC;IAED,iBAAiB;QACf,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;IACD,IAAI,CAAC,GAAG,IAAU;QAChB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IAC1D,CAAC;CACF;AAnBD,sCAmBC"} |
-10
| export interface IEmitter<Args extends any[]> { | ||
| addListener(callback: (event: string, ...args: Args) => void): void; | ||
| removeListener(callback: (event: string, ...args: Args) => void): void; | ||
| removeAllListeners(event: string): void; | ||
| emit(event: string, ...args: Args): void; | ||
| } | ||
| export declare const Emitter: { | ||
| (): IEmitter<any[]>; | ||
| new (): IEmitter<any[]>; | ||
| }; |
-38
| import { classify } from '@reverse/utils/classify'; | ||
| export interface IEmitter<Args extends any[]> { | ||
| addListener(callback: (event: string, ...args: Args) => void): void; | ||
| removeListener(callback: (event: string, ...args: Args) => void): void; | ||
| removeAllListeners(event: string): void; | ||
| emit(event: string, ...args: Args): void; | ||
| } | ||
| // A single channel EventEmitter. | ||
| export const Emitter = classify<[], IEmitter<any[]>>(function Emitter() { | ||
| const listenerMap = new Map<string, Set<Function>>(); | ||
| this.addListener = function(event: string, callback: Function) { | ||
| if(!listenerMap.has(event)) { | ||
| listenerMap.set(event, new Set()); | ||
| } | ||
| listenerMap.get(event).add(callback); | ||
| }; | ||
| this.removeListener = function(event: string, callback: Function) { | ||
| if(!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| listenerMap.get(event).delete(callback); | ||
| }; | ||
| this.removeAllListeners = function(event: string) { | ||
| if(!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| listenerMap.get(event).clear(); | ||
| }; | ||
| this.emit = function(event: string, ...args: any[]) { | ||
| if (!listenerMap.has(event)) { | ||
| return; | ||
| } | ||
| listenerMap.get(event).forEach((callback) => callback(...args)); | ||
| }; | ||
| }); |
| export * from './Emitter'; | ||
| export * from './SimpleEmitter'; |
-2
| export * from './Emitter'; | ||
| export * from './SimpleEmitter'; |
| export interface ISimpleEmitter<Args extends any[]> { | ||
| addListener(callback: (...args: Args) => void): void; | ||
| removeListener(callback: (...args: Args) => void): void; | ||
| removeAllListeners(): void; | ||
| emit(...args: Args): void; | ||
| } | ||
| export declare const SimpleEmitter: { | ||
| (): ISimpleEmitter<any[]>; | ||
| new (): ISimpleEmitter<any[]>; | ||
| }; |
| import { classify } from '@reverse/utils/classify'; | ||
| export interface ISimpleEmitter<Args extends any[]> { | ||
| addListener(callback: (...args: Args) => void): void; | ||
| removeListener(callback: (...args: Args) => void): void; | ||
| removeAllListeners(): void; | ||
| emit(...args: Args): void; | ||
| } | ||
| // A single channel EventEmitter. | ||
| export const SimpleEmitter = classify<[], ISimpleEmitter<any[]>>(function SimpleEmitter<Args extends any[]>() { | ||
| type Callback = (...args: Args) => void; | ||
| const listeners = new Set<Callback>(); | ||
| this.addListener = function (callback: Callback) { | ||
| listeners.add(callback); | ||
| }; | ||
| this.removeListener = function (callback: Callback) { | ||
| listeners.delete(callback); | ||
| }; | ||
| this.removeAllListeners = function () { | ||
| listeners.clear(); | ||
| }; | ||
| this.emit = function(...args: Args) { | ||
| listeners.forEach((callback) => callback(...args)); | ||
| }; | ||
| }); |
5365
-35.79%9
-40%60
-56.52%Updated