@nxtedition/scheduler 1.0.1 → 1.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
@@ -3,8 +3,10 @@ export declare class Scheduler {
3
3
  static LOW: 0;
4
4
  static NORMAL: 1;
5
5
  static HIGH: 2;
6
- constructor({ concurrency }?: {
6
+ static makeState(concurrency?: number): SharedArrayBuffer;
7
+ constructor({ concurrency, stateBuffer }?: {
7
8
  concurrency?: number | undefined;
9
+ stateBuffer?: SharedArrayBuffer | undefined;
8
10
  });
9
- schedule(fn: ((next: Function) => any), priority?: 1): any;
11
+ schedule(fn: (next: Function) => any, priority?: 1): any;
10
12
  }
package/lib/index.js CHANGED
@@ -6,18 +6,34 @@ export class Scheduler {
6
6
  static HIGH = 2
7
7
 
8
8
  #concurrency
9
+ #stateView
10
+
11
+ static #RUNNING_INDEX = 0
12
+ static #COUNTER_INDEX = 1
13
+ static #CONCURRENCY_INDEX = 2
14
+
9
15
  #running = 0
10
- #counter = 0
16
+ #pending = 0
11
17
 
12
18
  #lowQueue = new FixedQueue()
13
19
  #normalQueue = new FixedQueue()
14
20
  #highQueue = new FixedQueue()
15
21
 
16
- constructor({ concurrency = Infinity } = {}) {
17
- this.#concurrency = concurrency
22
+ static makeState(concurrency = 0) {
23
+ const stateBuffer = new SharedArrayBuffer(64)
24
+ const stateView = new Uint32Array(stateBuffer)
25
+ Atomics.store(stateView, Scheduler.#CONCURRENCY_INDEX, concurrency ?? 0)
26
+ return stateBuffer
27
+ }
28
+
29
+ constructor({ concurrency = Infinity, stateBuffer = Scheduler.makeState(concurrency) } = {}) {
30
+ this.#stateView = new Uint32Array(stateBuffer)
31
+ this.#concurrency =
32
+ concurrency ??
33
+ (Atomics.load(new Uint32Array(stateBuffer), Scheduler.#CONCURRENCY_INDEX) || Infinity)
18
34
  }
19
35
 
20
- schedule(fn , priority = Scheduler.NORMAL) {
36
+ schedule(fn , priority = Scheduler.NORMAL) {
21
37
  if (typeof fn !== 'function') {
22
38
  throw new TypeError('First argument must be a function')
23
39
  }
@@ -30,8 +46,12 @@ export class Scheduler {
30
46
  throw new Error('Invalid priority')
31
47
  }
32
48
 
33
- if (this.#running < this.#concurrency) {
34
- this.#running++
49
+ if (
50
+ this.#running > 1 &&
51
+ Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency
52
+ ) {
53
+ Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
54
+ this.#running += 1
35
55
  return fn(this.#next)
36
56
  }
37
57
 
@@ -42,21 +62,33 @@ export class Scheduler {
42
62
  } else {
43
63
  this.#normalQueue.push(fn)
44
64
  }
65
+ this.#pending += 1
45
66
  }
46
67
 
47
68
  #next = () => {
48
- this.#counter++
49
- this.#running--
50
-
51
- const fn =
52
- ((this.#counter & 63) === 0 && this.#lowQueue.shift()) ??
53
- ((this.#counter & 15) === 0 && this.#normalQueue.shift()) ??
54
- this.#highQueue.shift() ??
55
- this.#normalQueue.shift() ??
56
- this.#lowQueue.shift()
57
-
58
- if (fn) {
59
- this.#running++
69
+ Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
70
+ this.#running -= 1
71
+
72
+ while (
73
+ this.#pending > 0 &&
74
+ (this.#running < 1 ||
75
+ Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX) < this.#concurrency)
76
+ ) {
77
+ const counter = Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
78
+ const fn =
79
+ ((counter & 63) === 0 && this.#lowQueue.shift()) ??
80
+ ((counter & 15) === 0 && this.#normalQueue.shift()) ??
81
+ this.#highQueue.shift() ??
82
+ this.#normalQueue.shift() ??
83
+ this.#lowQueue.shift()
84
+
85
+ if (!fn) {
86
+ break
87
+ }
88
+
89
+ Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
90
+ this.#running += 1
91
+ this.#pending -= 1
60
92
  fn(this.#next)
61
93
  }
62
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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": "5e9be5511fdd2e10aecbab7cc007bdaf990f728a"
23
+ "gitHead": "a8531a2fd6f9e627ebb81eaaf9ea9e4bb8f85145"
24
24
  }