diff --git a/package/signal/README.md b/package/signal/README.md index bd6d2761f..c62bffdc5 100644 --- a/package/signal/README.md +++ b/package/signal/README.md @@ -200,14 +200,28 @@ Example: const listener = contentChangeSignal.addListener((content) => console.log(content)); ``` -### `removeListener(listener)` +### Listener API Interface + +#### `listener.disabled` + +Disable the listener, not called anymore. + +Example: + +```ts +const listener = contentChangeSignal.addListener((content) => console.log(content)); +... +listener.disabled = true; +``` + +#### `listener.remove()` Remove listener from specific signal. Example: ```ts -const listenerId = contentChangeSignal.addListener((content) => console.log(content)); +const listener = contentChangeSignal.addListener((content) => console.log(content)); ... -contentChangeSignal.removeListener(listenerId); +listener.remove(); ``` diff --git a/package/signal/src/core.ts b/package/signal/src/core.ts index 6a7198570..d3a94b488 100644 --- a/package/signal/src/core.ts +++ b/package/signal/src/core.ts @@ -80,7 +80,7 @@ export function _addSignalListener( signalName: SignalName, signalCallback: ListenerCallback, options?: ListenerOptions, -): symbol { +): ListenerObject { logger.logMethodArgs('addSignalListener', {signalName, options}); const signal = __getSignalObject(signalName); @@ -125,7 +125,7 @@ export function _addSignalListener( } } - return listener.id; + return listener; } /** @@ -204,7 +204,7 @@ export function _setSignalProvider, options?: SignalProviderOptions, -): symbol { +): ListenerObject { logger.logMethodArgs('setSignalProvider', {signalName, options}); // @TODO: refactor with removeSignalProvider diff --git a/package/signal/src/signal.ts b/package/signal/src/signal.ts index b5b45bf3b..cec544b30 100644 --- a/package/signal/src/signal.ts +++ b/package/signal/src/signal.ts @@ -13,14 +13,16 @@ import type { ListenerCallback, SignalProvider, SignalProviderOptions, + ListenerObject, + SignalObject, } from './type'; /** * Signal API interface as a remote controller. */ export class SignalInterface { - private _signal; - private _logger; + protected _signal; + protected _logger; constructor(signalName: SignalName) { this._logger = createLogger('Signal:' + signalName); @@ -86,6 +88,7 @@ export class SignalInterface { return this._signal.disabled; } set disabled(disabled: boolean) { + this._logger.logProperty('disabled', disabled); this._signal.disabled = disabled; } @@ -129,7 +132,7 @@ export class SignalInterface { */ setProvider(signalProvider: SignalProvider, options?: SignalProviderOptions): symbol { this._logger.logMethodArgs('setProvider', {options}); - return _setSignalProvider(this.name, signalProvider, options); + return _setSignalProvider(this.name, signalProvider, options).id; } // @TODO: removeProvider(signalName): void @@ -226,30 +229,58 @@ export class SignalInterface { * const listener = contentChangeSignal.addListener((content) => console.log(content)); * ``` */ - addListener(listener: ListenerCallback, options?: ListenerOptions): symbol { + addListener(listener: ListenerCallback, options?: ListenerOptions): ListenerInterface { this._logger.logMethodArgs('addListener', {listener, options}); - return _addSignalListener(this._signal.name, listener, options); + const listenerId = _addSignalListener(this._signal.name, listener, options); + return new ListenerInterface(this._signal, listenerId); + } +} + +/** + * Signal Listener API interface as a remote controller. + */ +export class ListenerInterface { + protected _logger; + constructor( + protected _signal: SignalObject, + protected _listener: ListenerObject, + ) { + this._logger = createLogger('Listener of ' + _signal.name); } /** - * Remove listener from specific signal. + * Disable the listener, not called anymore. + * + * Example: + * + * ```ts + * const listener = contentChangeSignal.addListener((content) => console.log(content)); + * ... + * listener.disabled = true; + * ``` + */ + get disabled(): boolean { + return this._listener.disabled; + } + set disabled(disabled: boolean) { + this._logger.logProperty('disabled', disabled); + this._listener.disabled = disabled; + } + + /** + * Remove listener from target signal. * * Example: * * ```ts * const contentChangeSignal = new SignalInterface('content-change'); - * const listenerId = contentChangeSignal.addListener((content) => console.log(content)); + * const listener = contentChangeSignal.addListener((content) => console.log(content)); * ... - * contentChangeSignal.removeListener(listenerId); + * listener.remove(); * ``` */ - removeListener(listenerId: symbol): void { - this._logger.logMethod('removeListener'); - _removeSignalListener(this._signal.name, listenerId); + remove(): void { + this._logger.logMethod('remove'); + _removeSignalListener(this._signal.name, this._listener.id); } } - -// @TODO: getSignalOptions(signalName); -// @TODO: getListenerOptions(listenerId); -// @TODO: setSignalOptions(signalName, {...}); -// @TODO: setListenerOptions(listenerId, {...});