2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-09-19 23:29:31 +00:00
|
|
|
// TODO(iuioiua): Remove `ignore` directives from following snippets
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2024-05-22 00:40:43 +00:00
|
|
|
* Make a {@linkcode Promise} abortable with the given signal.
|
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* @throws {DOMException} If the signal is already aborted and `signal.reason`
|
|
|
|
* is undefined. Otherwise, throws `signal.reason`.
|
2024-05-22 00:40:43 +00:00
|
|
|
* @typeParam T The type of the provided and returned promise.
|
|
|
|
* @param p The promise to make abortable.
|
|
|
|
* @param signal The signal to abort the promise with.
|
|
|
|
* @returns A promise that can be aborted.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* @example Error-handling a timeout
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-06-21 06:37:23 +00:00
|
|
|
* import { abortable, delay } from "@std/async";
|
|
|
|
* import { assertRejects, assertEquals } from "@std/assert";
|
|
|
|
*
|
|
|
|
* const promise = delay(1_000);
|
|
|
|
*
|
|
|
|
* // Rejects with `DOMException` after 100 ms
|
|
|
|
* await assertRejects(
|
|
|
|
* () => abortable(promise, AbortSignal.timeout(100)),
|
|
|
|
* DOMException,
|
|
|
|
* "Signal timed out."
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Error-handling an abort
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-06-21 06:37:23 +00:00
|
|
|
* import { abortable, delay } from "@std/async";
|
|
|
|
* import { assertRejects, assertEquals } from "@std/assert";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* const promise = delay(1_000);
|
|
|
|
* const controller = new AbortController();
|
|
|
|
* controller.abort(new Error("This is my reason"));
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* // Rejects with `DOMException` immediately
|
|
|
|
* await assertRejects(
|
|
|
|
* () => abortable(promise, controller.signal),
|
|
|
|
* Error,
|
|
|
|
* "This is my reason"
|
|
|
|
* );
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2022-02-21 04:51:39 +00:00
|
|
|
export function abortable<T>(p: Promise<T>, signal: AbortSignal): Promise<T>;
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
2024-05-22 00:40:43 +00:00
|
|
|
* Make an {@linkcode AsyncIterable} abortable with the given signal.
|
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* @throws {DOMException} If the signal is already aborted and `signal.reason`
|
|
|
|
* is undefined. Otherwise, throws `signal.reason`.
|
2024-05-22 00:40:43 +00:00
|
|
|
* @typeParam T The type of the provided and returned async iterable.
|
|
|
|
* @param p The async iterable to make abortable.
|
|
|
|
* @param signal The signal to abort the promise with.
|
|
|
|
* @returns An async iterable that can be aborted.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* @example Error-handling a timeout
|
|
|
|
* ```ts
|
|
|
|
* import { abortable, delay } from "@std/async";
|
|
|
|
* import { assertRejects, assertEquals } from "@std/assert";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-06-21 06:37:23 +00:00
|
|
|
* const asyncIter = async function* () {
|
2022-11-25 11:40:23 +00:00
|
|
|
* yield "Hello";
|
2024-06-21 06:37:23 +00:00
|
|
|
* await delay(1_000);
|
2022-11-25 11:40:23 +00:00
|
|
|
* yield "World";
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* const items: string[] = [];
|
2024-06-21 06:37:23 +00:00
|
|
|
* // Below throws `DOMException` after 100 ms and items become `["Hello"]`
|
|
|
|
* await assertRejects(
|
|
|
|
* async () => {
|
|
|
|
* for await (const item of abortable(asyncIter(), AbortSignal.timeout(100))) {
|
|
|
|
* items.push(item);
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* DOMException,
|
|
|
|
* "Signal timed out."
|
|
|
|
* );
|
|
|
|
* assertEquals(items, ["Hello"]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Error-handling an abort
|
|
|
|
* ```ts
|
|
|
|
* import { abortable, delay } from "@std/async";
|
|
|
|
* import { assertRejects, assertEquals } from "@std/assert";
|
|
|
|
*
|
|
|
|
* const asyncIter = async function* () {
|
|
|
|
* yield "Hello";
|
|
|
|
* await delay(1_000);
|
|
|
|
* yield "World";
|
|
|
|
* };
|
|
|
|
* const controller = new AbortController();
|
|
|
|
* controller.abort(new Error("This is my reason"));
|
|
|
|
*
|
|
|
|
* const items: string[] = [];
|
|
|
|
* // Below throws `DOMException` immediately
|
|
|
|
* await assertRejects(
|
|
|
|
* async () => {
|
|
|
|
* for await (const item of abortable(asyncIter(), controller.signal)) {
|
|
|
|
* items.push(item);
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* Error,
|
|
|
|
* "This is my reason"
|
|
|
|
* );
|
|
|
|
* assertEquals(items, []);
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2024-08-05 05:31:35 +00:00
|
|
|
|
2022-02-21 04:51:39 +00:00
|
|
|
export function abortable<T>(
|
|
|
|
p: AsyncIterable<T>,
|
|
|
|
signal: AbortSignal,
|
|
|
|
): AsyncGenerator<T>;
|
|
|
|
export function abortable<T>(
|
|
|
|
p: Promise<T> | AsyncIterable<T>,
|
|
|
|
signal: AbortSignal,
|
|
|
|
): Promise<T> | AsyncIterable<T> {
|
|
|
|
if (p instanceof Promise) {
|
|
|
|
return abortablePromise(p, signal);
|
|
|
|
} else {
|
|
|
|
return abortableAsyncIterable(p, signal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 05:26:44 +00:00
|
|
|
function abortablePromise<T>(
|
2022-03-17 05:09:32 +00:00
|
|
|
p: Promise<T>,
|
|
|
|
signal: AbortSignal,
|
|
|
|
): Promise<T> {
|
2024-06-21 06:37:23 +00:00
|
|
|
if (signal.aborted) return Promise.reject(signal.reason);
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, reject } = Promise.withResolvers<never>();
|
2024-06-21 06:37:23 +00:00
|
|
|
const abort = () => reject(signal.reason);
|
2022-02-21 04:51:39 +00:00
|
|
|
signal.addEventListener("abort", abort, { once: true });
|
2023-12-07 21:36:52 +00:00
|
|
|
return Promise.race([promise, p]).finally(() => {
|
|
|
|
signal.removeEventListener("abort", abort);
|
|
|
|
});
|
2022-02-21 04:51:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 05:26:44 +00:00
|
|
|
async function* abortableAsyncIterable<T>(
|
2022-02-21 04:51:39 +00:00
|
|
|
p: AsyncIterable<T>,
|
|
|
|
signal: AbortSignal,
|
|
|
|
): AsyncGenerator<T> {
|
2024-06-21 06:37:23 +00:00
|
|
|
signal.throwIfAborted();
|
2023-11-10 03:31:16 +00:00
|
|
|
const { promise, reject } = Promise.withResolvers<never>();
|
2024-06-21 06:37:23 +00:00
|
|
|
const abort = () => reject(signal.reason);
|
2022-02-21 04:51:39 +00:00
|
|
|
signal.addEventListener("abort", abort, { once: true });
|
|
|
|
|
|
|
|
const it = p[Symbol.asyncIterator]();
|
2024-08-01 00:25:42 +00:00
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
const race = Promise.race([promise, it.next()]);
|
|
|
|
race.catch(() => {
|
|
|
|
signal.removeEventListener("abort", abort);
|
|
|
|
});
|
|
|
|
const { done, value } = await race;
|
|
|
|
if (done) {
|
|
|
|
signal.removeEventListener("abort", abort);
|
2024-08-05 05:31:35 +00:00
|
|
|
const result = await it.return?.(value);
|
|
|
|
return result?.value;
|
2024-08-01 00:25:42 +00:00
|
|
|
}
|
|
|
|
yield value;
|
2022-02-21 04:51:39 +00:00
|
|
|
}
|
2024-08-01 00:25:42 +00:00
|
|
|
} catch (e) {
|
|
|
|
await it.return?.();
|
|
|
|
throw e;
|
2022-02-21 04:51:39 +00:00
|
|
|
}
|
|
|
|
}
|