@nxtedition/scheduler 1.0.6 → 1.0.8
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 -1
- package/lib/index.js +38 -25
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -8,5 +8,5 @@ export declare class Scheduler {
|
|
|
8
8
|
constructor(opts?: SharedArrayBuffer | {
|
|
9
9
|
concurrency?: number;
|
|
10
10
|
});
|
|
11
|
-
schedule(fn: (next: Function) => any, priority?: 0 | 1 | 2 |
|
|
11
|
+
schedule(fn: (next: Function) => any, priority?: 0 | 1 | 2 | 'low' | 'normal' | 'high'): any;
|
|
12
12
|
}
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,7 @@ export class Scheduler {
|
|
|
15
15
|
#running = 0
|
|
16
16
|
#pending = 0
|
|
17
17
|
#counter = 0
|
|
18
|
+
#actived = false
|
|
18
19
|
|
|
19
20
|
#lowQueue = new FixedQueue()
|
|
20
21
|
#normalQueue = new FixedQueue()
|
|
@@ -43,7 +44,10 @@ export class Scheduler {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
schedule(
|
|
47
|
+
schedule(
|
|
48
|
+
fn ,
|
|
49
|
+
priority = Scheduler.NORMAL,
|
|
50
|
+
) {
|
|
47
51
|
if (typeof fn !== 'function') {
|
|
48
52
|
throw new TypeError('First argument must be a function')
|
|
49
53
|
}
|
|
@@ -87,32 +91,41 @@ export class Scheduler {
|
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
#next = () => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
: this.#counter++
|
|
99
|
-
const fn =
|
|
100
|
-
((counter & 63) === 0 && this.#lowQueue.shift()) ??
|
|
101
|
-
((counter & 15) === 0 && this.#normalQueue.shift()) ??
|
|
102
|
-
this.#highQueue.shift() ??
|
|
103
|
-
this.#normalQueue.shift() ??
|
|
104
|
-
this.#lowQueue.shift()
|
|
105
|
-
|
|
106
|
-
if (!fn) {
|
|
107
|
-
break
|
|
94
|
+
try {
|
|
95
|
+
let running = this.#stateView
|
|
96
|
+
? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
|
|
97
|
+
: this.#running - 1
|
|
98
|
+
this.#running -= 1
|
|
99
|
+
|
|
100
|
+
if (this.#actived) {
|
|
101
|
+
return
|
|
108
102
|
}
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
104
|
+
this.#actived = true
|
|
105
|
+
while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
|
|
106
|
+
const counter = this.#stateView
|
|
107
|
+
? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
|
|
108
|
+
: this.#counter++
|
|
109
|
+
const fn =
|
|
110
|
+
((counter & 63) === 0 && this.#lowQueue.shift()) ||
|
|
111
|
+
((counter & 15) === 0 && this.#normalQueue.shift()) ||
|
|
112
|
+
this.#highQueue.shift() ||
|
|
113
|
+
this.#normalQueue.shift() ||
|
|
114
|
+
this.#lowQueue.shift()
|
|
115
|
+
|
|
116
|
+
running = this.#stateView
|
|
117
|
+
? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
|
|
118
|
+
: this.#running + 1
|
|
119
|
+
this.#running += 1
|
|
120
|
+
this.#pending -= 1
|
|
121
|
+
fn(this.#next)
|
|
122
|
+
}
|
|
123
|
+
this.#actived = false
|
|
124
|
+
} catch (err) {
|
|
125
|
+
// Throwing here is undefined behavior...
|
|
126
|
+
queueMicrotask(() => {
|
|
127
|
+
throw new Error('Scheduler task error', { cause: err })
|
|
128
|
+
})
|
|
116
129
|
}
|
|
117
130
|
}
|
|
118
131
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/scheduler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -20,5 +20,5 @@
|
|
|
20
20
|
"rimraf": "^6.1.2",
|
|
21
21
|
"typescript": "^5.9.3"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "ba7f0417c8dbf2a8d6e38f5848f5e8ec162f4eb7"
|
|
24
24
|
}
|