@nxtedition/lib 23.15.6 → 23.16.0
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 +21 -15
package/package.json
CHANGED
package/yield.js
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
import { FixedQueue } from './fixed-queue.js'
|
|
2
2
|
|
|
3
|
-
const yieldTimeout = 50
|
|
4
3
|
const yieldQueue = new FixedQueue()
|
|
5
4
|
const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
|
|
6
|
-
|
|
5
|
+
|
|
6
|
+
let yieldTimeout = 40
|
|
7
|
+
let yieldState = 0
|
|
7
8
|
let yieldTime = performance.now()
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
export function setYieldTimeout(timeout) {
|
|
11
|
+
if (typeof timeout !== 'number' || timeout < 0) {
|
|
12
|
+
throw new TypeError('timeout must be a positive number')
|
|
13
|
+
}
|
|
14
|
+
yieldTimeout = timeout
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function shouldYield() {
|
|
18
|
+
return yieldState > 0 || performance.now() - yieldTime >= yieldTimeout
|
|
19
|
+
}
|
|
9
20
|
|
|
10
21
|
export function maybeYield(callback) {
|
|
11
22
|
if (callback != null && typeof callback !== 'function') {
|
|
12
23
|
throw new TypeError('callback must be a function')
|
|
13
24
|
}
|
|
14
25
|
|
|
15
|
-
return
|
|
26
|
+
return shouldYield() ? (doYield(callback) ?? true) : null
|
|
16
27
|
}
|
|
17
28
|
|
|
18
29
|
export function doYield(callback) {
|
|
19
|
-
if (
|
|
20
|
-
|
|
30
|
+
if (yieldState === 0) {
|
|
31
|
+
yieldState = 1
|
|
21
32
|
yieldSchedule(dispatchYield)
|
|
22
33
|
}
|
|
23
34
|
|
|
24
35
|
if (callback) {
|
|
25
36
|
yieldQueue.push(callback)
|
|
26
|
-
return true
|
|
27
37
|
} else {
|
|
28
38
|
return new Promise((resolve) => {
|
|
29
39
|
yieldQueue.push(resolve)
|
|
@@ -32,11 +42,11 @@ export function doYield(callback) {
|
|
|
32
42
|
}
|
|
33
43
|
|
|
34
44
|
function dispatchYield() {
|
|
35
|
-
if (
|
|
45
|
+
if (yieldState === 2) {
|
|
36
46
|
return
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
yieldState = 2
|
|
40
50
|
yieldTime = performance.now()
|
|
41
51
|
|
|
42
52
|
while (!yieldQueue.isEmpty()) {
|
|
@@ -45,15 +55,11 @@ function dispatchYield() {
|
|
|
45
55
|
resolve(null)
|
|
46
56
|
|
|
47
57
|
if (performance.now() - yieldTime > yieldTimeout) {
|
|
58
|
+
yieldState = 1
|
|
48
59
|
yieldSchedule(dispatchYield)
|
|
49
60
|
return
|
|
50
61
|
}
|
|
51
62
|
}
|
|
52
63
|
|
|
53
|
-
|
|
54
|
-
yieldScheduled = false
|
|
64
|
+
yieldState = 0
|
|
55
65
|
}
|
|
56
|
-
|
|
57
|
-
setInterval(() => {
|
|
58
|
-
yieldTime = performance.now()
|
|
59
|
-
}, 500).unref()
|