utilium 3.11.1 → 3.12.0

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Runs operations in the order they are added, without waiting for them.
3
+ */
4
+ export declare class AsyncQueue extends EventTarget {
5
+ #private;
6
+ settled(): Promise<unknown>;
7
+ get size(): number;
8
+ /** Call `thunk` once every earlier operation has settled */
9
+ defer(thunk: () => Promise<unknown>): void;
10
+ /** Track an operation that has already started */
11
+ add(promise: Promise<unknown>): void;
12
+ throwIfError(): void;
13
+ }
@@ -0,0 +1,45 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ /**
3
+ * Runs operations in the order they are added, without waiting for them.
4
+ */
5
+ export class AsyncQueue extends EventTarget {
6
+ #promise = Promise.resolve();
7
+ settled() {
8
+ return this.#promise;
9
+ }
10
+ /** How many queued operations have not finished */
11
+ #pending = 0;
12
+ get size() {
13
+ return this.#pending;
14
+ }
15
+ #error;
16
+ async #settle(operation) {
17
+ try {
18
+ await operation();
19
+ }
20
+ catch (e) {
21
+ this.#error ??= Error.isError(e) ? e : new Error(String(e));
22
+ this.dispatchEvent(new ErrorEvent('error', { error: e }));
23
+ }
24
+ finally {
25
+ this.#pending--;
26
+ }
27
+ }
28
+ /** Call `thunk` once every earlier operation has settled */
29
+ defer(thunk) {
30
+ this.#pending++;
31
+ this.#promise = this.#promise.finally(() => this.#settle(thunk));
32
+ }
33
+ /** Track an operation that has already started */
34
+ add(promise) {
35
+ this.#pending++;
36
+ this.#promise = Promise.all([this.#promise, this.#settle(() => promise)]);
37
+ }
38
+ throwIfError() {
39
+ if (!this.#error)
40
+ return;
41
+ const error = this.#error;
42
+ this.#error = undefined;
43
+ throw error;
44
+ }
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.11.1",
3
+ "version": "3.12.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -55,7 +55,7 @@
55
55
  "eventemitter3": "^5.0.1"
56
56
  },
57
57
  "engines": {
58
- "node": ">=22.0.0"
58
+ "node": ">=22"
59
59
  },
60
60
  "prettier": {
61
61
  "singleQuote": true,