项目作者: deeplay-io

项目描述 :
Abortable async function helpers
高级语言: TypeScript
项目地址: git://github.com/deeplay-io/abort-controller-x.git
创建时间: 2020-10-11T05:50:30Z
项目社区:https://github.com/deeplay-io/abort-controller-x

开源协议:MIT License

下载


Abort Controller Extras npm version

Abortable async function primitives and combinators.

Installation

  1. yarn add abort-controller-x

Abort Controller

See
AbortController MDN page.
AbortController is
available in NodeJS
since 15.0.0, NodeJS 14.17+ requires the
—experimental-abortcontroller
flag. A polyfill is available
for older NodeJS versions and browsers.

Abortable Functions

We define abortable function as a function that obeys following rules:

  • It must accept AbortSignal in its arguments.
  • It must return a Promise.
  • It must add
    abort
    event listener to the AbortSignal. Once the AbortSignal is aborted, the
    returned Promise must reject with AbortError either immediately, or after
    doing any async cleanup. It’s also possible to reject with other errors that
    happen during cleanup.
  • Once the returned Promise is fulfilled or rejected, it must remove
    abort
    event listener.

An example of abortable function is the standard
fetch function.

Composing Abortable Functions

This library provides a way to build complex abortable functions using standard
async/await syntax, without the burden of manually managing
abort
event listeners. You can reuse a single AbortSignal between many operations
inside a parent function:

  1. /**
  2. * Make requests repeatedly with a delay between consecutive requests
  3. */
  4. async function makeRequests(signal: AbortSignal): Promise<never> {
  5. while (true) {
  6. await fetch('...', {signal});
  7. await delay(signal, 1000);
  8. }
  9. }
  10. const abortController = new AbortController();
  11. makeRequests(abortController.signal).catch(catchAbortError);
  12. process.on('SIGTERM', () => {
  13. abortController.abort();
  14. });

The above example can be rewritten in a more ergonomic way using run
helper.

Usually you should only create AbortController somewhere on the top level, and
in regular code use async/await and pass AbortSignal to abortable
functions provided by this library or custom ones composed of other abortable
functions.

Companion Packages

API

all

  1. function all<T>(
  2. signal: AbortSignal,
  3. executor: (innerSignal: AbortSignal) => readonly PromiseLike<T>[],
  4. ): Promise<T[]>;

Abortable version of Promise.all.

Creates new inner AbortSignal and passes it to executor. That signal is
aborted when signal is aborted or any of the promises returned from executor
are rejected.

Returns a promise that fulfills with an array of results when all of the
promises returned from executor fulfill, rejects when any of the promises
returned from executor are rejected, and rejects with AbortError when
signal is aborted.

The promises returned from executor must be abortable, i.e. once innerSignal
is aborted, they must reject with AbortError either immediately, or after
doing any async cleanup.

Example:

  1. const [result1, result2] = await all(signal, signal => [
  2. makeRequest(signal, params1),
  3. makeRequest(signal, params2),
  4. ]);

race

  1. function race<T>(
  2. signal: AbortSignal,
  3. executor: (innerSignal: AbortSignal) => readonly PromiseLike<T>[],
  4. ): Promise<T>;

Abortable version of Promise.race.

Creates new inner AbortSignal and passes it to executor. That signal is
aborted when signal is aborted or any of the promises returned from executor
are fulfilled or rejected.

Returns a promise that fulfills or rejects when any of the promises returned
from executor are fulfilled or rejected, and rejects with AbortError when
signal is aborted.

The promises returned from executor must be abortable, i.e. once innerSignal
is aborted, they must reject with AbortError either immediately, or after
doing any async cleanup.

Example:

  1. const result = await race(signal, signal => [
  2. delay(signal, 1000).then(() => ({status: 'timeout'})),
  3. makeRequest(signal, params).then(value => ({status: 'success', value})),
  4. ]);
  5. if (result.status === 'timeout') {
  6. // request timed out
  7. } else {
  8. const response = result.value;
  9. }

delay

  1. function delay(signal: AbortSignal, dueTime: number | Date): Promise<void>;

Return a promise that resolves after delay and rejects with AbortError once
signal is aborted.

The delay time is specified as a Date object or as an integer denoting
milliseconds to wait.

Example:

  1. // Make a request repeatedly with a delay between consecutive requests
  2. while (true) {
  3. await makeRequest(signal, params);
  4. await delay(signal, 1000);
  5. }

Example:

  1. // Make a request repeatedly with a fixed interval
  2. import {addMilliseconds} from 'date-fns';
  3. let date = new Date();
  4. while (true) {
  5. await makeRequest(signal, params);
  6. date = addMilliseconds(date, 1000);
  7. await delay(signal, date);
  8. }

