@nxtedition/scheduler 3.0.5 → 3.0.8

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.
Files changed (2) hide show
  1. package/lib/index.js +36 -21
  2. package/package.json +4 -3
package/lib/index.js CHANGED
@@ -103,17 +103,20 @@ export class Scheduler {
103
103
  throw new Error('Invalid concurrency')
104
104
  }
105
105
  const stateBuffer = new SharedArrayBuffer(64)
106
- const stateView = new Uint32Array(stateBuffer)
107
- Atomics.store(stateView, CONCURRENCY_INDEX, concurrency ?? 0)
106
+ const stateView = new Int32Array(stateBuffer)
107
+ Atomics.store(stateView, CONCURRENCY_INDEX, concurrency ?? -1)
108
108
  return stateBuffer
109
109
  }
110
110
 
111
111
  constructor(opts ) {
112
112
  if (opts instanceof SharedArrayBuffer) {
113
- this.#stateView = new Uint32Array(opts)
114
- this.#concurrency = Atomics.load(this.#stateView, CONCURRENCY_INDEX) || Infinity
113
+ this.#stateView = new Int32Array(opts)
114
+ this.#concurrency = Atomics.load(this.#stateView, CONCURRENCY_INDEX)
115
+ if (this.#concurrency === -1) {
116
+ this.#concurrency = Infinity
117
+ }
115
118
  } else {
116
- this.#concurrency = opts?.concurrency || Infinity
119
+ this.#concurrency = opts?.concurrency ?? Infinity
117
120
  }
118
121
  }
119
122
 
@@ -153,9 +156,9 @@ export class Scheduler {
153
156
  return
154
157
  }
155
158
 
156
- if (Atomics.add(this.#stateView, RUNNING_INDEX, 1) >= this.#concurrency) {
157
- Atomics.sub(this.#stateView, RUNNING_INDEX, 1)
158
- } else {
159
+ // We use non atomic access here as an optimization and treat the concurrency limit as a soft limit.
160
+ if (this.#stateView[RUNNING_INDEX] < this.#concurrency) {
161
+ Atomics.add(this.#stateView, RUNNING_INDEX, 1)
159
162
  this.#running += 1
160
163
  fn(opaque)
161
164
  return
@@ -174,10 +177,16 @@ export class Scheduler {
174
177
  }
175
178
 
176
179
  release() {
177
- let running = this.#stateView
178
- ? Atomics.sub(this.#stateView, RUNNING_INDEX, 1) - 1
179
- : this.#running - 1
180
- this.#running -= 1
180
+ let running
181
+ if (this.#running > 0) {
182
+ running = this.#stateView
183
+ ? Atomics.sub(this.#stateView, RUNNING_INDEX, 1) - 1
184
+ : this.#running - 1
185
+ this.#running -= 1
186
+ } else {
187
+ // Gracefully handle user error...
188
+ running = this.#stateView ? Atomics.load(this.#stateView, RUNNING_INDEX) : this.#running
189
+ }
181
190
 
182
191
  if (this.#pending === 0 || this.#releasing) {
183
192
  return
@@ -222,33 +231,39 @@ export class Scheduler {
222
231
  throw new Error('Invariant violation: pending > 0 but no tasks in queues')
223
232
  }
224
233
 
225
- const fn = queue.arr[queue.idx++]
226
- const opaque = queue.arr[queue.idx++]
234
+ const fn = queue.arr[queue.idx]
235
+ queue.arr[queue.idx++] = null
236
+ const opaque = queue.arr[queue.idx]
237
+ queue.arr[queue.idx++] = null
227
238
  queue.cnt -= 1
228
239
 
229
240
  if (queue.cnt === 0) {
230
241
  queue.idx = 0
231
242
  queue.arr.length = 0
232
243
  } else if (queue.idx > 1024) {
233
- queue.idx = 0
234
244
  queue.arr.splice(0, queue.idx)
245
+ queue.idx = 0
235
246
  }
236
247
 
237
- running = this.#stateView
238
- ? Atomics.add(this.#stateView, RUNNING_INDEX, 1) + 1
239
- : this.#running + 1
240
-
241
- this.#counter += 1
242
248
  this.#pending -= 1
243
249
  this.#running += 1
250
+
251
+ if (this.#stateView) {
252
+ Atomics.add(this.#stateView, RUNNING_INDEX, 1)
253
+ }
254
+
244
255
  fn(opaque)
256
+
257
+ // Re-read running after fn() in case it synchronously called release()
258
+ running = this.#stateView ? Atomics.load(this.#stateView, RUNNING_INDEX) : this.#running
245
259
  }
246
- this.#releasing = false
247
260
  } catch (err) {
248
261
  // Throwing here is undefined behavior...
249
262
  queueMicrotask(() => {
250
263
  throw new Error('Scheduler task error', { cause: err })
251
264
  })
265
+ } finally {
266
+ this.#releasing = false
252
267
  }
253
268
  }
254
269
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "3.0.5",
3
+ "version": "3.0.8",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -18,7 +18,8 @@
18
18
  "prepublishOnly": "yarn build",
19
19
  "typecheck": "tsc --noEmit",
20
20
  "test": "yarn build && node --test",
21
- "test:ci": "yarn build && node --test"
21
+ "test:ci": "yarn build && node --test",
22
+ "test:coverage": "npx tsx --test --experimental-test-coverage src/index.test.ts"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@types/node": "^25.2.3",
@@ -27,5 +28,5 @@
27
28
  "rimraf": "^6.1.2",
28
29
  "typescript": "^5.9.3"
29
30
  },
30
- "gitHead": "6174bfadecfd6232d4fd5c526fecf17d24528ff3"
31
+ "gitHead": "12c38fec15bd155797cb1edae372a81061715df3"
31
32
  }