@nxtedition/scheduler 2.0.3 → 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 CHANGED
@@ -19,6 +19,7 @@ export declare class Scheduler {
19
19
  constructor(opts: SharedArrayBuffer | {
20
20
  concurrency?: number;
21
21
  });
22
- acquire(fn: (opaque?: any) => any, priority?: 0 | 1 | 2 | 'low' | 'normal' | 'high', opaque?: any): any;
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
- acquire(
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
- return fn(opaque)
128
+ fn(opaque)
129
+ return
107
130
  }
108
131
  } else if (this.#running < 1 || this.#running < queue.lim) {
109
132
  this.#running += 1
110
- return fn(opaque)
133
+ fn(opaque)
134
+ return
111
135
  }
112
136
 
113
137
  queue.arr.push(fn, opaque)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/scheduler",
3
- "version": "2.0.3",
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": "c9cfc87959862c94ed18d15b6090eff035ed271a"
23
+ "gitHead": "aa8978d5b536477418eb6cc60255d4a36bb9d013"
24
24
  }