@nxtedition/scheduler 2.0.2 → 2.0.4
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 +2 -1
- package/lib/index.js +32 -8
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare class Scheduler {
|
|
|
19
19
|
constructor(opts: SharedArrayBuffer | {
|
|
20
20
|
concurrency?: number;
|
|
21
21
|
});
|
|
22
|
-
|
|
22
|
+
run<T, U>(fn: (opaque?: U) => Promise<T> | T, priority?: 0 | 1 | 2 | 'low' | 'normal' | 'high', opaque?: U): Promise<T>;
|
|
23
|
+
acquire<U>(fn: (opaque?: U) => unknown, priority?: 0 | 1 | 2 | 'low' | 'normal' | 'high', opaque?: U): void;
|
|
23
24
|
release(): void;
|
|
24
25
|
}
|
package/lib/index.js
CHANGED
|
@@ -67,11 +67,33 @@ export class Scheduler {
|
|
|
67
67
|
this.#highQueue.lim = this.#concurrency
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
fn
|
|
70
|
+
run (
|
|
71
|
+
fn ,
|
|
72
72
|
priority = Scheduler.NORMAL,
|
|
73
|
-
opaque
|
|
74
|
-
)
|
|
73
|
+
opaque ,
|
|
74
|
+
) {
|
|
75
|
+
return new Promise ((resolve, reject) => {
|
|
76
|
+
this.acquire(
|
|
77
|
+
async (o) => {
|
|
78
|
+
try {
|
|
79
|
+
resolve(await fn(o))
|
|
80
|
+
} catch (err) {
|
|
81
|
+
reject(err)
|
|
82
|
+
} finally {
|
|
83
|
+
this.release()
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
priority,
|
|
87
|
+
opaque,
|
|
88
|
+
)
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
acquire (
|
|
93
|
+
fn ,
|
|
94
|
+
priority = Scheduler.NORMAL,
|
|
95
|
+
opaque ,
|
|
96
|
+
) {
|
|
75
97
|
if (typeof priority === 'number' && Number.isInteger(priority) && priority >= 0) {
|
|
76
98
|
// Do nothing
|
|
77
99
|
} else if (priority == null) {
|
|
@@ -103,11 +125,13 @@ export class Scheduler {
|
|
|
103
125
|
if (this.#running < 1 || this.#stateView[RUNNING_INDEX] < queue.lim) {
|
|
104
126
|
Atomics.add(this.#stateView, RUNNING_INDEX, 1)
|
|
105
127
|
this.#running += 1
|
|
106
|
-
|
|
128
|
+
fn(opaque)
|
|
129
|
+
return
|
|
107
130
|
}
|
|
108
131
|
} else if (this.#running < 1 || this.#running < queue.lim) {
|
|
109
132
|
this.#running += 1
|
|
110
|
-
|
|
133
|
+
fn(opaque)
|
|
134
|
+
return
|
|
111
135
|
}
|
|
112
136
|
|
|
113
137
|
queue.arr.push(fn, opaque)
|
|
@@ -145,9 +169,9 @@ export class Scheduler {
|
|
|
145
169
|
break
|
|
146
170
|
}
|
|
147
171
|
|
|
148
|
-
if ((this.#counter & 63) === 0) {
|
|
172
|
+
if ((this.#counter & 63) === 0 && this.#lowQueue.cnt > 0) {
|
|
149
173
|
queue = this.#lowQueue
|
|
150
|
-
} else if ((this.#counter & 15) === 0) {
|
|
174
|
+
} else if ((this.#counter & 15) === 0 && this.#normalQueue.cnt > 0) {
|
|
151
175
|
queue = this.#normalQueue
|
|
152
176
|
}
|
|
153
177
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/scheduler",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
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": "
|
|
23
|
+
"gitHead": "aa8978d5b536477418eb6cc60255d4a36bb9d013"
|
|
24
24
|
}
|