@nxtedition/scheduler 1.0.6 → 1.0.7

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 +34 -24
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -15,6 +15,7 @@ export class Scheduler {
15
15
  #running = 0
16
16
  #pending = 0
17
17
  #counter = 0
18
+ #actived = false
18
19
 
19
20
  #lowQueue = new FixedQueue()
20
21
  #normalQueue = new FixedQueue()
@@ -87,32 +88,41 @@ export class Scheduler {
87
88
  }
88
89
 
89
90
  #next = () => {
90
- let running = this.#stateView
91
- ? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
92
- : this.#running - 1
93
- this.#running -= 1
94
-
95
- while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
96
- const counter = this.#stateView
97
- ? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
98
- : this.#counter++
99
- const fn =
100
- ((counter & 63) === 0 && this.#lowQueue.shift()) ??
101
- ((counter & 15) === 0 && this.#normalQueue.shift()) ??
102
- this.#highQueue.shift() ??
103
- this.#normalQueue.shift() ??
104
- this.#lowQueue.shift()
105
-
106
- if (!fn) {
107
- break
91
+ try {
92
+ let running = this.#stateView
93
+ ? Atomics.sub(this.#stateView, Scheduler.#RUNNING_INDEX, 1) - 1
94
+ : this.#running - 1
95
+ this.#running -= 1
96
+
97
+ if (this.#actived) {
98
+ return
108
99
  }
109
100
 
110
- running = this.#stateView
111
- ? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
112
- : this.#running + 1
113
- this.#running += 1
114
- this.#pending -= 1
115
- fn(this.#next)
101
+ this.#actived = true
102
+ while (this.#pending > 0 && (this.#running < 1 || running < this.#concurrency)) {
103
+ const counter = this.#stateView
104
+ ? Atomics.add(this.#stateView, Scheduler.#COUNTER_INDEX, 1)
105
+ : this.#counter++
106
+ const fn =
107
+ ((counter & 63) === 0 && this.#lowQueue.shift()) ??
108
+ ((counter & 15) === 0 && this.#normalQueue.shift()) ??
109
+ this.#highQueue.shift() ??
110
+ this.#normalQueue.shift() ??
111
+ this.#lowQueue.shift()
112
+
113
+ running = this.#stateView
114
+ ? Atomics.add(this.#stateView, Scheduler.#RUNNING_INDEX, 1) + 1
115
+ : this.#running + 1
116
+ this.#running += 1
117
+ this.#pending -= 1
118
+ fn(this.#next)
119
+ }
120
+ this.#actived = false
121
+ } catch (err) {
122
+ // Throwing here is undefined behavior...
123
+ queueMicrotask(() => {
124
+ throw new Error('Scheduler task error', { cause: err })
125
+ })
116
126
  }
117
127
  }
118
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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": "1c0ae17fb7bb015b6f352befd2c629bc5ad0ea64"
23
+ "gitHead": "8a7862ed029e6ec42bbccd925bf638e7b069cc5e"
24
24
  }