@nxtedition/lib 23.16.4 → 23.17.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/logger.js +1 -1
- package/package.json +1 -2
- package/yield.js +37 -12
- package/scheduler.js +0 -32
package/logger.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "23.
|
|
3
|
+
"version": "23.17.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,6 @@
|
|
|
39
39
|
"errors.js",
|
|
40
40
|
"errors.d.ts",
|
|
41
41
|
"worker.js",
|
|
42
|
-
"scheduler.js",
|
|
43
42
|
"stream.js",
|
|
44
43
|
"timeline.js",
|
|
45
44
|
"transcript.js",
|
package/yield.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { FixedQueue } from './fixed-queue.js'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const yieldQueues = [new FixedQueue(), new FixedQueue(), new FixedQueue()]
|
|
4
|
+
const yieldQueueLength = yieldQueues.length
|
|
5
|
+
|
|
6
|
+
export const PRIORITY_HIGH = 0
|
|
7
|
+
export const PRIORITY_NORMAL = 1
|
|
8
|
+
export const PRIORITY_LOW = 2
|
|
9
|
+
|
|
4
10
|
const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
|
|
5
11
|
|
|
6
12
|
let yieldTimeout = 40
|
|
@@ -22,13 +28,13 @@ export function shouldYield() {
|
|
|
22
28
|
return yieldActive
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
export function maybeYield(callback) {
|
|
31
|
+
export function maybeYield(callback, priority) {
|
|
26
32
|
if (callback != null && typeof callback !== 'function') {
|
|
27
33
|
throw new TypeError('callback must be a function')
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
if (shouldYield()) {
|
|
31
|
-
return doYield(callback)
|
|
37
|
+
return doYield(callback, priority)
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
if (callback != null) {
|
|
@@ -38,23 +44,39 @@ export function maybeYield(callback) {
|
|
|
38
44
|
return null
|
|
39
45
|
}
|
|
40
46
|
|
|
41
|
-
export function doYield(
|
|
47
|
+
export function doYield(callbackOrPriority, priorityOrNil) {
|
|
48
|
+
let callback = callbackOrPriority
|
|
49
|
+
let priority = priorityOrNil
|
|
50
|
+
|
|
51
|
+
if (callbackOrPriority == null) {
|
|
52
|
+
// Do nothing...
|
|
53
|
+
} else if (typeof callbackOrPriority === 'number' && priorityOrNil == null) {
|
|
54
|
+
priority = callbackOrPriority
|
|
55
|
+
callback = null
|
|
56
|
+
} else {
|
|
57
|
+
callback = callbackOrPriority
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
if (callback != null && typeof callback !== 'function') {
|
|
43
61
|
throw new TypeError('callback must be a function')
|
|
44
62
|
}
|
|
45
63
|
|
|
64
|
+
if (priority != null && typeof priority !== 'number') {
|
|
65
|
+
throw new TypeError('priority must be a number')
|
|
66
|
+
}
|
|
67
|
+
|
|
46
68
|
if (!yieldScheduled) {
|
|
47
69
|
yieldScheduled = true
|
|
48
70
|
yieldSchedule(dispatchYield)
|
|
49
71
|
}
|
|
50
72
|
|
|
51
73
|
if (callback != null) {
|
|
52
|
-
|
|
74
|
+
yieldQueues[priority ?? PRIORITY_NORMAL].push(callback)
|
|
53
75
|
return null
|
|
54
76
|
}
|
|
55
77
|
|
|
56
78
|
return new Promise((resolve) => {
|
|
57
|
-
|
|
79
|
+
yieldQueues[priority ?? PRIORITY_NORMAL].push(resolve)
|
|
58
80
|
})
|
|
59
81
|
}
|
|
60
82
|
|
|
@@ -62,14 +84,17 @@ function dispatchYield() {
|
|
|
62
84
|
yieldTime = performance.now()
|
|
63
85
|
yieldActive = false
|
|
64
86
|
|
|
65
|
-
|
|
66
|
-
const
|
|
87
|
+
for (let n = 0; n < yieldQueueLength; n++) {
|
|
88
|
+
const yieldQueue = yieldQueues[n]
|
|
89
|
+
while (!yieldQueue.isEmpty()) {
|
|
90
|
+
const resolve = yieldQueue.shift()
|
|
67
91
|
|
|
68
|
-
|
|
92
|
+
resolve(null)
|
|
69
93
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
94
|
+
if (shouldYield()) {
|
|
95
|
+
yieldSchedule(dispatchYield)
|
|
96
|
+
return
|
|
97
|
+
}
|
|
73
98
|
}
|
|
74
99
|
}
|
|
75
100
|
|
package/scheduler.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { setImmediate } from 'timers/promises'
|
|
2
|
-
|
|
3
|
-
let yieldTime = performance.now()
|
|
4
|
-
let yieldPromise = undefined
|
|
5
|
-
|
|
6
|
-
function reset() {
|
|
7
|
-
yieldTime = performance.now()
|
|
8
|
-
yieldPromise = undefined
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
setInterval(reset, 100).unref()
|
|
12
|
-
|
|
13
|
-
export function shouldYield() {
|
|
14
|
-
if (yieldPromise === undefined) {
|
|
15
|
-
yieldPromise = performance.now() - yieldTime > 50 ? null : undefined
|
|
16
|
-
}
|
|
17
|
-
return yieldPromise !== undefined
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function maybeYield() {
|
|
21
|
-
if (yieldPromise === undefined) {
|
|
22
|
-
yieldPromise = performance.now() - yieldTime > 50 ? null : undefined
|
|
23
|
-
}
|
|
24
|
-
if (yieldPromise === null) {
|
|
25
|
-
yieldPromise = setImmediate().then(reset)
|
|
26
|
-
}
|
|
27
|
-
return yieldPromise ?? null
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export async function yield() {
|
|
31
|
-
return (yieldPromise ??= setImmediate().then(reset))
|
|
32
|
-
}
|