-
Notifications
You must be signed in to change notification settings - Fork 630
/
abortable.ts
151 lines (145 loc) · 3.99 KB
/
abortable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { deferred } from "./deferred.ts";
/**
* Make Promise abortable with the given signal.
*
* @example
* ```typescript
* import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
* import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
*
* const p = delay(1000);
* const c = new AbortController();
* setTimeout(() => c.abort(), 100);
*
* // Below throws `DOMException` after 100 ms
* await abortable(p, c.signal);
* ```
*/
export function abortable<T>(p: Promise<T>, signal: AbortSignal): Promise<T>;
/**
* Make AsyncIterable abortable with the given signal.
*
* @example
* ```typescript
* import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
* import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
*
* const p = async function* () {
* yield "Hello";
* await delay(1000);
* yield "World";
* };
* const c = new AbortController();
* setTimeout(() => c.abort(), 100);
*
* // Below throws `DOMException` after 100 ms
* // and items become `["Hello"]`
* const items: string[] = [];
* for await (const item of abortable(p(), c.signal)) {
* items.push(item);
* }
* ```
*/
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);
}
}
/**
* Make Promise abortable with the given signal.
*
* @example
* ```typescript
* import { abortablePromise } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
*
* const request = fetch("https://example.com");
*
* const c = new AbortController();
* setTimeout(() => c.abort(), 100);
*
* const p = abortablePromise(request, c.signal);
*
* // The below throws if the request didn't resolve in 100ms
* await p;
* ```
*/
export function abortablePromise<T>(
p: Promise<T>,
signal: AbortSignal,
): Promise<T> {
if (signal.aborted) {
return Promise.reject(createAbortError(signal.reason));
}
const waiter = deferred<never>();
const abort = () => waiter.reject(createAbortError(signal.reason));
signal.addEventListener("abort", abort, { once: true });
return Promise.race([
waiter,
p.finally(() => {
signal.removeEventListener("abort", abort);
}),
]);
}
/**
* Make AsyncIterable abortable with the given signal.
*
* @example
* ```typescript
* import { abortableAsyncIterable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
* import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
*
* const p = async function* () {
* yield "Hello";
* await delay(1000);
* yield "World";
* };
* const c = new AbortController();
* setTimeout(() => c.abort(), 100);
*
* // Below throws `DOMException` after 100 ms
* // and items become `["Hello"]`
* const items: string[] = [];
* for await (const item of abortableAsyncIterable(p(), c.signal)) {
* items.push(item);
* }
* ```
*/
export async function* abortableAsyncIterable<T>(
p: AsyncIterable<T>,
signal: AbortSignal,
): AsyncGenerator<T> {
if (signal.aborted) {
throw createAbortError(signal.reason);
}
const waiter = deferred<never>();
const abort = () => waiter.reject(createAbortError(signal.reason));
signal.addEventListener("abort", abort, { once: true });
const it = p[Symbol.asyncIterator]();
while (true) {
const { done, value } = await Promise.race([waiter, it.next()]);
if (done) {
signal.removeEventListener("abort", abort);
return;
}
yield value;
}
}
// This `reason` comes from `AbortSignal` thus must be `any`.
// deno-lint-ignore no-explicit-any
function createAbortError(reason?: any): DOMException {
return new DOMException(
reason ? `Aborted: ${reason}` : "Aborted",
"AbortError",
);
}