waitForEvent

  1. function waitForEvent<T>(
  2. signal: AbortSignal,
  3. target: EventTargetLike<T>,
  4. eventName: string,
  5. options?: EventListenerOptions,
  6. ): Promise<T>;

Returns a promise that fulfills when an event of specific type is emitted from
given event target and rejects with AbortError once signal is aborted.

Example:

  1. // Create a WebSocket and wait for connection
  2. const webSocket = new WebSocket(url);
  3. const openEvent = await race(signal, signal => [
  4. waitForEvent<WebSocketEventMap['open']>(signal, webSocket, 'open'),
  5. waitForEvent<WebSocketEventMap['close']>(signal, webSocket, 'close').then(
  6. event => {
  7. throw new Error(`Failed to connect to ${url}: ${event.reason}`);
  8. },
  9. ),
  10. ]);

forever

  1. function forever(signal: AbortSignal): Promise<never>;

Return a promise that never fulfills and only rejects with AbortError once
signal is aborted.

spawn

  1. function spawn<T>(
  2. signal: AbortSignal,
  3. fn: (signal: AbortSignal, effects: SpawnEffects) => Promise<T>,
  4. ): Promise<T>;
  5. type SpawnEffects = {
  6. defer(fn: () => void | Promise<void>): void;
  7. fork<T>(fn: (signal: AbortSignal) => Promise<T>): ForkTask<T>;
  8. };
  9. type ForkTask<T> = {
  10. abort(): void;
  11. join(): Promise<T>;
  12. };

Run an abortable function with fork and defer effects attached to it.

spawn allows to write Go-style coroutines.

  • SpawnEffects.defer

    Schedules a function to run after spawned function finishes.

    Deferred functions run serially in last-in-first-out order.

    Promise returned from spawn resolves or rejects only after all deferred
    functions finish.

  • SpawnEffects.fork

    Executes an abortable function in background.

    If a forked function throws an exception, spawned function and other forks are
    aborted and promise returned from spawn rejects with that exception.

    When spawned function finishes, all forks are aborted.

  • ForkTask.abort

    Abort a forked function.

  • ForkTask.join

    Returns a promise returned from a forked function.

Example:

  1. // Connect to a database, then start a server, then block until abort.
  2. // On abort, gracefully shutdown the server, and once done, disconnect
  3. // from the database.
  4. spawn(signal, async (signal, {defer}) => {
  5. const db = await connectToDb();
  6. defer(async () => {
  7. await db.close();
  8. });
  9. const server = await startServer(db);
  10. defer(async () => {
  11. await server.close();
  12. });
  13. await forever(signal);
  14. });

Example:

  1. // Connect to a database, then start an infinite polling loop.
  2. // On abort, disconnect from the database.
  3. spawn(signal, async (signal, {defer}) => {
  4. const db = await connectToDb();
  5. defer(async () => {
  6. await db.close();
  7. });
  8. while (true) {
  9. await poll(signal, db);
  10. await delay(signal, 5000);
  11. }
  12. });

Example:

  1. // Acquire a lock and execute a function.
  2. // Extend the lock while the function is running.
  3. // Once the function finishes or the signal is aborted, stop extending
  4. // the lock and release it.
  5. import Redlock = require('redlock');
  6. const lockTtl = 30_000;
  7. function withLock<T>(
  8. signal: AbortSignal,
  9. redlock: Redlock,
  10. key: string,
  11. fn: (signal: AbortSignal) => Promise<T>,
  12. ): Promise<T> {
  13. return spawn(signal, async (signal, {fork, defer}) => {
  14. const lock = await redlock.lock(key, lockTtl);
  15. defer(() => lock.unlock());
  16. fork(async signal => {
  17. while (true) {
  18. await delay(signal, lockTtl / 10);
  19. await lock.extend(lockTtl);
  20. }
  21. });
  22. return await fn(signal);
  23. });
  24. }
  25. const redlock = new Redlock([redis], {
  26. retryCount: -1,
  27. });
  28. await withLock(signal, redlock, 'the-lock-key', async signal => {
  29. // ...
  30. });

retry

  1. function retry<T>(
  2. signal: AbortSignal,
  3. fn: (signal: AbortSignal, attempt: number, reset: () => void) => Promise<T>,
  4. options?: RetryOptions,
  5. ): Promise<T>;
  6. type RetryOptions = {
  7. baseMs?: number;
  8. maxDelayMs?: number;
  9. maxAttempts?: number;
  10. onError?: (error: unknown, attempt: number, delayMs: number) => void;
  11. };

