@stoplight/lifecycle
Advanced tools
Comparing version 2.3.1 to 2.3.2
@@ -18,3 +18,4 @@ import { IDisposable } from '../disposable'; | ||
export declare class EventEmitter<E extends EventMap> implements IEventEmitter<E> { | ||
private _emitter; | ||
private readonly _emitter; | ||
private readonly _registeredListeners; | ||
on<P extends keyof E>(type: P, listener: E[P]): IDisposable; | ||
@@ -21,0 +22,0 @@ emit<P extends keyof E>(type: P, ...args: Parameters<E[P]>): void; |
@@ -8,4 +8,13 @@ "use strict"; | ||
this._emitter = new Emitter(); | ||
this._registeredListeners = new Map(); | ||
} | ||
on(type, listener) { | ||
let registeredListeners = this._registeredListeners.get(type); | ||
if (registeredListeners === void 0) { | ||
registeredListeners = new WeakSet(); | ||
this._registeredListeners.set(type, registeredListeners); | ||
} | ||
else if (registeredListeners.has(listener)) { | ||
throw new Error(`Double-registered for '${type}' event.`); | ||
} | ||
const wrappedListener = (...args) => { | ||
@@ -19,4 +28,7 @@ try { | ||
}; | ||
registeredListeners.add(listener); | ||
this._emitter.on(String(type), wrappedListener); | ||
return disposable_1.createDisposable(() => { | ||
var _a; | ||
(_a = registeredListeners) === null || _a === void 0 ? void 0 : _a.delete(listener); | ||
this._emitter.off(String(type), wrappedListener); | ||
@@ -41,2 +53,3 @@ }); | ||
dispose() { | ||
this._registeredListeners.clear(); | ||
this._emitter.removeAllListeners(); | ||
@@ -43,0 +56,0 @@ } |
{ | ||
"name": "@stoplight/lifecycle", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"description": "Event and disposable helpers.", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37818
428