@nxtedition/scheduler 3.0.5 → 3.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.
Files changed (2) hide show
  1. package/lib/index.js +28 -16
  2. package/package.json +4 -3
package/lib/index.js CHANGED
@@ -103,17 +103,17 @@ 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)
106
+ const stateView = new Int32Array(stateBuffer)
107
107
  Atomics.store(stateView, CONCURRENCY_INDEX, concurrency ?? 0)
108
108
  return stateBuffer
109
109
  }
110
110
 
111
111
  constructor(opts ) {
112
112
  if (opts instanceof SharedArrayBuffer) {
113
- this.#stateView = new Uint32Array(opts)
113
+ this.#stateView = new Int32Array(opts)
114
114
  this.#concurrency = Atomics.load(this.#stateView, CONCURRENCY_INDEX) || Infinity
115
115
  } else {
116
- this.#concurrency = opts?.concurrency || Infinity
116
+ this.#concurrency = opts?.concurrency ?? Infinity
117
117
  }
118
118
  }
119
119
 
@@ -174,10 +174,16 @@ export class Scheduler {
174
174
  }
175
175
 
176
176
  release() {
177
- let running = this.#stateView
178
- ? Atomics.sub(this.#stateView, RUNNING_INDEX, 1) - 1
179
- : this.#running - 1
180
- this.#running -= 1
177
+ let running
178
+ if (this.#running > 0) {
179
+ running = this.#stateView
180
+ ? Atomics.sub(this.#stateView, RUNNING_INDEX, 1) - 1
181
+ : this.#running - 1
182
+ this.#running -= 1
183
+ } else {
184
+ // Gracefully handle user error...
185
+ running = this.#stateView ? Atomics.load(this.#stateView, RUNNING_INDEX) : this.#running
186
+ }
181
187
 
182
188
  if (this.#pending === 0 || this.#releasing) {
183
189
  return
@@ -222,33 +228,39 @@ export class Scheduler {
222
228
  throw new Error('Invariant violation: pending > 0 but no tasks in queues')
223
229
  }
224
230
 
225
- const fn = queue.arr[queue.idx++]
226
- const opaque = queue.arr[queue.idx++]
231
+ const fn = queue.arr[queue.idx]
232
+ queue.arr[queue.idx++] = null
233
+ const opaque = queue.arr[queue.idx]
234
+ queue.arr[queue.idx++] = null
227
235
  queue.cnt -= 1
228
236
 
229
237
  if (queue.cnt === 0) {
230
238
  queue.idx = 0
231
239
  queue.arr.length = 0
232
240
  } else if (queue.idx > 1024) {
233
- queue.idx = 0
234
241
  queue.arr.splice(0, queue.idx)
242
+ queue.idx = 0
235
243
  }
236
244
 
237
- running = this.#stateView
238
- ? Atomics.add(this.#stateView, RUNNING_INDEX, 1) + 1
239
- : this.#running + 1
240
-
241
- this.#counter += 1
242
245
  this.#pending -= 1
243
246
  this.#running += 1
247
+
248
+ if (this.#stateView) {
249
+ Atomics.add(this.#stateView, RUNNING_INDEX, 1)
250
+ }
251
+
244
252
  fn(opaque)
253
+
254
+ // Re-read running after fn() in case it synchronously called release()
255
+ running = this.#stateView ? Atomics.load(this.#stateView, RUNNING_INDEX) : this.#running
245
256
  }
246
- this.#releasing = false
247
257
  } catch (err) {
248
258
  // Throwing here is undefined behavior...
249
259
  queueMicrotask(() => {
250
260
  throw new Error('Scheduler task error', { cause: err })
251
261
  })
262
+ } finally {
263
+ this.#releasing = false
252
264
  }
253
265
  }
254
266
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "3.0.5",
3
+ "version": "3.0.6",
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": "80ace3245dc851f38a288b0c3b167ca0024f0469"
31
32
  }