Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance signal: add seperate api interface for listeners (ListenerInterface) #120

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions package/signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
6 changes: 3 additions & 3 deletions package/signal/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
signalName: SignalName,
signalCallback: ListenerCallback<SignalName>,
options?: ListenerOptions,
): symbol {
): ListenerObject<SignalName> {
logger.logMethodArgs('addSignalListener', {signalName, options});

const signal = __getSignalObject(signalName);
Expand Down Expand Up @@ -125,7 +125,7 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
}
}

return listener.id;
return listener;
}

/**
Expand Down Expand Up @@ -204,7 +204,7 @@ export function _setSignalProvider<SignalName extends keyof AlwatrRequestSignals
signalName: SignalName,
signalProvider: SignalProvider<SignalName>,
options?: SignalProviderOptions,
): symbol {
): ListenerObject<SignalName> {
logger.logMethodArgs('setSignalProvider', {signalName, options});

// @TODO: refactor with removeSignalProvider
Expand Down
63 changes: 47 additions & 16 deletions package/signal/src/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import type {
ListenerCallback,
SignalProvider,
SignalProviderOptions,
ListenerObject,
SignalObject,
} from './type';

/**
* Signal API interface as a remote controller.
*/
export class SignalInterface<SignalName extends keyof AlwatrSignals> {
private _signal;
private _logger;
protected _signal;
protected _logger;

constructor(signalName: SignalName) {
this._logger = createLogger('Signal:' + signalName);
Expand Down Expand Up @@ -86,6 +88,7 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
return this._signal.disabled;
}
set disabled(disabled: boolean) {
this._logger.logProperty('disabled', disabled);
this._signal.disabled = disabled;
}

Expand Down Expand Up @@ -129,7 +132,7 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
*/
setProvider(signalProvider: SignalProvider<SignalName>, 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
Expand Down Expand Up @@ -226,30 +229,58 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
* const listener = contentChangeSignal.addListener((content) => console.log(content));
* ```
*/
addListener(listener: ListenerCallback<SignalName>, options?: ListenerOptions): symbol {
addListener(listener: ListenerCallback<SignalName>, options?: ListenerOptions): ListenerInterface<SignalName> {
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<SignalName extends keyof AlwatrSignals> {
protected _logger;
constructor(
protected _signal: SignalObject<SignalName>,
protected _listener: ListenerObject<SignalName>,
) {
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, {...});