@nxtedition/scheduler 1.0.5 → 1.0.7
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 +46 -28
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
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,16 +44,24 @@ export class Scheduler {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
schedule(fn , priority
|
|
47
|
+
schedule(fn , priority = Scheduler.NORMAL) {
|
|
47
48
|
if (typeof fn !== 'function') {
|
|
48
49
|
throw new TypeError('First argument must be a function')
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
if (priority == null) {
|
|
52
53
|
priority = Scheduler.NORMAL
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
} else if (typeof priority === 'string') {
|
|
55
|
+
if (priority === 'low') {
|
|
56
|
+
priority = Scheduler.LOW
|
|
57
|
+
} else if (priority === 'normal') {
|
|
58
|
+
priority = Scheduler.NORMAL
|
|
59
|
+
} else if (priority === 'high') {
|
|
60
|
+
priority = Scheduler.HIGH
|
|
61
|
+
} else {
|
|
62
|
+
throw new Error('Invalid priority')
|
|
63
|
+
}
|
|
64
|
+
} else if (!Number.isInteger(priority)) {
|
|
56
65
|
throw new Error('Invalid priority')
|
|
57
66
|
}
|
|
58
67
|
|
|
@@ -79,32 +88,41 @@ export class Scheduler {
|
|
|
79
88
|
}
|
|
80
89
|
|
|
81
90
|
#next = () => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
: this.#counter++
|
|
91
|
-
const fn =
|
|
92
|
-
((counter & 63) === 0 && this.#lowQueue.shift()) ??
|
|
93
|
-
((counter & 15) === 0 && this.#normalQueue.shift()) ??
|
|
94
|
-
this.#highQueue.shift() ??
|
|
95
|
-
this.#normalQueue.shift() ??
|
|
96
|
-
this.#lowQueue.shift()
|
|
97
|
-
|
|
98
|
-
if (!fn) {
|
|
99
|
-
break
|
|
91
|
+
try {
|
|
92
|
+
let running = this.#stateView
|
|
93
|
+
? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
|
|
94
|
+
: this.#running - 1
|
|
95
|
+
this.#running -= 1
|
|
96
|
+
|
|
97
|
+
if (this.#actived) {
|
|
98
|
+
return
|
|
100
99
|
}
|
|
101
100
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
this.#actived = true
|
|
102
|
+
while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
|
|
103
|
+
const counter = this.#stateView
|
|
104
|
+
? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
|
|
105
|
+
: this.#counter++
|
|
106
|
+
const fn =
|
|
107
|
+
((counter & 63) === 0 && this.#lowQueue.shift()) ??
|
|
108
|
+
((counter & 15) === 0 && this.#normalQueue.shift()) ??
|
|
109
|
+
this.#highQueue.shift() ??
|
|
110
|
+
this.#normalQueue.shift() ??
|
|
111
|
+
this.#lowQueue.shift()
|
|
112
|
+
|
|
113
|
+
running = this.#stateView
|
|
114
|
+
? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
|
|
115
|
+
: this.#running + 1
|
|
116
|
+
this.#running += 1
|
|
117
|
+
this.#pending -= 1
|
|
118
|
+
fn(this.#next)
|
|
119
|
+
}
|
|
120
|
+
this.#actived = false
|
|
121
|
+
} catch (err) {
|
|
122
|
+
// Throwing here is undefined behavior...
|
|
123
|
+
queueMicrotask(() => {
|
|
124
|
+
throw new Error('Scheduler task error', { cause: err })
|
|
125
|
+
})
|
|
108
126
|
}
|
|
109
127
|
}
|
|
110
128
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/scheduler",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
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": "8a7862ed029e6ec42bbccd925bf638e7b069cc5e"
|
|
24
24
|
}
|