@nxtedition/scheduler 1.0.3 → 1.0.5

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
@@ -4,10 +4,9 @@ export declare class Scheduler {
4
4
  static NORMAL: 1;
5
5
  static HIGH: 2;
6
6
  get concurrency(): number;
7
- static makeState(concurrency?: number): SharedArrayBuffer;
8
- constructor({ concurrency, stateBuffer }?: {
9
- concurrency?: number | undefined;
10
- stateBuffer?: SharedArrayBuffer | undefined;
7
+ static makeSharedState(concurrency?: number): SharedArrayBuffer;
8
+ constructor(opts?: SharedArrayBuffer | {
9
+ concurrency?: number;
11
10
  });
12
11
  schedule(fn: (next: Function) => any, priority?: 1): any;
13
12
  }
package/lib/index.js CHANGED
@@ -14,6 +14,7 @@ export class Scheduler {
14
14
 
15
15
  #running = 0
16
16
  #pending = 0
17
+ #counter = 0
17
18
 
18
19
  #lowQueue = new FixedQueue()
19
20
  #normalQueue = new FixedQueue()
@@ -23,7 +24,7 @@ export class Scheduler {
23
24
  return this.#concurrency
24
25
  }
25
26
 
26
- static makeState(concurrency = 0) {
27
+ static makeSharedState(concurrency = 0) {
27
28
  if (concurrency < 0 || !Number.isInteger(concurrency)) {
28
29
  throw new Error('Invalid concurrency')
29
30
  }
@@ -33,11 +34,13 @@ export class Scheduler {
33
34
  return stateBuffer
34
35
  }
35
36
 
36
- constructor({ concurrency = Infinity, stateBuffer = Scheduler.makeState(concurrency) } = {}) {
37
- this.#stateView = new Uint32Array(stateBuffer)
38
- this.#concurrency =
39
- concurrency ??
40
- (Atomics.load(new Uint32Array(stateBuffer), Scheduler.#CONCURRENCY_INDEX) || Infinity)
37
+ constructor(opts = {}) {
38
+ if (opts instanceof SharedArrayBuffer) {
39
+ this.#stateView = new Uint32Array(opts)
40
+ this.#concurrency = Atomics.load(this.#stateView, Scheduler.#CONCURRENCY_INDEX)
41
+ } else {
42
+ this.#concurrency = opts?.concurrency ?? 0
43
+ }
41
44
  }
42
45
 
43
46
  schedule(fn , priority = Scheduler.NORMAL) {
@@ -53,11 +56,14 @@ export class Scheduler {
53
56
  throw new Error('Invalid priority')
54
57
  }
55
58
 
56
- if (
57
- this.#running > 1 &&
58
- Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency
59
- ) {
60
- Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
59
+ const running = this.#stateView
60
+ ? Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX)
61
+ : this.#running
62
+
63
+ if (this.#running < 1 || running < this.#concurrency) {
64
+ if (this.#stateView) {
65
+ Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
66
+ }
61
67
  this.#running += 1
62
68
  return fn(this.#next)
63
69
  }
@@ -73,15 +79,15 @@ export class Scheduler {
73
79
  }
74
80
 
75
81
  #next = () => {
76
- Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
82
+ let running = this.#stateView
83
+ ? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
84
+ : this.#running - 1
77
85
  this.#running -= 1
78
86
 
79
- while (
80
- this.#pending > 0 &&
81
- (this.#running < 1 ||
82
- Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency)
83
- ) {
84
- const counter = Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
87
+ while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
88
+ const counter = this.#stateView
89
+ ? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
90
+ : this.#counter++
85
91
  const fn =
86
92
  ((counter & 63) === 0 && this.#lowQueue.shift()) ??
87
93
  ((counter & 15) === 0 && this.#normalQueue.shift()) ??
@@ -93,7 +99,9 @@ export class Scheduler {
93
99
  break
94
100
  }
95
101
 
96
- Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
102
+ running = this.#stateView
103
+ ? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
104
+ : this.#running + 1
97
105
  this.#running += 1
98
106
  this.#pending -= 1
99
107
  fn(this.#next)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
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": "68ef78988067239d47c2d01df4e54feaedf0afa1"
23
+ "gitHead": "8317f7c58c48ff5a16b3380f3b9f4c87c56e8500"
24
24
  }