@nxtedition/lib 23.9.12 → 23.9.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/yield.js +35 -6
package/package.json
CHANGED
package/yield.js
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
|
+
import { AbortError } from './errors.js'
|
|
2
|
+
|
|
1
3
|
let yieldTime = performance.now()
|
|
2
4
|
|
|
3
|
-
export function maybeYield(opts) {
|
|
5
|
+
export function maybeYield(callback, opaque, opts) {
|
|
6
|
+
if (callback != null && typeof callback !== 'function') {
|
|
7
|
+
throw new TypeError('callback must be a function')
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
const timeout = opts?.timeout ?? 50
|
|
11
|
+
|
|
12
|
+
if (!Number.isFinite(timeout) || timeout < 0) {
|
|
13
|
+
throw new TypeError('timeout must be a finite non-negative number')
|
|
14
|
+
}
|
|
15
|
+
|
|
5
16
|
if (performance.now() - yieldTime > timeout) {
|
|
6
|
-
|
|
17
|
+
doYield(opts, callback, opaque)
|
|
18
|
+
} else {
|
|
19
|
+
callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
|
|
7
20
|
}
|
|
8
|
-
opts?.signal?.throwIfAborted()
|
|
9
21
|
}
|
|
10
22
|
|
|
11
|
-
export async function doYield(opts) {
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
export async function doYield(callback, opaque, opts) {
|
|
24
|
+
if (callback != null && typeof callback !== 'function') {
|
|
25
|
+
throw new TypeError('callback must be a function')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// TODO (fix): Use a yield queue?
|
|
29
|
+
|
|
30
|
+
setImmediate(() => {
|
|
31
|
+
resetYield(opts)
|
|
32
|
+
callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
|
|
33
|
+
})
|
|
14
34
|
}
|
|
15
35
|
|
|
16
36
|
function resetYield(opts) {
|
|
@@ -21,3 +41,12 @@ function resetYield(opts) {
|
|
|
21
41
|
setInterval(() => {
|
|
22
42
|
yieldTime = performance.now()
|
|
23
43
|
}, 500).unref()
|
|
44
|
+
|
|
45
|
+
export const promises = {
|
|
46
|
+
maybeYield(opts, opaque) {
|
|
47
|
+
return new Promise((resolve, reject) =>
|
|
48
|
+
maybeYield((err, val) => (err ? reject(err) : resolve(val)), opaque, opts),
|
|
49
|
+
)
|
|
50
|
+
},
|
|
51
|
+
resetYield,
|
|
52
|
+
}
|