@nxtedition/yield 1.0.18 → 1.0.19
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 +1 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +95 -134
- package/lib/index.js.map +1 -0
- package/package.json +2 -3
package/lib/index.d.ts
CHANGED
|
@@ -7,3 +7,4 @@ export declare function maybeYield(timeout?: number): Promise<void> | null;
|
|
|
7
7
|
export declare function doYield<T>(callback: (opaque: T | undefined) => void, opaque?: T): void;
|
|
8
8
|
export declare function doYield(): Promise<void>;
|
|
9
9
|
export {};
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAA;AAahD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,UAAU,GAAG,IAAI,QAE7D;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,QAK9C;AAED,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,WAU3C;AAED,wBAAgB,UAAU,CAAC,CAAC,EAC1B,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG,SAAS,KAAK,IAAI,EACzC,MAAM,CAAC,EAAE,CAAC,EACV,OAAO,CAAC,EAAE,MAAM,GACf,IAAI,CAAA;AACP,wBAAgB,UAAU,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;AAmClE,wBAAgB,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,GAAG,SAAS,KAAK,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,CAAA;AACvF,wBAAgB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA"}
|
package/lib/index.js
CHANGED
|
@@ -1,146 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
let
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
let
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
let yieldScheduled = false
|
|
12
|
-
let yieldTime = performance.now()
|
|
13
|
-
|
|
14
|
-
export function setYieldDispatcher(schedule ) {
|
|
15
|
-
yieldSchedule = schedule ?? kDefaultSchedule
|
|
1
|
+
const kDefaultSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0));
|
|
2
|
+
let yieldSchedule = kDefaultSchedule;
|
|
3
|
+
const yieldQueue = [];
|
|
4
|
+
let yieldIndex = 0;
|
|
5
|
+
let yieldTimeout = 40;
|
|
6
|
+
let yieldActive = false;
|
|
7
|
+
let yieldScheduled = false;
|
|
8
|
+
let yieldTime = performance.now();
|
|
9
|
+
export function setYieldDispatcher(schedule) {
|
|
10
|
+
yieldSchedule = schedule ?? kDefaultSchedule;
|
|
16
11
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
yieldTimeout = timeout
|
|
12
|
+
export function setYieldTimeout(timeout) {
|
|
13
|
+
if (typeof timeout !== 'number' || timeout < 0) {
|
|
14
|
+
throw new TypeError('timeout must be a positive number');
|
|
15
|
+
}
|
|
16
|
+
yieldTimeout = timeout;
|
|
23
17
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
yieldActive
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return yieldActive
|
|
18
|
+
export function shouldYield(timeout) {
|
|
19
|
+
if (timeout === undefined) {
|
|
20
|
+
timeout = yieldTimeout;
|
|
21
|
+
}
|
|
22
|
+
if (!yieldActive && performance.now() - yieldTime >= timeout) {
|
|
23
|
+
yieldActive = true;
|
|
24
|
+
}
|
|
25
|
+
return yieldActive;
|
|
35
26
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw new TypeError('callback must be a function')
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const callback = typeof callbackOrTimeout === 'function' ? callbackOrTimeout : undefined
|
|
57
|
-
if (timeout === undefined && typeof callbackOrTimeout === 'number') {
|
|
58
|
-
timeout = callbackOrTimeout
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (shouldYield(timeout)) {
|
|
27
|
+
export function maybeYield(callbackOrTimeout, opaque, timeout) {
|
|
28
|
+
if (callbackOrTimeout != null &&
|
|
29
|
+
typeof callbackOrTimeout !== 'function' &&
|
|
30
|
+
typeof callbackOrTimeout !== 'number') {
|
|
31
|
+
throw new TypeError('callback must be a function');
|
|
32
|
+
}
|
|
33
|
+
const callback = typeof callbackOrTimeout === 'function' ? callbackOrTimeout : undefined;
|
|
34
|
+
if (timeout === undefined && typeof callbackOrTimeout === 'number') {
|
|
35
|
+
timeout = callbackOrTimeout;
|
|
36
|
+
}
|
|
37
|
+
if (shouldYield(timeout)) {
|
|
38
|
+
if (callback != null) {
|
|
39
|
+
doYield(callback, opaque);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
return doYield();
|
|
43
|
+
}
|
|
62
44
|
if (callback != null) {
|
|
63
|
-
|
|
64
|
-
|
|
45
|
+
callback(opaque);
|
|
46
|
+
return;
|
|
65
47
|
}
|
|
66
|
-
return
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (callback != null) {
|
|
70
|
-
callback(opaque)
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return null
|
|
48
|
+
return null;
|
|
75
49
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (callback == null) {
|
|
93
|
-
return new Promise((resolve) => {
|
|
94
|
-
yieldQueue.push(resolve, null)
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
yieldQueue.push(callback, opaque)
|
|
50
|
+
export function doYield(callback, opaque) {
|
|
51
|
+
if (callback != null && typeof callback !== 'function') {
|
|
52
|
+
throw new TypeError('callback must be a function');
|
|
53
|
+
}
|
|
54
|
+
if (!yieldScheduled) {
|
|
55
|
+
yieldScheduled = true;
|
|
56
|
+
yieldSchedule(dispatchYield);
|
|
57
|
+
}
|
|
58
|
+
if (callback == null) {
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
yieldQueue.push(resolve, null);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
yieldQueue.push(callback, opaque);
|
|
99
64
|
}
|
|
100
|
-
|
|
101
65
|
function dispatchYield() {
|
|
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
|
-
|
|
66
|
+
yieldTime = performance.now();
|
|
67
|
+
yieldActive = false;
|
|
68
|
+
const maxIndex = yieldQueue.length;
|
|
69
|
+
while (yieldIndex < maxIndex) {
|
|
70
|
+
const resolve = yieldQueue[yieldIndex];
|
|
71
|
+
yieldQueue[yieldIndex] = null;
|
|
72
|
+
yieldIndex += 1;
|
|
73
|
+
const opaque = yieldQueue[yieldIndex];
|
|
74
|
+
yieldQueue[yieldIndex] = null;
|
|
75
|
+
yieldIndex += 1;
|
|
76
|
+
try {
|
|
77
|
+
resolve(opaque);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
// Isolate the throw: rethrow asynchronously so dispatch continues.
|
|
81
|
+
// Otherwise yieldIndex stays mid-loop and the queue gets stuck.
|
|
82
|
+
// Materialize the stack now — queueMicrotask severs the sync frame.
|
|
83
|
+
if (err instanceof Error) {
|
|
84
|
+
void err.stack;
|
|
85
|
+
}
|
|
86
|
+
queueMicrotask(() => {
|
|
87
|
+
throw err;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (shouldYield()) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
128
93
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
94
|
+
if (yieldIndex === yieldQueue.length) {
|
|
95
|
+
yieldScheduled = false;
|
|
96
|
+
yieldQueue.length = 0;
|
|
97
|
+
yieldIndex = 0;
|
|
132
98
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
} else {
|
|
140
|
-
if (yieldIndex > 4 * 1024) {
|
|
141
|
-
yieldQueue.splice(0, yieldIndex)
|
|
142
|
-
yieldIndex = 0
|
|
99
|
+
else {
|
|
100
|
+
if (yieldIndex > 4 * 1024) {
|
|
101
|
+
yieldQueue.splice(0, yieldIndex);
|
|
102
|
+
yieldIndex = 0;
|
|
103
|
+
}
|
|
104
|
+
yieldSchedule(dispatchYield);
|
|
143
105
|
}
|
|
144
|
-
yieldSchedule(dispatchYield)
|
|
145
|
-
}
|
|
146
106
|
}
|
|
107
|
+
//# sourceMappingURL=index.js.map
|
package/lib/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AAC,CAAA;IAC9B,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/yield",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "rimraf lib && tsc -p tsconfig.build.json
|
|
17
|
+
"build": "rimraf lib && tsc -p tsconfig.build.json",
|
|
18
18
|
"prepublishOnly": "yarn build",
|
|
19
19
|
"typecheck": "tsc --noEmit",
|
|
20
20
|
"test": "node --test",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^25.5.0",
|
|
26
|
-
"amaroc": "^1.0.1",
|
|
27
26
|
"oxlint-tsgolint": "^0.17.0",
|
|
28
27
|
"rimraf": "^6.1.3",
|
|
29
28
|
"tsd": "^0.33.0",
|