Retry a function with exponential backoff.

  • fn

    A function that will be called and retried in case of error. It receives:

    • signal

      AbortSignal that is aborted when the signal passed to retry is aborted.

    • attempt

      Attempt number starting with 0.

    • reset

      Function that sets attempt number to -1 so that the next attempt will be
      made without delay.

  • RetryOptions.baseMs

    Starting delay before first retry attempt in milliseconds.

    Defaults to 1000.

    Example: if baseMs is 100, then retries will be attempted in 100ms, 200ms,
    400ms etc (not counting jitter).

  • RetryOptions.maxDelayMs

    Maximum delay between attempts in milliseconds.

    Defaults to 30 seconds.

    Example: if baseMs is 1000 and maxDelayMs is 3000, then retries will be
    attempted in 1000ms, 2000ms, 3000ms, 3000ms etc (not counting jitter).

  • RetryOptions.maxAttempts

    Maximum for the total number of attempts.

    Defaults to Infinity.

  • RetryOptions.onError

    Called after each failed attempt before setting delay timer.

    Rethrow error from this callback to prevent further retries.

proactiveRetry

  1. function proactiveRetry<T>(
  2. signal: AbortSignal,
  3. fn: (signal: AbortSignal, attempt: number) => Promise<T>,
  4. options?: ProactiveRetryOptions,
  5. ): Promise<T>;
  6. type ProactiveRetryOptions = {
  7. baseMs?: number;
  8. maxAttempts?: number;
  9. onError?: (error: unknown, attempt: number) => void;
  10. };

Proactively retry a function with exponential backoff.

Also known as hedging.

The function will be called multiple times in parallel until it succeeds, in
which case all the other calls will be aborted.

  • fn

    A function that will be called multiple times in parallel until it succeeds.
    It receives:

    • signal

      AbortSignal that is aborted when the signal passed to retry is aborted,
      or when the function succeeds.

    • attempt

      Attempt number starting with 0.

  • ProactiveRetryOptions.baseMs

    Base delay between attempts in milliseconds.

    Defaults to 1000.

    Example: if baseMs is 100, then retries will be attempted in 100ms, 200ms,
    400ms etc (not counting jitter).

  • ProactiveRetryOptions.maxAttempts

    Maximum for the total number of attempts.

    Defaults to Infinity.

  • ProactiveRetryOptions.onError

    Called after each failed attempt.

    Rethrow error from this callback to prevent further retries.

execute

  1. function execute<T>(
  2. signal: AbortSignal,
  3. executor: (
  4. resolve: (value: T) => void,
  5. reject: (reason?: any) => void,
  6. ) => () => void | PromiseLike<void>,
  7. ): Promise<T>;

Similar to new Promise(executor), but allows executor to return abort callback
that is called once signal is aborted.

Returned promise rejects with AbortError once signal is aborted.

Callback can return a promise, e.g. for doing any async cleanup. In this case,
the promise returned from execute rejects with AbortError after that promise
fulfills.

abortable

  1. function abortable<T>(signal: AbortSignal, promise: PromiseLike<T>): Promise<T>;

Wrap a promise to reject with AbortError once signal is aborted.

Useful to wrap non-abortable promises. Note that underlying process will NOT be
aborted.

run

  1. function run(fn: (signal: AbortSignal) => Promise<void>): () => Promise<void>;

Invokes an abortable function with implicitly created AbortSignal.

Returns a function that aborts that signal and waits until passed function
finishes.

Any error other than AbortError thrown from passed function will result in
unhandled promise rejection.

Example:

  1. const stop = run(async signal => {
  2. try {
  3. while (true) {
  4. await delay(signal, 1000);
  5. console.log('tick');
  6. }
  7. } finally {
  8. await doCleanup();
  9. }
  10. });
  11. // abort and wait until cleanup is done
  12. await stop();

This function is also useful with React useEffect hook:

  1. // make requests periodically while the component is mounted
  2. useEffect(
  3. () =>
  4. run(async signal => {
  5. while (true) {
  6. await makeRequest(signal);
  7. await delay(signal, 1000);
  8. }
  9. }),
  10. [],
  11. );

AbortError

  1. class AbortError extends Error

Thrown when an abortable function was aborted.

Warning: do not use instanceof with this class. Instead, use
isAbortError function.

isAbortError

  1. function isAbortError(error: unknown): boolean;

Checks whether given error is an AbortError.

throwIfAborted

  1. function throwIfAborted(signal: AbortSignal): void;

If signal is aborted, throws AbortError. Otherwise does nothing.

rethrowAbortError

  1. function rethrowAbortError(error: unknown): void;

If error is AbortError, throws it. Otherwise does nothing.

Useful for try/catch blocks around abortable code:

  1. try {
  2. await somethingAbortable(signal);
  3. } catch (err) {
  4. rethrowAbortError(err);
  5. // do normal error handling
  6. }

catchAbortError

  1. function catchAbortError(error: unknown): void;

If error is AbortError, does nothing. Otherwise throws it.

Useful for invoking top-level abortable functions:

  1. somethingAbortable(signal).catch(catchAbortError);

Without catchAbortError, aborting would result in unhandled promise rejection.