@nxtedition/yield 1.0.0-alpha.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/lib/index.d.ts +7 -0
- package/lib/index.js +113 -0
- package/package.json +23 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type Dispatcher = (callback: () => void) => void;
|
|
2
|
+
export declare function setYieldDispatcher(schedule: Dispatcher | null): void;
|
|
3
|
+
export declare function setYieldTimeout(timeout: number): void;
|
|
4
|
+
export declare function shouldYield(timeout?: number): boolean;
|
|
5
|
+
export declare function maybeYield<T>(callback?: (opaque: T | undefined) => void, opaque?: T, timeout?: number | undefined): Promise<null> | null;
|
|
6
|
+
export declare function doYield<T>(callback?: (opaque: T | undefined) => void, opaque?: T): Promise<null> | null;
|
|
7
|
+
export {};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
const kDefaultSchedule =
|
|
4
|
+
globalThis.setImmediate ?? ((fn ) => setTimeout(fn, 0))
|
|
5
|
+
let yieldSchedule = kDefaultSchedule
|
|
6
|
+
const yieldQueue = []
|
|
7
|
+
|
|
8
|
+
let yieldIndex = 0
|
|
9
|
+
let yieldTimeout = 40
|
|
10
|
+
let yieldActive = false
|
|
11
|
+
let yieldScheduled = false
|
|
12
|
+
let yieldTime = performance.now()
|
|
13
|
+
|
|
14
|
+
export function setYieldDispatcher(schedule ) {
|
|
15
|
+
yieldSchedule = schedule ?? kDefaultSchedule
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function setYieldTimeout(timeout ) {
|
|
19
|
+
if (typeof timeout !== 'number' || timeout < 0) {
|
|
20
|
+
throw new TypeError('timeout must be a positive number')
|
|
21
|
+
}
|
|
22
|
+
yieldTimeout = timeout
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function shouldYield(timeout ) {
|
|
26
|
+
if (timeout === undefined) {
|
|
27
|
+
timeout = yieldTimeout
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!yieldActive && performance.now() - yieldTime >= timeout) {
|
|
31
|
+
yieldActive = true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return yieldActive
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function maybeYield (
|
|
38
|
+
callback ,
|
|
39
|
+
opaque ,
|
|
40
|
+
timeout ,
|
|
41
|
+
) {
|
|
42
|
+
if (callback != null && typeof callback !== 'function') {
|
|
43
|
+
throw new TypeError('callback must be a function')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (shouldYield(timeout)) {
|
|
47
|
+
return doYield(callback, opaque)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (callback != null) {
|
|
51
|
+
callback(opaque)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function doYield (
|
|
58
|
+
callback ,
|
|
59
|
+
opaque ,
|
|
60
|
+
) {
|
|
61
|
+
if (callback != null && typeof callback !== 'function') {
|
|
62
|
+
throw new TypeError('callback must be a function')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!yieldScheduled) {
|
|
66
|
+
yieldScheduled = true
|
|
67
|
+
yieldSchedule(dispatchYield)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (callback != null) {
|
|
71
|
+
yieldQueue.push(callback, opaque)
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
yieldQueue.push(resolve, null)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function dispatchYield() {
|
|
81
|
+
yieldTime = performance.now()
|
|
82
|
+
yieldActive = false
|
|
83
|
+
|
|
84
|
+
const maxIndex = yieldQueue.length
|
|
85
|
+
|
|
86
|
+
while (yieldIndex < maxIndex) {
|
|
87
|
+
const resolve = yieldQueue[yieldIndex]
|
|
88
|
+
yieldQueue[yieldIndex] = null
|
|
89
|
+
yieldIndex += 1
|
|
90
|
+
|
|
91
|
+
const opaque = yieldQueue[yieldIndex]
|
|
92
|
+
yieldQueue[yieldIndex] = null
|
|
93
|
+
yieldIndex += 1
|
|
94
|
+
|
|
95
|
+
resolve(opaque)
|
|
96
|
+
|
|
97
|
+
if (shouldYield()) {
|
|
98
|
+
break
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (yieldIndex === yieldQueue.length) {
|
|
103
|
+
yieldScheduled = false
|
|
104
|
+
yieldQueue.splice(0)
|
|
105
|
+
yieldIndex = 0
|
|
106
|
+
} else {
|
|
107
|
+
if (yieldIndex > 16 * 1024) {
|
|
108
|
+
yieldQueue.splice(0, yieldIndex)
|
|
109
|
+
yieldIndex = 0
|
|
110
|
+
}
|
|
111
|
+
yieldSchedule(dispatchYield)
|
|
112
|
+
}
|
|
113
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nxtedition/yield",
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"license": "UNLICENSED",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rimraf lib && tsc && amaroc ./src/index.ts && mv src/index.js lib/",
|
|
13
|
+
"prepublishOnly": "yarn build",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"test": "node --test"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"amaroc": "^1.0.1",
|
|
19
|
+
"rimraf": "^6.0.1",
|
|
20
|
+
"typescript": "^5.9.3"
|
|
21
|
+
},
|
|
22
|
+
"gitHead": "7e4d71a9276d64d7cb8c39eaed681121903e16a4"
|
|
23
|
+
}
|