@nxtedition/scheduler 1.0.2 → 1.0.4

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
@@ -3,6 +3,7 @@ export declare class Scheduler {
3
3
  static LOW: 0;
4
4
  static NORMAL: 1;
5
5
  static HIGH: 2;
6
+ get concurrency(): number;
6
7
  static makeState(concurrency?: number): SharedArrayBuffer;
7
8
  constructor({ concurrency, stateBuffer }?: {
8
9
  concurrency?: number | undefined;
package/lib/index.js CHANGED
@@ -19,7 +19,14 @@ export class Scheduler {
19
19
  #normalQueue = new FixedQueue()
20
20
  #highQueue = new FixedQueue()
21
21
 
22
+ get concurrency() {
23
+ return this.#concurrency
24
+ }
25
+
22
26
  static makeState(concurrency = 0) {
27
+ if (concurrency < 0 || !Number.isInteger(concurrency)) {
28
+ throw new Error('Invalid concurrency')
29
+ }
23
30
  const stateBuffer = new SharedArrayBuffer(64)
24
31
  const stateView = new Uint32Array(stateBuffer)
25
32
  Atomics.store(stateView, Scheduler.#CONCURRENCY_INDEX, concurrency ?? 0)
@@ -47,7 +54,7 @@ export class Scheduler {
47
54
  }
48
55
 
49
56
  if (
50
- this.#running > 1 &&
57
+ this.#running < 1 ||
51
58
  Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency
52
59
  ) {
53
60
  Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
@@ -66,14 +73,10 @@ export class Scheduler {
66
73
  }
67
74
 
68
75
  #next = () => {
69
- Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
76
+ let running = Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
70
77
  this.#running -= 1
71
78
 
72
- while (
73
- this.#pending > 0 &&
74
- (this.#running < 1 ||
75
- Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency)
76
- ) {
79
+ while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
77
80
  const counter = Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
78
81
  const fn =
79
82
  ((counter & 63) === 0 && this.#lowQueue.shift()) ??
@@ -86,7 +89,7 @@ export class Scheduler {
86
89
  break
87
90
  }
88
91
 
89
- Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
92
+ running = Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
90
93
  this.#running += 1
91
94
  this.#pending -= 1
92
95
  fn(this.#next)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
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": "a8531a2fd6f9e627ebb81eaaf9ea9e4bb8f85145"
23
+ "gitHead": "5ed8daa54f2d78c2a8be28b306b6144582332093"
24
24
  }