Skip to content

Commit

Permalink
Merge pull request #120 from AliMD/feat/signal
Browse files Browse the repository at this point in the history
Enhance signal: add seperate api interface for listeners (ListenerInterface)
  • Loading branch information
alimd authored Mar 22, 2022
2 parents f151464 + 55c0c86 commit 1c95bb3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
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, {...});

0 comments on commit 1c95bb3

Please sign in to comment.