Skip to content

Commit

Permalink
Auto-abort nested signal
Browse files Browse the repository at this point in the history
  • Loading branch information
smikhalevski committed Mar 19, 2024
1 parent 14ce4aa commit d237f00
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/main/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AbortablePromise } from './AbortablePromise';
import { Awaitable } from './types';
import { withSignal } from './utils';

/**
* Invokes a callback periodically with the given delay between fulfillment of returned promises until the condition is
Expand All @@ -19,7 +20,7 @@ import { Awaitable } from './types';
*/
export function repeat<I, O extends I>(
cb: (signal: AbortSignal, index: number) => Awaitable<I>,
ms: ((value: I, index: number) => number) | number,
ms: ((value: I, index: number) => number) | number | undefined,
until: (value: I, index: number) => value is O
): AbortablePromise<O>;

Expand Down Expand Up @@ -58,7 +59,7 @@ export function repeat(

(function next(index: number) {
new Promise(resolve => {
resolve(cb(signal, index));
resolve(withSignal(cb(signal, index), signal));
})
.then(value => {
if (signal.aborted) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/retry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AbortablePromise } from './AbortablePromise';
import { Awaitable } from './types';
import { withSignal } from './utils';

/**
* Invokes a callback periodically until it returns the value. If a callback throws an error or returns a promise that
Expand Down Expand Up @@ -27,7 +28,7 @@ export function retry<T>(

(function next(index: number) {
new Promise<T>(resolve => {
resolve(cb(signal, index));
resolve(withSignal(cb(signal, index), signal));
}).then(resolve, reason => {
if (signal.aborted) {
return;
Expand Down
15 changes: 3 additions & 12 deletions src/main/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AbortablePromise } from './AbortablePromise';
import { AbortableCallback } from './types';
import { withSignal } from './utils';

/**
* Returns a promise that is fulfilled with a produced value, or rejected after the timeout elapses.
Expand All @@ -11,16 +12,6 @@ import { AbortableCallback } from './types';
* @template T The value returned from the callback or promise.
*/
export function timeout<T>(cb: AbortableCallback<T> | PromiseLike<T>, ms: number): AbortablePromise<T> {
if (cb instanceof AbortablePromise) {
return timeout(signal => {
signal.addEventListener('abort', () => {
cb.abort(signal.reason);
});

return cb;
}, ms);
}

if (typeof cb !== 'function') {
return timeout(() => cb, ms);
}
Expand All @@ -34,8 +25,8 @@ export function timeout<T>(cb: AbortableCallback<T> | PromiseLike<T>, ms: number
clearTimeout(timer);
});

new Promise<T>(resolveValue => {
resolveValue(cb(signal));
new Promise<T>(resolve => {
resolve(withSignal(cb(signal), signal));
}).then(
value => {
clearTimeout(timer);
Expand Down
6 changes: 6 additions & 0 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AbortablePromise } from './AbortablePromise';

export function noop() {}

/**
Expand All @@ -13,3 +15,7 @@ export function isEqual(a: unknown, b: unknown): boolean {
export function isPromiseLike<T>(value: any): value is PromiseLike<T> {
return value !== null && typeof value === 'object' && typeof value.then === 'function';
}

export function withSignal<T>(value: T, signal: AbortSignal): T {
return value instanceof AbortablePromise ? value.withSignal(signal) : value;
}

0 comments on commit d237f00

Please sign in to comment.