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

fix(signal): default options #335

Merged
merged 3 commits into from
Oct 20, 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
6 changes: 5 additions & 1 deletion packages/core/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {createLogger, alwatrRegisteredList} from '@alwatr/logger';

const logger = createLogger('alwatr/fetch');
Expand Down Expand Up @@ -107,7 +106,9 @@ export function fetch(url: string, options: FetchOptions = {}): Promise<Response
return response
.then((response) => {
clearTimeout(timeoutId);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (response.status >= 502 && response.status <= 504 && options.retry! > 1) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
options.retry!--;
options.signal = externalAbortSignal;

Expand All @@ -121,7 +122,9 @@ export function fetch(url: string, options: FetchOptions = {}): Promise<Response
return response;
})
.catch((reason) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (timedOut && options.retry! > 1) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
options.retry!--;
options.signal = externalAbortSignal;

Expand Down Expand Up @@ -169,6 +172,7 @@ export async function getJson<ResponseType extends Record<string | number, unkno
err,
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (options.retry! > 1) {
data = await getJson(url, options);
}
Expand Down
51 changes: 33 additions & 18 deletions packages/core/signal/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ function __callListeners<SignalName extends keyof AlwatrSignals>(signal: SignalO
export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
signal: SignalObject<SignalName>,
listenerCallback: ListenerCallback<SignalName>,
options?: ListenerOptions,
options: ListenerOptions = {},
): ListenerObject<SignalName> {
options.once ??= false;
options.disabled ??= false;
options.receivePrevious ??= true;
options.priority ??= false;

logger.logMethodArgs('_addSignalListener', {signalName: signal.name, options});

const listener: ListenerObject<SignalName> = {
id: ++_lastListenerId,
once: options?.once ?? false,
disabled: options?.disabled ?? false,
once: options.once,
disabled: options.disabled,
callback: listenerCallback,
};

Expand All @@ -115,11 +120,16 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
// Run callback for old dispatch signal
if (signal.value !== undefined) {
// null is a valid value for signal.
if (options?.receivePrevious === 'Immediate') {
logger.incident('_addSignalListener', 'call_signal_callback', 'run callback with previous signal value!', {
signalName: signal.name,
mode: 'Immediate',
});
if (options.receivePrevious === 'Immediate') {
logger.incident(
'_addSignalListener',
'call_signal_callback',
'run callback with previous signal value',
{
signalName: signal.name,
mode: 'Immediate',
},
);
try {
listenerCallback(signal.value);
}
Expand All @@ -130,11 +140,11 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
}
callbackCalled = true;
}
else if (options?.receivePrevious === true) {
else if (options.receivePrevious === true) {
requestAnimationFrame(() => {
if (signal.value !== undefined) {
// null is a valid value for signal.
logger.incident('_addSignalListener', 'call_signal_callback', 'run callback with previous signal value!', {
logger.incident('_addSignalListener', 'call_signal_callback', 'run callback with previous signal value', {
signalName: signal.name,
mode: 'Delay',
});
Expand All @@ -146,8 +156,8 @@ export function _addSignalListener<SignalName extends keyof AlwatrSignals>(
}

// if once then must remove listener after fist callback called! then why push it to listenerList?!
if (!(options?.once && callbackCalled)) {
if (options?.priority) {
if (!(options.once === true && callbackCalled === true)) {
if (options.priority === true) {
signal.listenerList.unshift(listener);
}
else {
Expand Down Expand Up @@ -190,17 +200,19 @@ export function _removeSignalListener<SignalName extends keyof AlwatrSignals>(
export function _dispatchSignal<SignalName extends keyof AlwatrSignals>(
signal: SignalObject<SignalName>,
value: AlwatrSignals[SignalName],
options?: DispatchOptions,
options: DispatchOptions = {},
): void {
options.debounce ??= true;

logger.logMethodArgs('dispatchSignal', {signalName: signal.name, value, options});

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

if (signal.disabled) return; // signal is disabled.
if (options?.debounce && signal.debounced) return; // last dispatch in progress.
if (options.debounce === true && signal.debounced === true) return; // last dispatch in progress.

if (!options?.debounce) {
if (options.debounce !== true) {
// call listeners immediately.
__callListeners(signal);
return;
Expand Down Expand Up @@ -236,8 +248,11 @@ export function _setSignalProvider<SignalName extends keyof AlwatrSignals>(
signal: SignalObject<SignalName>,
requestSignal: SignalObject<SignalName>,
signalProvider: SignalProvider<SignalName>,
options?: SignalProviderOptions,
options: SignalProviderOptions = {},
): ListenerObject<SignalName> {
options.debounce ??= true;
options.receivePrevious ??= true;

logger.logMethodArgs('_setSignalProvider', {signal: signal.name, requestSignal: requestSignal.name, options});

if (requestSignal.listenerList.length > 0) {
Expand All @@ -256,11 +271,11 @@ export function _setSignalProvider<SignalName extends keyof AlwatrSignals>(
const signalValue = await signalProvider(requestParam);
if (signalValue !== undefined) {
// null is a valid value for signal.
_dispatchSignal(signal, signalValue, {debounce: options?.debounce ?? true});
_dispatchSignal(signal, signalValue, {debounce: options.debounce});
}
};

return _addSignalListener(requestSignal, _callback as unknown as ListenerCallback<SignalName>, {
receivePrevious: options?.receivePrevious ?? true,
receivePrevious: options.receivePrevious,
});
}