@bodil/core 0.3.0 → 0.4.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.
Files changed (49) hide show
  1. package/dist/async/abortable-promise.d.ts +71 -0
  2. package/dist/async/abortable-promise.js +204 -0
  3. package/dist/async/abortable-promise.js.map +1 -0
  4. package/dist/async/abortable-promise.test.d.ts +1 -0
  5. package/dist/async/abortable-promise.test.js +134 -0
  6. package/dist/async/abortable-promise.test.js.map +1 -0
  7. package/dist/async.d.ts +20 -13
  8. package/dist/async.js +45 -21
  9. package/dist/async.js.map +1 -1
  10. package/dist/collections/lfu-cache.d.ts +45 -0
  11. package/dist/collections/lfu-cache.js +107 -0
  12. package/dist/collections/lfu-cache.js.map +1 -0
  13. package/dist/collections/lfu-cache.test.d.ts +1 -0
  14. package/dist/collections/lfu-cache.test.js +29 -0
  15. package/dist/collections/lfu-cache.test.js.map +1 -0
  16. package/dist/collections/linked-list.d.ts +41 -0
  17. package/dist/collections/linked-list.js +126 -0
  18. package/dist/collections/linked-list.js.map +1 -0
  19. package/dist/collections/lru-cache.d.ts +42 -0
  20. package/dist/collections/lru-cache.js +84 -0
  21. package/dist/collections/lru-cache.js.map +1 -0
  22. package/dist/collections/lru-cache.test.d.ts +1 -0
  23. package/dist/collections/lru-cache.test.js +27 -0
  24. package/dist/collections/lru-cache.test.js.map +1 -0
  25. package/dist/collections.d.ts +7 -0
  26. package/dist/collections.js +8 -0
  27. package/dist/collections.js.map +1 -0
  28. package/dist/disposable.d.ts +120 -0
  29. package/dist/disposable.js +219 -0
  30. package/dist/disposable.js.map +1 -0
  31. package/dist/fun.d.ts +6 -0
  32. package/dist/fun.js +5 -0
  33. package/dist/fun.js.map +1 -1
  34. package/dist/index.d.ts +2 -1
  35. package/dist/index.js +2 -1
  36. package/dist/index.js.map +1 -1
  37. package/package.json +27 -11
  38. package/src/async/abortable-promise.test.ts +179 -0
  39. package/src/async/abortable-promise.ts +280 -0
  40. package/src/async.ts +56 -35
  41. package/src/collections/lfu-cache.test.ts +31 -0
  42. package/src/collections/lfu-cache.ts +141 -0
  43. package/src/collections/linked-list.ts +131 -0
  44. package/src/collections/lru-cache.test.ts +29 -0
  45. package/src/collections/lru-cache.ts +109 -0
  46. package/src/collections.ts +8 -0
  47. package/src/disposable.ts +285 -0
  48. package/src/fun.ts +12 -0
  49. package/src/index.ts +2 -1
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Tools for working with {@link Disposable}s and things that should have been
3
+ * {@link Disposable}.
4
+ *
5
+ * @module
6
+ */
7
+ import { assert } from "@bodil/core/assert";
8
+ // @ts-ignore polyfill Symbol.dispose if missing
9
+ Symbol.dispose ??= Symbol.for("Symbol.dispose");
10
+ /**
11
+ * Test whether something implements {@link Disposable}.
12
+ */
13
+ export function isDisposable(value) {
14
+ try {
15
+ return typeof value[Symbol.dispose] === "function";
16
+ }
17
+ catch (_e) {
18
+ return false;
19
+ }
20
+ }
21
+ function extractDisposeFn(value) {
22
+ if (typeof value === "function") {
23
+ return value;
24
+ }
25
+ if (typeof value === "object") {
26
+ if (typeof value[Symbol.dispose] === "function") {
27
+ return value[Symbol.dispose].bind(value);
28
+ }
29
+ if (typeof value.unsubscribe === "function") {
30
+ return value.unsubscribe.bind(value);
31
+ }
32
+ }
33
+ throw new Error("toDisposable(): don't know how to convert given value to disposable");
34
+ }
35
+ /**
36
+ * Construct a new {@link Disposable} out of any {@link Disposifiable}.
37
+ *
38
+ * This constructs a new object which implements {@link Disposable}, and nothing
39
+ * else. The new object's dispose method will dispose of the original
40
+ * {@link Disposifiable}, and ensure that this happens only once, regardless of
41
+ * how many times the dispose method is called.
42
+ */
43
+ export function toDisposable(value) {
44
+ let disposed = false;
45
+ const disposeFn = extractDisposeFn(value);
46
+ return {
47
+ [Symbol.dispose]() {
48
+ if (!disposed) {
49
+ disposed = true;
50
+ disposeFn();
51
+ }
52
+ },
53
+ };
54
+ }
55
+ /**
56
+ * Attach an event listener to an {@link EventTarget} and return a
57
+ * {@link Disposable} which will remove the listener when disposed.
58
+ */
59
+ export function eventListener(target, type, callback, options) {
60
+ target.addEventListener(type, callback, options);
61
+ return toDisposable(() => target.removeEventListener(type, callback));
62
+ }
63
+ /**
64
+ * Call {@link setTimeout} with the provided callback and return a
65
+ * {@link Disposable} which will cancel the timeout when disposed.
66
+ */
67
+ export function timeout(fn, timeout) {
68
+ let id = globalThis.setTimeout(() => {
69
+ id = null;
70
+ disposable[Symbol.dispose]();
71
+ fn();
72
+ }, typeof timeout === "number" ? timeout : timeout.total("milliseconds"));
73
+ const disposable = toDisposable(() => {
74
+ if (id !== null) {
75
+ clearTimeout(id);
76
+ }
77
+ });
78
+ return disposable;
79
+ }
80
+ /**
81
+ * Call {@link setInterval} with the provided callback and return a
82
+ * {@link Disposable} which will cancel the interval when disposed.
83
+ */
84
+ export function interval(fn, interval) {
85
+ const id = globalThis.setInterval(fn, typeof interval === "number" ? interval : interval.total("milliseconds"));
86
+ return toDisposable(() => {
87
+ clearInterval(id);
88
+ });
89
+ }
90
+ /**
91
+ * Call {@link window.requestAnimationFrame} with the provided callback and return a
92
+ * {@link Disposable} which will cancel the request when disposed.
93
+ */
94
+ export function animationFrame(fn) {
95
+ // @ts-ignore requestAnimationFrame is only available in browser contexts
96
+ let id = globalThis.requestAnimationFrame((time) => {
97
+ id = null;
98
+ fn(time);
99
+ disposable[Symbol.dispose]();
100
+ });
101
+ const disposable = toDisposable(() => {
102
+ if (id !== null) {
103
+ // @ts-ignore cancelAnimationFrame is only available in browser contexts
104
+ globalThis.cancelAnimationFrame(id);
105
+ }
106
+ });
107
+ return disposable;
108
+ }
109
+ export function idleCallback(callback, options) {
110
+ let id = globalThis.requestIdleCallback((deadline) => {
111
+ id = null;
112
+ disposable[Symbol.dispose]();
113
+ callback(deadline);
114
+ }, options);
115
+ const disposable = toDisposable(() => {
116
+ if (id !== null) {
117
+ globalThis.cancelIdleCallback(id);
118
+ }
119
+ });
120
+ return disposable;
121
+ }
122
+ /**
123
+ * An implementation of {@link AbortController} which also implements
124
+ * {@link Disposable}.
125
+ *
126
+ * Disposing the controller automatically triggers the abort signal.
127
+ */
128
+ export class DisposableAbortController {
129
+ #controller = new AbortController();
130
+ get signal() {
131
+ return this.#controller.signal;
132
+ }
133
+ abort(reason) {
134
+ this.#controller.abort(reason);
135
+ this.#controller = new AbortController();
136
+ }
137
+ [Symbol.dispose]() {
138
+ this.abort("disposed");
139
+ }
140
+ }
141
+ /**
142
+ * Create an {@link AbortController} which also implements {@link Disposable}.
143
+ *
144
+ * Disposing the controller automatically triggers the abort signal.
145
+ *
146
+ * @returns A {@link DisposableAbortController}.
147
+ */
148
+ export function abortController() {
149
+ return new DisposableAbortController();
150
+ }
151
+ /**
152
+ * A reentrant version of {@link DisposableStack}.
153
+ *
154
+ * This maintains a list of {@link Disposable}s, added with the
155
+ * {@link DisposableContext.use} method, which will all be disposed when you
156
+ * dispose of the {@link DisposableContext} itself.
157
+ *
158
+ * Where this differs from {@link DisposableStack} is that disposing it only
159
+ * resets it after disposing of its dependents, and you can reuse it as many
160
+ * times as you like. This is very handy for managing lifecycle resources, eg. a
161
+ * web component can allocate resources in its `connectedCallback` method and
162
+ * during its connected lifecycle phase which can all be disposed in
163
+ * `disconnectedCallback` with a single call to
164
+ * {@link DisposableContext.dispose}, and it's instantly ready to go again on
165
+ * reconnect.
166
+ */
167
+ export class DisposableContext {
168
+ constructor() {
169
+ this.#disposables = new Set();
170
+ this.dispose = this[Symbol.dispose];
171
+ }
172
+ #disposables;
173
+ [Symbol.dispose]() {
174
+ if (this.#disposables.size === 0) {
175
+ return;
176
+ }
177
+ for (const disposable of this.#disposables) {
178
+ disposable[Symbol.dispose]();
179
+ }
180
+ assert(this.#disposables.size === 0, "DisposableContext: disposables remaining in context after dispose, this isn't supposed to happen");
181
+ }
182
+ use(resource) {
183
+ if (resource === undefined || resource === null) {
184
+ return resource;
185
+ }
186
+ let disposables = this.#disposables;
187
+ const dispose = extractDisposeFn(resource);
188
+ const wrappedDisposable = {
189
+ [Symbol.dispose]() {
190
+ if (disposables?.delete(wrappedDisposable) === true) {
191
+ dispose();
192
+ }
193
+ disposables = undefined;
194
+ },
195
+ };
196
+ disposables.add(wrappedDisposable);
197
+ return wrappedDisposable;
198
+ }
199
+ }
200
+ /**
201
+ * A class mixin which provides a {@link Disposable} implementation.
202
+ *
203
+ * The `use(resource)` method can be used to register {@link Disposifiable}
204
+ * resources which will be disposed when the object's dispose method is called.
205
+ */
206
+ export const AutoDisposable = (superclass) => class extends superclass {
207
+ #_context = new DisposableContext();
208
+ [Symbol.dispose]() {
209
+ if (this.#_context !== undefined) {
210
+ this.#_context.dispose();
211
+ this.#_context = undefined;
212
+ }
213
+ }
214
+ use(resource) {
215
+ assert(this.#_context, "AutoDisposable: use() called after dispose");
216
+ return this.#_context.use(resource);
217
+ }
218
+ };
219
+ //# sourceMappingURL=disposable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"disposable.js","sourceRoot":"","sources":["../src/disposable.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAI5C,gDAAgD;AAChD,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAEhD;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACvC,IAAI,CAAC;QACD,OAAO,OAAQ,KAAoB,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC;IACvE,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACV,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAYD,SAAS,gBAAgB,CAAC,KAAoB;IAC1C,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5B,IAAI,OAAQ,KAAoB,CAAC,MAAM,CAA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4CAA4C,CAAC,CAAC;QACrE,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;CACJ,CAAC"}
package/dist/fun.d.ts CHANGED
@@ -14,3 +14,9 @@ export declare function id<A>(value: A): A;
14
14
  * A function which does nothing and returns `void`.
15
15
  */
16
16
  export declare function nop(): void;
17
+ /**
18
+ * A function which applies the `+` operator to its arguments.
19
+ */
20
+ export declare function sum(a: number, b: number): number;
21
+ export declare function sum(a: bigint, b: bigint): bigint;
22
+ export declare function sum(a: string, b: string): string;
package/dist/fun.js CHANGED
@@ -20,4 +20,9 @@ export function id(value) {
20
20
  export function nop() {
21
21
  // nop
22
22
  }
23
+ export function sum(a, b) {
24
+ // @ts-ignore
25
+ // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
26
+ return a + b;
27
+ }
23
28
  //# sourceMappingURL=fun.js.map
package/dist/fun.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC1B,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG;IACf,MAAM;AACV,CAAC"}
1
+ {"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC1B,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG;IACf,MAAM;AACV,CAAC;AAQD,MAAM,UAAU,GAAG,CAAqC,CAAI,EAAE,CAAI;IAC9D,aAAa;IACb,qEAAqE;IACrE,OAAO,CAAC,GAAG,CAAC,CAAC;AACjB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -4,9 +4,10 @@
4
4
  */
5
5
  import * as assert from "./assert";
6
6
  import * as async from "./async";
7
+ import * as disposable from "./disposable";
7
8
  import * as event from "./event";
8
9
  import * as fun from "./fun";
9
10
  import * as order from "./order";
10
11
  import * as time from "./time";
11
12
  import * as types from "./types";
12
- export { assert, async, event, fun, order, time, types };
13
+ export { assert, async, disposable, event, fun, order, time, types };
package/dist/index.js CHANGED
@@ -4,10 +4,11 @@
4
4
  */
5
5
  import * as assert from "./assert";
6
6
  import * as async from "./async";
7
+ import * as disposable from "./disposable";
7
8
  import * as event from "./event";
8
9
  import * as fun from "./fun";
9
10
  import * as order from "./order";
10
11
  import * as time from "./time";
11
12
  import * as types from "./types";
12
- export { assert, async, event, fun, order, time, types };
13
+ export { assert, async, disposable, event, fun, order, time, types };
13
14
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,GAAG,MAAM,OAAO,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bodil/core",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "My TypeScript standard library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,6 +32,18 @@
32
32
  "import": "./dist/async.js"
33
33
  }
34
34
  },
35
+ "./collections": {
36
+ "import": {
37
+ "types": "./dist/collections.d.ts",
38
+ "import": "./dist/collections.js"
39
+ }
40
+ },
41
+ "./disposable": {
42
+ "import": {
43
+ "types": "./dist/disposable.d.ts",
44
+ "import": "./dist/disposable.js"
45
+ }
46
+ },
35
47
  "./event": {
36
48
  "import": {
37
49
  "types": "./dist/event.d.ts",
@@ -68,28 +80,32 @@
68
80
  },
69
81
  "dependencies": {
70
82
  "@bodil/opt": "^0.4.1",
71
- "temporal-polyfill": "^0.3.0"
83
+ "temporal-polyfill": "^0.3.0",
84
+ "type-fest": "^4.41.0"
72
85
  },
73
86
  "devDependencies": {
74
87
  "@bodil/opt-vitest": "^1.1.0",
75
88
  "@eslint/eslintrc": "^3.3.1",
76
- "@eslint/js": "^9.33.0",
77
- "@typescript-eslint/eslint-plugin": "^8.39.1",
78
- "@typescript-eslint/parser": "^8.39.1",
79
- "eslint": "^9.33.0",
89
+ "@eslint/js": "^9.34.0",
90
+ "@typescript-eslint/eslint-plugin": "^8.41.0",
91
+ "@typescript-eslint/parser": "^8.41.0",
92
+ "eslint": "^9.34.0",
80
93
  "eslint-config-prettier": "^10.1.8",
81
- "eslint-plugin-jsdoc": "^53.0.1",
82
- "globals": "^15.15.0",
83
- "typedoc": "^0.28.10",
94
+ "eslint-plugin-jsdoc": "^54.1.1",
95
+ "globals": "^16.3.0",
96
+ "npm-run-all2": "^8.0.4",
97
+ "typedoc": "^0.28.11",
84
98
  "typedoc-plugin-extras": "^4.0.1",
85
- "typedoc-plugin-mdn-links": "^5.0.8",
99
+ "typedoc-plugin-mdn-links": "^5.0.9",
86
100
  "typescript": "^5.9.2",
87
101
  "vitest": "^3.2.4"
88
102
  },
89
103
  "scripts": {
90
104
  "build": "tsc",
91
105
  "test": "vitest --run",
92
- "lint": "eslint src",
106
+ "check:eslint": "eslint src",
107
+ "check:tsc": "tsc --noEmit",
108
+ "check": "run-p check:tsc check:eslint",
93
109
  "doc": "typedoc",
94
110
  "doc:readthedocs": "typedoc --out $READTHEDOCS_OUTPUT/html",
95
111
  "prepublish": "tsc"
@@ -0,0 +1,179 @@
1
+ import { assert, expect, test } from "vitest";
2
+ import { AbortablePromise, defer } from "../async";
3
+ import { Err, Ok } from "@bodil/opt";
4
+
5
+ test("AbortablePromise works like a normal Promise", async () => {
6
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(5)));
7
+ const r = await p;
8
+ expect(r).toEqual(5);
9
+
10
+ const p2 = AbortablePromise.run<number>((resolve) => defer(() => resolve(5)));
11
+ const r2 = await p2.then((value) => value + 5);
12
+ expect(r2).toEqual(10);
13
+ });
14
+
15
+ test("AbortablePromise actually aborts", async () => {
16
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(undefined)));
17
+ assert.isFalse(p.aborted);
18
+ p.abort("welp");
19
+ assert.isTrue(p.aborted);
20
+ await expect(p).rejects.toThrowError(new DOMException("welp", "AbortError"));
21
+ });
22
+
23
+ test("abort() on descendant aborts the whole chain", async () => {
24
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(undefined)));
25
+ assert.isFalse(p.aborted);
26
+ const p2 = p.then(() => "aborted promise resolved");
27
+ p2.abort("welp");
28
+ assert.isTrue(p.aborted);
29
+ assert.isTrue(p2.aborted);
30
+ await expect(p2).rejects.toThrowError(new DOMException("welp", "AbortError"));
31
+ });
32
+
33
+ test("then() should catch the abort", async () => {
34
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(undefined)));
35
+ assert.isFalse(p.aborted);
36
+ p.abort("welp");
37
+ assert.isTrue(p.aborted);
38
+ const p2 = p.then(
39
+ () => "aborted promise resolved",
40
+ (err) => err
41
+ );
42
+ assert.isTrue(p2.aborted);
43
+ await expect(p2).resolves.toEqual(new DOMException("welp", "AbortError"));
44
+ });
45
+
46
+ test("catch() should catch the abort", async () => {
47
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(undefined)));
48
+ assert.isFalse(p.aborted);
49
+ const p2 = p.catch((err) => err);
50
+ p2.abort("welp");
51
+ assert.isTrue(p.aborted);
52
+ assert.isTrue(p2.aborted);
53
+ await expect(p2).resolves.toEqual(new DOMException("welp", "AbortError"));
54
+ });
55
+
56
+ test("finally() should run after abort", async () => {
57
+ const p = AbortablePromise.run((resolve) => defer(() => resolve(undefined)));
58
+ assert.isFalse(p.aborted);
59
+ let finallyRan = false;
60
+ const p2 = p.finally(() => {
61
+ finallyRan = true;
62
+ });
63
+ p2.abort("welp");
64
+ assert.isTrue(p.aborted);
65
+ assert.isTrue(p2.aborted);
66
+ await expect(p2).rejects.toThrowError(new DOMException("welp", "AbortError"));
67
+ assert.isTrue(finallyRan);
68
+ });
69
+
70
+ test("executor receives signal", async () => {
71
+ let aborted = false;
72
+ const p = AbortablePromise.run((resolve, _reject, abort) => {
73
+ abort.addEventListener(
74
+ "abort",
75
+ () => {
76
+ aborted = true;
77
+ },
78
+ { once: true }
79
+ );
80
+ defer(() => resolve(undefined));
81
+ });
82
+ p.abort("welp");
83
+ await expect(p).rejects.toThrowError(new DOMException("welp", "AbortError"));
84
+ assert.isTrue(aborted);
85
+ });
86
+
87
+ test("result()", async () => {
88
+ const p1 = AbortablePromise.resolve(5);
89
+ await expect(p1.result()).resolves.toEqual(Ok(5));
90
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
91
+ const p2 = AbortablePromise.reject(23);
92
+ await expect(p2.result()).resolves.toEqual(Err(23));
93
+ });
94
+
95
+ test("allAbortable() resolves", async () => {
96
+ const aborted: Array<number> = [];
97
+ const p = AbortablePromise.allAbortable((signal) =>
98
+ [1, 2, 3].map((n) =>
99
+ AbortablePromise.run((resolve, _reject, abort) => {
100
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
101
+ defer(() => resolve(n + 10));
102
+ }, signal)
103
+ )
104
+ );
105
+ await expect(p).resolves.toEqual([11, 12, 13]);
106
+ expect(aborted).toEqual([]);
107
+ });
108
+
109
+ test("allAbortable() aborts", async () => {
110
+ const aborted: Array<number> = [];
111
+ const p = AbortablePromise.allAbortable((signal) =>
112
+ [1, 2, 3].map((n) =>
113
+ AbortablePromise.run((resolve, _reject, abort) => {
114
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
115
+ defer(() => resolve(n + 10));
116
+ }, signal)
117
+ )
118
+ );
119
+ p.abort("welp");
120
+ await expect(p).rejects.toThrowError(new DOMException("welp", "AbortError"));
121
+ expect(aborted).toEqual([1, 2, 3]);
122
+ });
123
+
124
+ test("allAbortable() rejects", async () => {
125
+ const aborted: Array<number> = [];
126
+ const p = AbortablePromise.allAbortable((signal) =>
127
+ [1, 2, 3].map((n) =>
128
+ AbortablePromise.run((resolve, reject, abort) => {
129
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
130
+ defer(() => (n !== 1 ? resolve(n + 10) : reject(new Error("whoops"))));
131
+ }, signal)
132
+ )
133
+ );
134
+ await expect(p).rejects.toThrowError(new Error("whoops"));
135
+ expect(aborted).toEqual([2, 3]);
136
+ });
137
+
138
+ test("raceAbortable() resolves", async () => {
139
+ const aborted: Array<number> = [];
140
+ const p = AbortablePromise.raceAbortable((signal) =>
141
+ [1, 2, 3].map((n) =>
142
+ AbortablePromise.run((resolve, _reject, abort) => {
143
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
144
+ defer(() => resolve(n + 10));
145
+ }, signal)
146
+ )
147
+ );
148
+ await expect(p).resolves.toEqual(11);
149
+ expect(aborted).toEqual([2, 3]);
150
+ });
151
+
152
+ test("raceAbortable() aborts", async () => {
153
+ const aborted: Array<number> = [];
154
+ const p = AbortablePromise.raceAbortable((signal) =>
155
+ [1, 2, 3].map((n) =>
156
+ AbortablePromise.run((resolve, _reject, abort) => {
157
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
158
+ defer(() => resolve(n + 10));
159
+ }, signal)
160
+ )
161
+ );
162
+ p.abort("welp");
163
+ await expect(p).rejects.toThrowError(new DOMException("welp", "AbortError"));
164
+ expect(aborted).toEqual([1, 2, 3]);
165
+ });
166
+
167
+ test("raceAbortable() rejects", async () => {
168
+ const aborted: Array<number> = [];
169
+ const p = AbortablePromise.raceAbortable((signal) =>
170
+ [1, 2, 3].map((n) =>
171
+ AbortablePromise.run((_resolve, reject, abort) => {
172
+ abort.addEventListener("abort", () => aborted.push(n), { once: true });
173
+ defer(() => reject(new Error("whoops")));
174
+ }, signal)
175
+ )
176
+ );
177
+ await expect(p).rejects.toThrowError(new Error("whoops"));
178
+ expect(aborted).toEqual([2, 3]);
179
+ });