-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
153 lines (129 loc) · 4 KB
/
promises.js
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
152
/**
* @copyright 2023 Chris Zuber <[email protected]>
*/
import { signalAborted } from './abort.js';
export const infinitPromise = new Promise(() => {});
export async function locksSupported() {
if ('locks' in navigator) {
try {
return await navigator.locks.request('lock-test', () => true);
} catch {
return false;
}
}
}
export function isAsyncFunction(what) {
return what instanceof Function && what.constructor.name === 'AsyncFunction';
}
export function isAsync(what) {
return isAsyncFunction(what) || what instanceof Promise;
}
export function getDeferred({ signal } = {}) {
const deferred = Promise.withResolvers();
if (signal instanceof AbortSignal) {
if (signal.aborted) {
deferred.reject(signal.reason);
} else {
signal.addEventListener('abort', ({ target }) => deferred.reject(target.reason), { once: true });
}
}
return deferred;
}
export async function callAsAsync(callback, args = [], { thisArg = globalThis, signal } = {}) {
const { promise, resolve, reject } = getDeferred({ signal });
if (! (callback instanceof Function)) {
reject(new TypeError('`callAsAsync` expects callback to be a function'));
} else if (! Array.isArray(args)) {
reject(new TypeError('`args` must be an array'));
} else if (signal instanceof AbortSignal && signal.aborted) {
reject(signal.reason);
} else if (isAsyncFunction(callback)) {
callback.call(thisArg, args).then(resolve).catch(reject);
} else {
queueMicrotask(() => {
try {
resolve(callback.call(thisArg, args));
} catch(err) {
reject(err);
}
});
}
return await promise;
}
export function createDeferredCallback(callback, { signal, thisArg } = {}) {
const { promise, resolve } = getDeferred({ signal });
const retPromise = promise.then(() => callAsAsync(callback, [], { signal, thisArg }));
return async () => {
resolve();
return await retPromise;
};
}
export async function lock(name, callback, {
thisArg = globalThis,
args = [],
mode = 'exclusive',
ifAvailable = false,
steal = false,
allowFallback = true,
signal,
} = {}) {
if ((! allowFallback) || await locksSupported()) {
return await navigator.locks.request(name, { mode, ifAvailable, steal, signal }, async lock => {
if (lock) {
return await callAsAsync(callback, [lock, ...args], { thisArg, signal });
}
});
} else {
return await callAsAsync(callback, [null, ...args], { signal, thisArg });
}
}
export async function onAnimationFrame(callback, {
thisArg = globalThis,
args = [],
signal,
} = {}) {
const { promise, resolve, reject } = getDeferred({ signal });
const id = requestAnimationFrame(hrts => callAsAsync(callback, [hrts, ...args], { signal, thisArg }).then(resolve, reject));
if (signal instanceof AbortSignal) {
signalAborted(signal).finally(() => cancelAnimationFrame(id));
}
return await promise;
}
export async function onIdle(callback, {
timeout,
thisArg = globalThis,
args = [],
signal,
} = {}) {
const { promise, resolve, reject } = getDeferred({ signal });
const id = requestIdleCallback(hrts => callAsAsync(callback, [hrts, ...args], { thisArg, signal }).then(resolve, reject), { timeout });
if (signal instanceof AbortSignal) {
signalAborted(signal).finally(() => cancelIdleCallback(id));
}
return await promise;
}
export async function onTimeout(callback, {
timeout = 0,
thisArg = globalThis,
args = [],
signal,
} = {}) {
const { resolve, reject, promise } = getDeferred({ signal });
if (Number.isSafeInteger(timeout) && timeout >= 0) {
const id = setTimeout(() => {
callAsAsync(callback, args, { signal, thisArg }).then(resolve).catch(reject);
}, timeout);
if (signal instanceof AbortSignal) {
signalAborted(signal).finally(() => clearTimeout(id));
}
} else {
reject(new TypeError('`timeout` must be a positive intege'));
}
return await promise;
}
export async function sleep(timeout, { signal } = {}) {
const { resolve, promise } = getDeferred({ signal });
onTimeout(() => resolve(performance.now()), { signal, timeout })
.catch(() => resolve(performance.now()));
return await promise;
}