@nxtedition/scheduler 3.0.0 → 3.0.2

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  export type Priority = -3 | -2 | -1 | 0 | 1 | 2 | 3 | 'lowest' | 'lower' | 'low' | 'normal' | 'high' | 'higher' | 'highest';
2
- export declare function parsePriority(p: Priority | string | number): number;
2
+ export declare function parsePriority(p: string | number): number;
3
3
  export declare class Scheduler {
4
4
  #private;
5
5
  static LOWEST: Priority;
@@ -16,7 +16,6 @@ export declare class Scheduler {
16
16
  pending: number;
17
17
  queues: {
18
18
  count: number;
19
- age: number;
20
19
  }[];
21
20
  };
22
21
  static makeSharedState(concurrency: number): SharedArrayBuffer;
package/lib/index.js CHANGED
@@ -3,16 +3,10 @@ const CONCURRENCY_INDEX = 1
3
3
 
4
4
  const maxInt = 2147483647
5
5
 
6
- let fastNow = Date.now()
7
- setInterval(() => {
8
- fastNow = Date.now()
9
- }, 1e3).unref()
10
-
11
6
  class FastQueue {
12
7
  idx = 0
13
8
  cnt = 0
14
- arr = []
15
- age = fastNow
9
+ arr = []
16
10
  }
17
11
 
18
12
 
@@ -31,7 +25,7 @@ class FastQueue {
31
25
 
32
26
 
33
27
 
34
- export function parsePriority(p ) {
28
+ export function parsePriority(p ) {
35
29
  if (typeof p === 'number') {
36
30
  // Do nothing...
37
31
  } else if (p === 'lowest') {
@@ -52,7 +46,7 @@ export function parsePriority(p ) {
52
46
  p = Number(p)
53
47
  }
54
48
 
55
- if (typeof p !== 'number') {
49
+ if (typeof p !== 'number' || Number.isNaN(p)) {
56
50
  return 0
57
51
  } else if (p < -3) {
58
52
  return -3
@@ -100,7 +94,7 @@ export class Scheduler {
100
94
  deferred: this.#deferred,
101
95
  running: this.#running,
102
96
  pending: this.#pending,
103
- queues: this.#queues.map((q) => ({ count: q.cnt, age: q.age })),
97
+ queues: this.#queues.map((q) => ({ count: q.cnt })),
104
98
  }
105
99
  }
106
100
 
@@ -134,6 +128,7 @@ export class Scheduler {
134
128
  try {
135
129
  resolve(await fn(o))
136
130
  } catch (err) {
131
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
137
132
  reject(err)
138
133
  } finally {
139
134
  this.release()
@@ -146,7 +141,7 @@ export class Scheduler {
146
141
  }
147
142
 
148
143
  acquire (fn , priority = Scheduler.NORMAL, opaque ) {
149
- const p = parsePriority(priority)
144
+ const p = parsePriority(priority)
150
145
  const queue = this.#queues[p + 3]
151
146
 
152
147
  if (this.#stateView) {
@@ -156,7 +151,7 @@ export class Scheduler {
156
151
  fn(opaque)
157
152
  return
158
153
  }
159
- } else if (this.#running < 1 || this.#running < this.#concurrency) {
154
+ } else if ((this.#running < 1 && this.#concurrency > 0) || this.#running < this.#concurrency) {
160
155
  this.#running += 1
161
156
  fn(opaque)
162
157
  return
@@ -165,10 +160,6 @@ export class Scheduler {
165
160
  queue.arr.push(fn, opaque)
166
161
  queue.cnt += 1
167
162
 
168
- if (queue.cnt === 1) {
169
- queue.age = fastNow
170
- }
171
-
172
163
  this.#deferred += 1
173
164
  this.#pending += 1
174
165
  }
@@ -185,31 +176,36 @@ export class Scheduler {
185
176
 
186
177
  try {
187
178
  this.#releasing = true
188
- while (this.#pending > 0) {
179
+ while (this.#pending > 0 && (this.#running === 0 || running < this.#concurrency)) {
189
180
  let queue = null
190
181
 
191
- // Avoid starvation by randomizing the starting queue.
182
+ let idx = this.#queues.length - 1
192
183
 
193
- let idx = 6 // highest: 50%
194
- if (this.#counter & 0b0000010) {
195
- idx = 5 // higher: 25%
196
- } else if (this.#counter & 0b0000100) {
197
- idx = 4 // high: 12.5%
198
- } else if (this.#counter & 0b0001000) {
199
- idx = 3 // normal: 6.25%
200
- } else if (this.#counter & 0b0010000) {
201
- idx = 2 // low: 3.125%
202
- } else if (this.#counter & 0b0100000) {
203
- idx = 1 // lower: 1.5625%
204
- } else if (this.#counter & 0b1000000) {
205
- idx = 0 // lowest: 0.78%
184
+ // Avoid starvation by randomizing the starting queue.
185
+ {
186
+ this.#counter = (this.#counter + 1) & maxInt
187
+ if (this.#counter & 0b0000001) {
188
+ idx = 6 // highest: 50%
189
+ } else if (this.#counter & 0b0000010) {
190
+ idx = 5 // higher: 25%
191
+ } else if (this.#counter & 0b0000100) {
192
+ idx = 4 // high: 12.5%
193
+ } else if (this.#counter & 0b0001000) {
194
+ idx = 3 // normal: 6.25%
195
+ } else if (this.#counter & 0b0010000) {
196
+ idx = 2 // low: 3.125%
197
+ } else if (this.#counter & 0b0100000) {
198
+ idx = 1 // lower: 1.5625%
199
+ } else if (this.#counter & 0b1000000) {
200
+ idx = 0 // lowest: 0.78%
201
+ }
206
202
  }
207
203
 
208
204
  for (let n = idx; n >= 0 && (queue == null || queue.cnt === 0); n--) {
209
205
  queue = this.#queues[n]
210
206
  }
211
207
 
212
- for (let n = 6; n > idx && (queue == null || queue.cnt === 0); n--) {
208
+ for (let n = this.#queues.length - 1; n > idx && (queue == null || queue.cnt === 0); n--) {
213
209
  queue = this.#queues[n]
214
210
  }
215
211
 
@@ -217,23 +213,16 @@ export class Scheduler {
217
213
  throw new Error('Invariant violation: pending > 0 but no tasks in queues')
218
214
  }
219
215
 
220
- if (this.#running > 0 && running >= this.#concurrency) {
221
- break
222
- }
223
-
224
- this.#counter = (this.#counter + 1) & maxInt
225
-
226
- const fn = queue.arr[queue.idx++]
216
+ const fn = queue.arr[queue.idx++]
227
217
  const opaque = queue.arr[queue.idx++]
228
218
  queue.cnt -= 1
229
- queue.age = fastNow
230
219
 
231
220
  if (queue.cnt === 0) {
232
221
  queue.idx = 0
233
222
  queue.arr.length = 0
234
223
  } else if (queue.idx > 1024) {
235
- queue.arr.splice(0, queue.idx)
236
224
  queue.idx = 0
225
+ queue.arr.splice(0, queue.idx)
237
226
  }
238
227
 
239
228
  running = this.#stateView
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -19,6 +19,5 @@
19
19
  "amaroc": "^1.0.1",
20
20
  "rimraf": "^6.1.2",
21
21
  "typescript": "^5.9.3"
22
- },
23
- "gitHead": "dc18f1fb0cf53929205b2de3cb53c08b631a4a9d"
22
+ }
24
23
  }