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

Refactor signal #217

Merged
merged 2 commits into from
Aug 21, 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
52 changes: 26 additions & 26 deletions packages/core/signal/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ alwatrRegisteredList.push({
version: '{{ALWATR_VERSION}}',
});

/**
* Listener `id`
*/
let _lastListenerId = 0;

/**
* Signal stack database.
*/
Expand Down Expand Up @@ -69,7 +74,7 @@ function __callListeners<SignalName extends keyof AlwatrSignals>(signal: SignalO

signal.listenerList
.filter((listener) => !listener.disabled && listener.once)
.forEach((listener) => _removeSignalListener(signal.name, listener.id));
.forEach((listener) => _removeSignalListener(signal, listener.id));
}

/**
Expand All @@ -82,15 +87,14 @@ function __callListeners<SignalName extends keyof AlwatrSignals>(signal: SignalO
* ```
*/
export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
signalName: SignalName,
signal: SignalObject<SignalName>,
signalCallback: ListenerCallback<SignalName>,
options?: ListenerOptions,
): ListenerObject<SignalName> {
logger.logMethodArgs('addSignalListener', {signalName, options});
logger.logMethodArgs('addSignalListener', {signal, options});

const signal = __getSignalObject(signalName);
const listener: ListenerObject<SignalName> = {
id: Symbol('Alwatr signal listener for ' + signalName),
id: ++_lastListenerId,
once: options?.once ?? false,
disabled: options?.disabled ?? false,
callback: signalCallback,
Expand All @@ -102,20 +106,22 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
if (signal.value !== undefined) {
if (options?.receivePrevious === 'Immediate') {
logger.incident('addSignalListener', 'call_signal_callback', 'run callback with previous signal value!', {
signalName,
signalName: signal.name,
mode: 'Immediate',
});
try {
signalCallback(signal.value);
} catch (err) {
logger.error('addSignalListener', 'call_signal_callback_failed', (err as Error).stack || err, {signalName});
logger.error('addSignalListener', 'call_signal_callback_failed', (err as Error).stack || err, {
signalName: signal.name,
});
}
callbackCalled = true;
} else if (options?.receivePrevious === true) {
requestAnimationFrame(() => {
if (signal.value !== undefined) {
logger.incident('addSignalListener', 'call_signal_callback', 'run callback with previous signal value!', {
signalName,
signalName: signal.name,
mode: 'Delay',
});
signalCallback(signal.value);
Expand Down Expand Up @@ -148,11 +154,10 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
* ```
*/
export function _removeSignalListener<SignalName extends keyof AlwatrSignals>(
signalName: SignalName,
listenerId: symbol,
signal: SignalObject<SignalName>,
listenerId: number,
): void {
logger.logMethodArgs('_removeSignalListener', signalName);
const signal = __getSignalObject(signalName);
logger.logMethodArgs('_removeSignalListener', {signal, listenerId});
const listenerIndex = signal.listenerList.findIndex((_listener) => _listener.id === listenerId);
if (listenerIndex !== -1) {
signal.listenerList.splice(listenerIndex, 1);
Expand All @@ -166,13 +171,12 @@ export function _removeSignalListener<SignalName extends keyof AlwatrSignals>(
* dispatchSignal('content-change', content);
*/
export function _dispatchSignal<SignalName extends keyof AlwatrSignals>(
signalName: SignalName,
signal: SignalObject<SignalName>,
value: AlwatrSignals[SignalName],
options?: DispatchOptions,
): void {
logger.logMethodArgs('dispatchSignal', {signalName, value, options});
logger.logMethodArgs('dispatchSignal', {signal, value, options});

const signal = __getSignalObject(signalName);
// set value before check signal.debounced for act like throttle (call listeners with last dispatch value).
signal.value = value;

Expand Down Expand Up @@ -210,17 +214,15 @@ export function _dispatchSignal<SignalName extends keyof AlwatrSignals>(
* ```
*/
export function _setSignalProvider<SignalName extends keyof AlwatrRequestSignals>(
signalName: SignalName,
signal: SignalObject<SignalName>,
signalProvider: SignalProvider<SignalName>,
options?: SignalProviderOptions,
): ListenerObject<SignalName> {
logger.logMethodArgs('setSignalProvider', {signalName, options});
logger.logMethodArgs('setSignalProvider', {signal, options});

// @TODO: refactor with removeSignalProvider
const signal = __getSignalObject(`request-${signalName}` as unknown as SignalName);
if (signal.listenerList.length > 0) {
logger.accident('setSignalProvider', 'signal_provider_already_set', 'another provider defined and will removed', {
signalName,
signalName: signal.name,
});
signal.listenerList = [];
}
Expand All @@ -229,13 +231,11 @@ export function _setSignalProvider<SignalName extends keyof AlwatrRequestSignals
const signalValue = await signalProvider(requestParam);
if (signalValue !== undefined) {
// null can be a valid value.
_dispatchSignal(signalName, signalValue, {debounce: options?.debounce ?? true});
_dispatchSignal(signal, signalValue, {debounce: options?.debounce ?? true});
}
};

return _addSignalListener(
`request-${signalName}` as unknown as SignalName,
_callback as unknown as ListenerCallback<SignalName>,
{receivePrevious: options?.receivePrevious ?? true},
);
return _addSignalListener(signal, _callback as unknown as ListenerCallback<SignalName>, {
mohammadhonarvar marked this conversation as resolved.
Show resolved Hide resolved
receivePrevious: options?.receivePrevious ?? true,
});
}
17 changes: 8 additions & 9 deletions packages/core/signal/src/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import type {
*/
export class SignalInterface<SignalName extends keyof AlwatrSignals> {
protected _signal;
protected _requestSignal;
protected _logger;

constructor(signalName: SignalName) {
this._logger = createLogger(`signal<${signalName}>`);
this._signal = __getSignalObject(signalName);
this._requestSignal = __getSignalObject(`request-${signalName}` as unknown as SignalName);
}

/**
Expand Down Expand Up @@ -131,13 +133,11 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
* });
* ```
*/
setProvider(signalProvider: SignalProvider<SignalName>, options?: SignalProviderOptions): symbol {
setProvider(signalProvider: SignalProvider<SignalName>, options?: SignalProviderOptions): ListenerObject<SignalName> {
this._logger.logMethodArgs('setProvider', {options});
return _setSignalProvider(this.name, signalProvider, options).id;
return _setSignalProvider(this._signal, signalProvider, options);
}

// @TODO: removeProvider(signalName): void

/**
* Dispatch request signal and wait for answer (wait for new signal dispatched).
*
Expand All @@ -154,8 +154,7 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
request(requestParam: AlwatrRequestSignals[SignalName]): Promise<AlwatrSignals[SignalName]> {
this._logger.logMethodArgs('request', {requestParam});
const nextSignalValuePromise = this.getNextSignalValue();
_dispatchSignal(
`request-${this.name}` as unknown as SignalName,
_dispatchSignal(this._requestSignal,
requestParam as unknown as AlwatrSignals[SignalName], // mastmalize to avoid type error
);
return nextSignalValuePromise;
Expand Down Expand Up @@ -217,7 +216,7 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
*/
dispatch(signalValue: AlwatrSignals[SignalName], options?: DispatchOptions): void {
this._logger.logMethodArgs('dispatch', {signalValue, options});
_dispatchSignal(this._signal.name, signalValue, options);
_dispatchSignal(this._signal, signalValue, options);
}

/**
Expand All @@ -232,7 +231,7 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
*/
addListener(listener: ListenerCallback<SignalName>, options?: ListenerOptions): ListenerInterface<SignalName> {
this._logger.logMethodArgs('addListener', {listener, options});
const listenerId = _addSignalListener(this._signal.name, listener, options);
const listenerId = _addSignalListener(this._signal, listener, options);
return new ListenerInterface(this._signal, listenerId);
}
}
Expand Down Expand Up @@ -279,6 +278,6 @@ export class ListenerInterface<SignalName extends keyof AlwatrSignals> {
*/
remove(): void {
this._logger.logMethod('remove');
_removeSignalListener(this._signal.name, this._listener.id);
_removeSignalListener(this._signal, this._listener.id);
}
}
4 changes: 2 additions & 2 deletions packages/core/signal/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ export type SignalProvider<SignalName extends keyof AlwatrRequestSignals> = (
*/
export interface ListenerObject<SignalName extends keyof AlwatrSignals> {
/**
* Listener symbol id (unique).
* Unique listener id
*/
id: symbol;
id: number;

/**
* If true, the listener will be called only once and removed automatically after first call
Expand Down