@nxtedition/scheduler 1.0.4 → 1.0.6

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
- schedule(fn: (next: Function) => any, priority?: 1): any;
11
+ schedule(fn: (next: Function) => any, priority?: 0 | 1 | 2 | "low" | "normal" | "high"): 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,31 +34,44 @@ 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
- schedule(fn , priority = Scheduler.NORMAL) {
46
+ schedule(fn , priority = Scheduler.NORMAL) {
44
47
  if (typeof fn !== 'function') {
45
48
  throw new TypeError('First argument must be a function')
46
49
  }
47
50
 
48
51
  if (priority == null) {
49
52
  priority = Scheduler.NORMAL
50
- }
51
-
52
- if (!Number.isInteger(priority)) {
53
+ } else if (typeof priority === 'string') {
54
+ if (priority === 'low') {
55
+ priority = Scheduler.LOW
56
+ } else if (priority === 'normal') {
57
+ priority = Scheduler.NORMAL
58
+ } else if (priority === 'high') {
59
+ priority = Scheduler.HIGH
60
+ } else {
61
+ throw new Error('Invalid priority')
62
+ }
63
+ } else if (!Number.isInteger(priority)) {
53
64
  throw new Error('Invalid priority')
54
65
  }
55
66
 
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)
67
+ const running = this.#stateView
68
+ ? Atomics.load(this.#stateView, Scheduler.#RUNNING_INDEX)
69
+ : this.#running
70
+
71
+ if (this.#running < 1 || running < this.#concurrency) {
72
+ if (this.#stateView) {
73
+ Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1)
74
+ }
61
75
  this.#running += 1
62
76
  return fn(this.#next)
63
77
  }
@@ -73,11 +87,15 @@ export class Scheduler {
73
87
  }
74
88
 
75
89
  #next = () => {
76
- let running = Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
90
+ let running = this.#stateView
91
+ ? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
92
+ : this.#running - 1
77
93
  this.#running -= 1
78
94
 
79
95
  while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
80
- const counter = Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
96
+ const counter = this.#stateView
97
+ ? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
98
+ : this.#counter++
81
99
  const fn =
82
100
  ((counter & 63) === 0 && this.#lowQueue.shift()) ??
83
101
  ((counter & 15) === 0 && this.#normalQueue.shift()) ??
@@ -89,7 +107,9 @@ export class Scheduler {
89
107
  break
90
108
  }
91
109
 
92
- running = Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
110
+ running = this.#stateView
111
+ ? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
112
+ : this.#running + 1
93
113
  this.#running += 1
94
114
  this.#pending -= 1
95
115
  fn(this.#next)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
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": "5ed8daa54f2d78c2a8be28b306b6144582332093"
23
+ "gitHead": "1c0ae17fb7bb015b6f352befd2c629bc5ad0ea64"
24
24
  }