signal-controller
Advanced tools
+7
-2
| declare const ONCE_SYMBOL: unique symbol; | ||
| declare const EMIT: unique symbol; | ||
| declare const CLEAR: unique symbol; | ||
| declare const CLEAR_SIGNAL: unique symbol; | ||
| declare const CLEAR_ALL_SIGNALS: unique symbol; | ||
| interface ListenerFunction { | ||
@@ -25,3 +26,4 @@ (...args: any[]): unknown; | ||
| off(signalName: keyof T, listener: ListenerFunction): void; | ||
| [CLEAR](signalName?: keyof T): void; | ||
| [CLEAR_SIGNAL](signalName: keyof T): void; | ||
| [CLEAR_ALL_SIGNALS](): void; | ||
| } | ||
@@ -34,4 +36,7 @@ export declare class SignalController<T extends BaseEventEmitterAPI> { | ||
| clear(): void; | ||
| /** Removes all event listeners, stops accepting new event listeners, and future calls to {@link emit} will only | ||
| * produce a warning message, but otherwise be ignored. */ | ||
| destroy(): void; | ||
| writableStream<K extends keyof T>(signalName: K): WritableStream<Parameters<T[K]>>; | ||
| } | ||
| export {}; |
+25
-11
| const ONCE_SYMBOL = Symbol('once'); | ||
| const EMIT = Symbol('emit'); | ||
| const CLEAR = Symbol('clear'); | ||
| const CLEAR_SIGNAL = Symbol('off'); | ||
| const CLEAR_ALL_SIGNALS = Symbol('clear'); | ||
| export class SignalEmitter { | ||
@@ -80,11 +81,9 @@ #listeners = new Map(); | ||
| } | ||
| [CLEAR](signalName) { | ||
| if (signalName === undefined) { | ||
| Object.keys(this.#listeners).forEach(key => this[CLEAR](key)); | ||
| } | ||
| else { | ||
| this.#listeners.get(signalName) | ||
| ?.forEach(listener => this.off(signalName, listener)); | ||
| } | ||
| [CLEAR_SIGNAL](signalName) { | ||
| this.#listeners.get(signalName) | ||
| ?.forEach(listener => this.off(signalName, listener)); | ||
| } | ||
| [CLEAR_ALL_SIGNALS]() { | ||
| Object.keys(this.#listeners).forEach(key => this[CLEAR_SIGNAL](key)); | ||
| } | ||
| } | ||
@@ -100,7 +99,22 @@ export class SignalController { | ||
| off(signalName) { | ||
| this.signal[CLEAR](signalName); | ||
| this.signal[CLEAR_SIGNAL](signalName); | ||
| } | ||
| clear() { | ||
| this.signal[CLEAR](); | ||
| this.signal[CLEAR_ALL_SIGNALS](); | ||
| } | ||
| /** Removes all event listeners, stops accepting new event listeners, and future calls to {@link emit} will only | ||
| * produce a warning message, but otherwise be ignored. */ | ||
| destroy() { | ||
| this.clear(); | ||
| this.emit = () => { | ||
| const dummy = {}; | ||
| Error.captureStackTrace(dummy, SignalController); | ||
| console.warn("Ignoring event emitted to a SignalController that has been destroyed.", dummy.stack); | ||
| }; | ||
| this.signal.on = () => { | ||
| const dummy = {}; | ||
| Error.captureStackTrace(dummy, SignalController); | ||
| console.warn("Ignoring subscription of new listener to a SignalEmitter whose controller has been destroyed.", dummy.stack); | ||
| }; | ||
| } | ||
| writableStream(signalName) { | ||
@@ -107,0 +121,0 @@ return new WritableStream({ |
+1
-1
| { | ||
| "name": "signal-controller", | ||
| "version": "0.3.0", | ||
| "version": "0.4.0", | ||
| "description": "A lightweight event emitter with separation of concerns between emitter and listener inspired by the AbortController interface. It also supports using AbortController to remove listeners.", | ||
@@ -5,0 +5,0 @@ "author": "Leonardo Raele <leonardoraele@gmail.com>", |
6592
19.2%163
13.19%