@bodil/core 0.5.6 → 0.5.8
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/dist/assert.js.map +1 -1
- package/dist/assert.test.js +2 -2
- package/dist/assert.test.js.map +1 -1
- package/dist/async/abortable-promise.d.ts +1 -1
- package/dist/async/abortable-promise.js +1 -1
- package/dist/async/abortable-promise.js.map +1 -1
- package/dist/async/abortable-promise.test.js +9 -9
- package/dist/async/abortable-promise.test.js.map +1 -1
- package/dist/async/emitter.test.js +1 -1
- package/dist/async/emitter.test.js.map +1 -1
- package/dist/collections/lfu-cache.js +1 -1
- package/dist/collections/lfu-cache.js.map +1 -1
- package/dist/collections/lru-cache.js +1 -1
- package/dist/collections/lru-cache.js.map +1 -1
- package/dist/decorators.test.js.map +1 -1
- package/dist/disposable.js +17 -0
- package/dist/disposable.js.map +1 -1
- package/dist/disposable.test.js +19 -4
- package/dist/disposable.test.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/logger.d.ts +19 -0
- package/dist/logger.js +26 -0
- package/dist/logger.js.map +1 -0
- package/dist/order.js.map +1 -1
- package/dist/order.test.js +1 -1
- package/dist/order.test.js.map +1 -1
- package/dist/time.test.js.map +1 -1
- package/package.json +40 -12
- package/src/assert.test.ts +11 -10
- package/src/assert.ts +9 -7
- package/src/async/abortable-promise.test.ts +24 -23
- package/src/async/abortable-promise.ts +17 -17
- package/src/async/emitter.test.ts +3 -2
- package/src/async.ts +2 -1
- package/src/collections/lfu-cache.ts +1 -1
- package/src/collections/linked-list.ts +1 -1
- package/src/collections/lru-cache.ts +2 -2
- package/src/decorators.test.ts +1 -0
- package/src/decorators.ts +1 -1
- package/src/disposable.test.ts +24 -4
- package/src/disposable.ts +20 -1
- package/src/index.ts +2 -1
- package/src/logger.ts +38 -0
- package/src/order.test.ts +2 -1
- package/src/order.ts +1 -1
- package/src/time.test.ts +1 -0
- package/src/time.ts +4 -4
- package/dist/assert.d.ts +0 -93
- package/dist/assert.test.d.ts +0 -1
- package/dist/async/abortable-promise.test.d.ts +0 -1
- package/dist/async/emitter.d.ts +0 -10
- package/dist/async/emitter.js +0 -22
- package/dist/async/emitter.js.map +0 -1
- package/dist/async/emitter.test.d.ts +0 -1
- package/dist/async.d.ts +0 -28
- package/dist/async.js +0 -52
- package/dist/async.js.map +0 -1
- package/dist/collections/lfu-cache.d.ts +0 -45
- package/dist/collections/lfu-cache.test.d.ts +0 -1
- package/dist/collections/linked-list.d.ts +0 -47
- package/dist/collections/lru-cache.d.ts +0 -45
- package/dist/collections/lru-cache.test.d.ts +0 -1
- package/dist/collections.d.ts +0 -7
- package/dist/collections.js +0 -8
- package/dist/collections.js.map +0 -1
- package/dist/decorators.d.ts +0 -12
- package/dist/decorators.test.d.ts +0 -1
- package/dist/disposable.d.ts +0 -115
- package/dist/disposable.test.d.ts +0 -1
- package/dist/fun.d.ts +0 -40
- package/dist/fun.js +0 -40
- package/dist/fun.js.map +0 -1
- package/dist/order.d.ts +0 -55
- package/dist/order.test.d.ts +0 -1
- package/dist/time.d.ts +0 -80
- package/dist/time.test.d.ts +0 -1
- package/dist/types.d.ts +0 -17
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bodil/core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.8",
|
|
4
4
|
"description": "My TypeScript standard library",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -56,6 +56,12 @@
|
|
|
56
56
|
"import": "./dist/fun.js"
|
|
57
57
|
}
|
|
58
58
|
},
|
|
59
|
+
"./logger": {
|
|
60
|
+
"import": {
|
|
61
|
+
"types": "./dist/logger.d.ts",
|
|
62
|
+
"import": "./dist/logger.js"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
59
65
|
"./order": {
|
|
60
66
|
"import": {
|
|
61
67
|
"types": "./dist/order.d.ts",
|
|
@@ -80,29 +86,51 @@
|
|
|
80
86
|
},
|
|
81
87
|
"dependencies": {
|
|
82
88
|
"@bodil/opt": "^1.0.0",
|
|
83
|
-
"type-fest": "^5.
|
|
89
|
+
"type-fest": "^5.8.0"
|
|
84
90
|
},
|
|
85
91
|
"devDependencies": {
|
|
86
92
|
"@bodil/opt-vitest": "^1.1.0",
|
|
87
|
-
"@eslint/eslintrc": "^3.3.
|
|
93
|
+
"@eslint/eslintrc": "^3.3.6",
|
|
88
94
|
"@eslint/js": "^10.0.1",
|
|
89
|
-
"@
|
|
90
|
-
"@typescript-eslint/
|
|
95
|
+
"@ianvs/prettier-plugin-sort-imports": "^4.7.1",
|
|
96
|
+
"@typescript-eslint/eslint-plugin": "^8.68.0",
|
|
97
|
+
"@typescript-eslint/parser": "^8.68.0",
|
|
91
98
|
"@typhonjs-typedoc/ts-lib-docs": "^2024.12.25",
|
|
92
|
-
"eslint": "^10.
|
|
99
|
+
"eslint": "^10.9.1",
|
|
93
100
|
"eslint-config-prettier": "^10.1.8",
|
|
94
|
-
"eslint-plugin-jsdoc": "^63.
|
|
95
|
-
"globals": "^17.
|
|
96
|
-
"npm-run-all2": "^9.0.
|
|
97
|
-
"prettier": "^3.
|
|
101
|
+
"eslint-plugin-jsdoc": "^63.3.3",
|
|
102
|
+
"globals": "^17.11.0",
|
|
103
|
+
"npm-run-all2": "^9.0.3",
|
|
104
|
+
"prettier": "^3.9.6",
|
|
98
105
|
"temporal-polyfill": "^0.3.2",
|
|
99
|
-
"typedoc": "^0.28.
|
|
106
|
+
"typedoc": "^0.28.20",
|
|
100
107
|
"typedoc-plugin-extras": "^4.0.1",
|
|
101
108
|
"typedoc-plugin-mdn-links": "^5.1.1",
|
|
102
109
|
"typedoc-theme-fresh": "^0.2.3",
|
|
103
110
|
"typescript": "^6.0.3",
|
|
104
111
|
"vite-plugin-doctest": "^2.0.0",
|
|
105
|
-
"vitest": "^4.1.
|
|
112
|
+
"vitest": "^4.1.11"
|
|
113
|
+
},
|
|
114
|
+
"prettier": {
|
|
115
|
+
"editorconfig": true,
|
|
116
|
+
"plugins": [
|
|
117
|
+
"@ianvs/prettier-plugin-sort-imports"
|
|
118
|
+
],
|
|
119
|
+
"importOrderParserPlugins": [
|
|
120
|
+
"typescript",
|
|
121
|
+
"decorators"
|
|
122
|
+
],
|
|
123
|
+
"importOrderTypeScriptVersion": "5.0.0",
|
|
124
|
+
"importOrder": [
|
|
125
|
+
"",
|
|
126
|
+
"<BUILTIN_MODULES>",
|
|
127
|
+
"",
|
|
128
|
+
"<THIRD_PARTY_MODULES>",
|
|
129
|
+
"",
|
|
130
|
+
"^[.](?!.*[?]inline$)",
|
|
131
|
+
"",
|
|
132
|
+
"[?]inline$"
|
|
133
|
+
]
|
|
106
134
|
},
|
|
107
135
|
"scripts": {
|
|
108
136
|
"build": "tsc",
|
package/src/assert.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "temporal-polyfill/global";
|
|
2
2
|
|
|
3
3
|
import { expect, test } from "vitest";
|
|
4
|
+
|
|
4
5
|
import { time } from ".";
|
|
5
6
|
import { isDeepEqual } from "./assert";
|
|
6
7
|
import type { Equals } from "./types";
|
|
@@ -29,8 +30,8 @@ test("deepEqual", () => {
|
|
|
29
30
|
new Map([
|
|
30
31
|
[1, 2],
|
|
31
32
|
[3, 4],
|
|
32
|
-
])
|
|
33
|
-
)
|
|
33
|
+
]),
|
|
34
|
+
),
|
|
34
35
|
).toEqual(true);
|
|
35
36
|
expect(
|
|
36
37
|
isDeepEqual(
|
|
@@ -41,8 +42,8 @@ test("deepEqual", () => {
|
|
|
41
42
|
new Map([
|
|
42
43
|
[1, 2],
|
|
43
44
|
[3, 4],
|
|
44
|
-
])
|
|
45
|
-
)
|
|
45
|
+
]),
|
|
46
|
+
),
|
|
46
47
|
).toEqual(false);
|
|
47
48
|
|
|
48
49
|
// Uint8Array
|
|
@@ -53,24 +54,24 @@ test("deepEqual", () => {
|
|
|
53
54
|
expect(
|
|
54
55
|
isDeepEqual(
|
|
55
56
|
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
56
|
-
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } }
|
|
57
|
-
)
|
|
57
|
+
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
58
|
+
),
|
|
58
59
|
).toEqual(true);
|
|
59
60
|
expect(
|
|
60
61
|
isDeepEqual(
|
|
61
62
|
{ foo: [1, "wibble", new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
62
|
-
{ foo: [1, new Set([1, 2, 3])], bar: { baz: time.days(1) } }
|
|
63
|
-
)
|
|
63
|
+
{ foo: [1, new Set([1, 2, 3])], bar: { baz: time.days(1) } },
|
|
64
|
+
),
|
|
64
65
|
).toEqual(false);
|
|
65
66
|
|
|
66
67
|
// Insistent equals impl
|
|
67
68
|
class Yes implements Equals {
|
|
68
|
-
equals(
|
|
69
|
+
equals(_other: unknown): _other is this {
|
|
69
70
|
return true;
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
class No implements Equals {
|
|
73
|
-
equals(
|
|
74
|
+
equals(_other: unknown): _other is this {
|
|
74
75
|
return false;
|
|
75
76
|
}
|
|
76
77
|
}
|
package/src/assert.ts
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
import type { TypedArray } from "type-fest";
|
|
9
9
|
|
|
10
|
+
import type { Equals } from "./types";
|
|
11
|
+
|
|
10
12
|
interface ImportMetaEnv {
|
|
11
13
|
DEV?: boolean;
|
|
12
14
|
}
|
|
@@ -36,7 +38,7 @@ export class AssertionError extends Error {}
|
|
|
36
38
|
*/
|
|
37
39
|
export function assert(
|
|
38
40
|
assertion: unknown,
|
|
39
|
-
message: string = "runtime assertion failed"
|
|
41
|
+
message: string = "runtime assertion failed",
|
|
40
42
|
): asserts assertion {
|
|
41
43
|
if (import.meta.env === undefined || import.meta.env.DEV === true) {
|
|
42
44
|
if (assertion === false) {
|
|
@@ -59,7 +61,7 @@ export function assertAlways(assertion: unknown, message?: string): asserts asse
|
|
|
59
61
|
*/
|
|
60
62
|
export function unreachable(): never {
|
|
61
63
|
throw new Error(
|
|
62
|
-
"Congratulations! You have reached a part of the code previously thought to be unreachable."
|
|
64
|
+
"Congratulations! You have reached a part of the code previously thought to be unreachable.",
|
|
63
65
|
);
|
|
64
66
|
}
|
|
65
67
|
|
|
@@ -168,7 +170,7 @@ export function isDeepEqual(a: unknown, b: unknown): boolean {
|
|
|
168
170
|
if (length !== (b as Array<unknown>).length) {
|
|
169
171
|
return false;
|
|
170
172
|
}
|
|
171
|
-
for (i = length; i-- !== 0;
|
|
173
|
+
for (i = length; i-- !== 0;) {
|
|
172
174
|
if (!isDeepEqual(a[i], (b as Array<unknown>)[i])) {
|
|
173
175
|
return false;
|
|
174
176
|
}
|
|
@@ -210,7 +212,7 @@ export function isDeepEqual(a: unknown, b: unknown): boolean {
|
|
|
210
212
|
if (length !== (b as TypedArray).length) {
|
|
211
213
|
return false;
|
|
212
214
|
}
|
|
213
|
-
for (i = length; i-- !== 0;
|
|
215
|
+
for (i = length; i-- !== 0;) {
|
|
214
216
|
if ((a as TypedArray)[i] !== (b as TypedArray)[i]) {
|
|
215
217
|
return false;
|
|
216
218
|
}
|
|
@@ -242,13 +244,13 @@ export function isDeepEqual(a: unknown, b: unknown): boolean {
|
|
|
242
244
|
return false;
|
|
243
245
|
}
|
|
244
246
|
|
|
245
|
-
for (i = length; i-- !== 0;
|
|
247
|
+
for (i = length; i-- !== 0;) {
|
|
246
248
|
if (!Object.hasOwn(b, keys[i])) {
|
|
247
249
|
return false;
|
|
248
250
|
}
|
|
249
251
|
}
|
|
250
252
|
|
|
251
|
-
for (i = length; i-- !== 0;
|
|
253
|
+
for (i = length; i-- !== 0;) {
|
|
252
254
|
const key = keys[i];
|
|
253
255
|
if (!isDeepEqual((a as any)[key], (b as any)[key])) {
|
|
254
256
|
return false;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { Err, Ok } from "@bodil/opt";
|
|
1
2
|
import { assert, expect, test } from "vitest";
|
|
3
|
+
|
|
2
4
|
import { AbortablePromise, defer } from "../async";
|
|
3
|
-
import { Err, Ok } from "@bodil/opt";
|
|
4
5
|
|
|
5
6
|
test("AbortablePromise works like a normal Promise", async () => {
|
|
6
7
|
const p = AbortablePromise.run((resolve) => defer(() => resolve(5)));
|
|
@@ -17,7 +18,7 @@ test("AbortablePromise actually aborts", async () => {
|
|
|
17
18
|
assert.isFalse(p.aborted);
|
|
18
19
|
p.abort("welp");
|
|
19
20
|
assert.isTrue(p.aborted);
|
|
20
|
-
await expect(p).rejects.
|
|
21
|
+
await expect(p).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
test("abort() on descendant aborts the whole chain", async () => {
|
|
@@ -27,7 +28,7 @@ test("abort() on descendant aborts the whole chain", async () => {
|
|
|
27
28
|
p2.abort("welp");
|
|
28
29
|
assert.isTrue(p.aborted);
|
|
29
30
|
assert.isTrue(p2.aborted);
|
|
30
|
-
await expect(p2).rejects.
|
|
31
|
+
await expect(p2).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
test("then() should catch the abort", async () => {
|
|
@@ -37,7 +38,7 @@ test("then() should catch the abort", async () => {
|
|
|
37
38
|
assert.isTrue(p.aborted);
|
|
38
39
|
const p2 = p.then(
|
|
39
40
|
() => "aborted promise resolved",
|
|
40
|
-
(err) => err
|
|
41
|
+
(err) => err,
|
|
41
42
|
);
|
|
42
43
|
assert.isTrue(p2.aborted);
|
|
43
44
|
await expect(p2).resolves.toEqual(new DOMException("welp", "AbortError"));
|
|
@@ -63,7 +64,7 @@ test("finally() should run after abort", async () => {
|
|
|
63
64
|
p2.abort("welp");
|
|
64
65
|
assert.isTrue(p.aborted);
|
|
65
66
|
assert.isTrue(p2.aborted);
|
|
66
|
-
await expect(p2).rejects.
|
|
67
|
+
await expect(p2).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
67
68
|
assert.isTrue(finallyRan);
|
|
68
69
|
});
|
|
69
70
|
|
|
@@ -75,12 +76,12 @@ test("executor receives signal", async () => {
|
|
|
75
76
|
() => {
|
|
76
77
|
aborted = true;
|
|
77
78
|
},
|
|
78
|
-
{ once: true }
|
|
79
|
+
{ once: true },
|
|
79
80
|
);
|
|
80
81
|
defer(() => resolve(undefined));
|
|
81
82
|
});
|
|
82
83
|
p.abort("welp");
|
|
83
|
-
await expect(p).rejects.
|
|
84
|
+
await expect(p).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
84
85
|
assert.isTrue(aborted);
|
|
85
86
|
});
|
|
86
87
|
|
|
@@ -99,8 +100,8 @@ test("allAbortable() resolves", async () => {
|
|
|
99
100
|
AbortablePromise.run((resolve, _reject, abort) => {
|
|
100
101
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
101
102
|
defer(() => resolve(n + 10));
|
|
102
|
-
}, signal)
|
|
103
|
-
)
|
|
103
|
+
}, signal),
|
|
104
|
+
),
|
|
104
105
|
);
|
|
105
106
|
await expect(p).resolves.toEqual([11, 12, 13]);
|
|
106
107
|
expect(aborted).toEqual([]);
|
|
@@ -113,11 +114,11 @@ test("allAbortable() aborts", async () => {
|
|
|
113
114
|
AbortablePromise.run((resolve, _reject, abort) => {
|
|
114
115
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
115
116
|
defer(() => resolve(n + 10));
|
|
116
|
-
}, signal)
|
|
117
|
-
)
|
|
117
|
+
}, signal),
|
|
118
|
+
),
|
|
118
119
|
);
|
|
119
120
|
p.abort("welp");
|
|
120
|
-
await expect(p).rejects.
|
|
121
|
+
await expect(p).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
121
122
|
expect(aborted).toEqual([1, 2, 3]);
|
|
122
123
|
});
|
|
123
124
|
|
|
@@ -128,10 +129,10 @@ test("allAbortable() rejects", async () => {
|
|
|
128
129
|
AbortablePromise.run((resolve, reject, abort) => {
|
|
129
130
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
130
131
|
defer(() => (n !== 1 ? resolve(n + 10) : reject(new Error("whoops"))));
|
|
131
|
-
}, signal)
|
|
132
|
-
)
|
|
132
|
+
}, signal),
|
|
133
|
+
),
|
|
133
134
|
);
|
|
134
|
-
await expect(p).rejects.
|
|
135
|
+
await expect(p).rejects.toThrow(new Error("whoops"));
|
|
135
136
|
expect(aborted).toEqual([2, 3]);
|
|
136
137
|
});
|
|
137
138
|
|
|
@@ -142,8 +143,8 @@ test("raceAbortable() resolves", async () => {
|
|
|
142
143
|
AbortablePromise.run((resolve, _reject, abort) => {
|
|
143
144
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
144
145
|
defer(() => resolve(n + 10));
|
|
145
|
-
}, signal)
|
|
146
|
-
)
|
|
146
|
+
}, signal),
|
|
147
|
+
),
|
|
147
148
|
);
|
|
148
149
|
await expect(p).resolves.toEqual(11);
|
|
149
150
|
expect(aborted).toEqual([2, 3]);
|
|
@@ -156,11 +157,11 @@ test("raceAbortable() aborts", async () => {
|
|
|
156
157
|
AbortablePromise.run((resolve, _reject, abort) => {
|
|
157
158
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
158
159
|
defer(() => resolve(n + 10));
|
|
159
|
-
}, signal)
|
|
160
|
-
)
|
|
160
|
+
}, signal),
|
|
161
|
+
),
|
|
161
162
|
);
|
|
162
163
|
p.abort("welp");
|
|
163
|
-
await expect(p).rejects.
|
|
164
|
+
await expect(p).rejects.toThrow(new DOMException("welp", "AbortError"));
|
|
164
165
|
expect(aborted).toEqual([1, 2, 3]);
|
|
165
166
|
});
|
|
166
167
|
|
|
@@ -171,9 +172,9 @@ test("raceAbortable() rejects", async () => {
|
|
|
171
172
|
AbortablePromise.run((_resolve, reject, abort) => {
|
|
172
173
|
abort.addEventListener("abort", () => aborted.push(n), { once: true });
|
|
173
174
|
defer(() => reject(new Error("whoops")));
|
|
174
|
-
}, signal)
|
|
175
|
-
)
|
|
175
|
+
}, signal),
|
|
176
|
+
),
|
|
176
177
|
);
|
|
177
|
-
await expect(p).rejects.
|
|
178
|
+
await expect(p).rejects.toThrow(new Error("whoops"));
|
|
178
179
|
expect(aborted).toEqual([2, 3]);
|
|
179
180
|
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import { Err, None, Ok, Some, type Result } from "@bodil/opt";
|
|
2
|
+
|
|
3
3
|
import { present } from "../assert";
|
|
4
4
|
import { isAbortError } from "../async";
|
|
5
5
|
|
|
6
6
|
type Executor<A> = (
|
|
7
7
|
resolve: (value: A | PromiseLike<A>) => void,
|
|
8
|
-
reject: (error?: any) => void
|
|
8
|
+
reject: (error?: any) => void,
|
|
9
9
|
) => void;
|
|
10
10
|
|
|
11
11
|
/** Executor function for an {@link AbortablePromise}. */
|
|
@@ -18,7 +18,7 @@ export type AbortableExecutor<A, E> = (
|
|
|
18
18
|
* The {@link AbortSignal} governing this promise. It should be passed on
|
|
19
19
|
* to any jobs launched by this function.
|
|
20
20
|
*/
|
|
21
|
-
abort: AbortSignal
|
|
21
|
+
abort: AbortSignal,
|
|
22
22
|
) => void;
|
|
23
23
|
|
|
24
24
|
type AbortablePromiseConfig<A, E> = {
|
|
@@ -53,7 +53,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
53
53
|
*/
|
|
54
54
|
static run<A, E = Error>(
|
|
55
55
|
executor: AbortableExecutor<A, E>,
|
|
56
|
-
abort?: AbortSignal
|
|
56
|
+
abort?: AbortSignal,
|
|
57
57
|
): AbortablePromise<A, E> {
|
|
58
58
|
return new AbortablePromise({ executor, abort });
|
|
59
59
|
}
|
|
@@ -66,7 +66,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
66
66
|
*/
|
|
67
67
|
static from<A, E = Error>(
|
|
68
68
|
promise: PromiseLike<A>,
|
|
69
|
-
abort?: AbortSignal
|
|
69
|
+
abort?: AbortSignal,
|
|
70
70
|
): AbortablePromise<A, E> {
|
|
71
71
|
return promise instanceof AbortablePromise
|
|
72
72
|
? promise
|
|
@@ -77,7 +77,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
static override resolve<A, E = Error>(
|
|
80
|
-
value?: A | PromiseLike<A
|
|
80
|
+
value?: A | PromiseLike<A>,
|
|
81
81
|
): AbortablePromise<Awaited<A>, E> {
|
|
82
82
|
return super.resolve(value) as AbortablePromise<Awaited<A>, E>;
|
|
83
83
|
}
|
|
@@ -89,7 +89,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
89
89
|
|
|
90
90
|
static allAbortable<A extends ReadonlyArray<AbortablePromise<any>>>(
|
|
91
91
|
executor: (signal: AbortSignal) => A,
|
|
92
|
-
abort?: AbortSignal
|
|
92
|
+
abort?: AbortSignal,
|
|
93
93
|
): AbortablePromise<{ -readonly [P in keyof A]: Awaited<A[P]> }> {
|
|
94
94
|
return AbortablePromise.run((resolve, reject, abort) => {
|
|
95
95
|
if (abort.aborted) {
|
|
@@ -116,7 +116,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
116
116
|
reject(error);
|
|
117
117
|
running.forEach((job) => job.abort());
|
|
118
118
|
}
|
|
119
|
-
}
|
|
119
|
+
},
|
|
120
120
|
);
|
|
121
121
|
});
|
|
122
122
|
}, abort);
|
|
@@ -124,7 +124,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
124
124
|
|
|
125
125
|
static raceAbortable<A extends AbortablePromise<any>>(
|
|
126
126
|
executor: (abort: AbortSignal) => ReadonlyArray<A>,
|
|
127
|
-
abort?: AbortSignal
|
|
127
|
+
abort?: AbortSignal,
|
|
128
128
|
): AbortablePromise<A extends PromiseLike<infer T> ? T : never> {
|
|
129
129
|
return AbortablePromise.run((resolve, reject, abort) => {
|
|
130
130
|
if (abort.aborted) {
|
|
@@ -159,8 +159,8 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
159
159
|
reject(error);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
-
}
|
|
163
|
-
)
|
|
162
|
+
},
|
|
163
|
+
),
|
|
164
164
|
);
|
|
165
165
|
}, abort);
|
|
166
166
|
}
|
|
@@ -184,7 +184,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
184
184
|
const config = executorOrConfig;
|
|
185
185
|
abortController = new AbortController();
|
|
186
186
|
abortSignal = AbortSignal.any(
|
|
187
|
-
[abortController.signal, config.abort].filter((s) => s !== undefined)
|
|
187
|
+
[abortController.signal, config.abort].filter((s) => s !== undefined),
|
|
188
188
|
);
|
|
189
189
|
executor = (resolve, reject) => {
|
|
190
190
|
const checkAborted = () => {
|
|
@@ -213,7 +213,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
213
213
|
*/
|
|
214
214
|
abort(reason?: string) {
|
|
215
215
|
present(this.#abortController, "cannot abort a non-abortable Promise").abort(
|
|
216
|
-
new DOMException(reason, "AbortError")
|
|
216
|
+
new DOMException(reason, "AbortError"),
|
|
217
217
|
);
|
|
218
218
|
}
|
|
219
219
|
|
|
@@ -223,7 +223,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
223
223
|
get signal(): AbortSignal {
|
|
224
224
|
return present(
|
|
225
225
|
this.#abortSignal,
|
|
226
|
-
"AbortablePromise: signal accessed before initialisation"
|
|
226
|
+
"AbortablePromise: signal accessed before initialisation",
|
|
227
227
|
);
|
|
228
228
|
}
|
|
229
229
|
|
|
@@ -250,7 +250,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
250
250
|
|
|
251
251
|
override then<TResult1 = A, TResult2 = never>(
|
|
252
252
|
onfulfilled?: ((value: A) => TResult1 | PromiseLike<TResult1>) | null,
|
|
253
|
-
onrejected?: ((reason: E) => TResult2 | PromiseLike<TResult2>) | null
|
|
253
|
+
onrejected?: ((reason: E) => TResult2 | PromiseLike<TResult2>) | null,
|
|
254
254
|
): AbortablePromise<TResult1 | TResult2, E> {
|
|
255
255
|
contextParent.unshift(this);
|
|
256
256
|
const result = super.then(onfulfilled, onrejected) as AbortablePromise<
|
|
@@ -262,7 +262,7 @@ export class AbortablePromise<A, E = Error> extends Promise<A> implements Dispos
|
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
override catch<TResult = never>(
|
|
265
|
-
onrejected?: ((reason: E) => TResult | PromiseLike<TResult>) | null
|
|
265
|
+
onrejected?: ((reason: E) => TResult | PromiseLike<TResult>) | null,
|
|
266
266
|
): AbortablePromise<A | TResult, E> {
|
|
267
267
|
contextParent.unshift(this);
|
|
268
268
|
const result = super.catch(onrejected) as AbortablePromise<A | TResult, E>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { expect, test } from "vitest";
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
import { sleep } from "../async";
|
|
4
|
+
import { Emitter } from "./emitter";
|
|
4
5
|
|
|
5
6
|
test("Emitter", async () => {
|
|
6
7
|
const e = new Emitter<number>();
|
|
@@ -42,7 +43,7 @@ test("more Emitter", async () => {
|
|
|
42
43
|
expect(result).deep.equal([1, 2, 3]);
|
|
43
44
|
done.resolve();
|
|
44
45
|
} catch (e) {
|
|
45
|
-
done.reject(e
|
|
46
|
+
done.reject(e);
|
|
46
47
|
}
|
|
47
48
|
}, 1);
|
|
48
49
|
await done.promise;
|
package/src/async.ts
CHANGED
|
@@ -32,6 +32,7 @@ export type SchedulerAPI = {
|
|
|
32
32
|
schedule: (job: () => void) => Disposable;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
// eslint-disable-next-line no-useless-assignment
|
|
35
36
|
export const SchedulerAPI: Record<string, SchedulerAPI> = {
|
|
36
37
|
Microtask: {
|
|
37
38
|
schedule: defer,
|
|
@@ -52,7 +53,7 @@ export function sleep(duration: number | Temporal.Duration): Promise<void> {
|
|
|
52
53
|
return new Promise((resolve) => {
|
|
53
54
|
setTimeout(
|
|
54
55
|
resolve,
|
|
55
|
-
typeof duration === "number" ? duration : duration.total("milliseconds")
|
|
56
|
+
typeof duration === "number" ? duration : duration.total("milliseconds"),
|
|
56
57
|
);
|
|
57
58
|
});
|
|
58
59
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { None, Some, type Option } from "@bodil/opt";
|
|
2
2
|
|
|
3
|
-
import { LinkedList, type ListNode } from "./linked-list";
|
|
4
3
|
import { assert } from "../assert";
|
|
5
4
|
import { isDisposable } from "../disposable";
|
|
5
|
+
import { LinkedList, type ListNode } from "./linked-list";
|
|
6
6
|
|
|
7
7
|
interface LFUNode<K> extends ListNode {
|
|
8
8
|
freq: number;
|
|
@@ -128,7 +128,7 @@ export class LinkedList<Node extends ListNode> implements Iterable<Node> {
|
|
|
128
128
|
* Iterate over the nodes in the list from head to tail.
|
|
129
129
|
*/
|
|
130
130
|
[Symbol.iterator]: () => IterableIterator<Node> = function* (
|
|
131
|
-
this: LinkedList<Node
|
|
131
|
+
this: LinkedList<Node>,
|
|
132
132
|
): Generator<Node> {
|
|
133
133
|
let node = this.head;
|
|
134
134
|
while (node !== undefined) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { None, Some, type Option } from "@bodil/opt";
|
|
2
|
-
import { assert } from "../assert";
|
|
3
2
|
|
|
4
|
-
import {
|
|
3
|
+
import { assert } from "../assert";
|
|
5
4
|
import { isDisposable } from "../disposable";
|
|
5
|
+
import { LinkedList, type ListNode } from "./linked-list";
|
|
6
6
|
|
|
7
7
|
interface LRUNode<K, V> extends ListNode {
|
|
8
8
|
key: K;
|
package/src/decorators.test.ts
CHANGED
package/src/decorators.ts
CHANGED
|
@@ -14,7 +14,7 @@ const cachedValues = new WeakMap<WeakKey, Record<string | symbol, unknown>>();
|
|
|
14
14
|
*/
|
|
15
15
|
export function cache<C extends object, T>(
|
|
16
16
|
getter: (this: C) => T,
|
|
17
|
-
context: ClassGetterDecoratorContext<C, T
|
|
17
|
+
context: ClassGetterDecoratorContext<C, T>,
|
|
18
18
|
): (this: C) => T {
|
|
19
19
|
return function (this: C): T {
|
|
20
20
|
let meta = cachedValues.get(this);
|
package/src/disposable.test.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
+
|
|
3
|
+
import type { Unsubscribable } from "type-fest/globals";
|
|
4
|
+
import { expect, test } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { DisposableContext, isDisposifiable } from "./disposable";
|
|
3
7
|
|
|
4
8
|
test("disposes resources", () => {
|
|
5
9
|
const context = new DisposableContext();
|
|
@@ -30,7 +34,7 @@ test("disposer errors", () => {
|
|
|
30
34
|
context.use(() => {
|
|
31
35
|
throw new TypeError("welp");
|
|
32
36
|
});
|
|
33
|
-
expect(() => context.dispose()).
|
|
37
|
+
expect(() => context.dispose()).toThrow(TypeError);
|
|
34
38
|
|
|
35
39
|
context.use(() => {
|
|
36
40
|
throw new TypeError("welp");
|
|
@@ -38,7 +42,7 @@ test("disposer errors", () => {
|
|
|
38
42
|
context.use(() => {
|
|
39
43
|
throw new TypeError("wolp");
|
|
40
44
|
});
|
|
41
|
-
expect(() => context.dispose()).
|
|
45
|
+
expect(() => context.dispose()).toThrow(SuppressedError);
|
|
42
46
|
});
|
|
43
47
|
|
|
44
48
|
test("using", () => {
|
|
@@ -54,3 +58,19 @@ test("using", () => {
|
|
|
54
58
|
} catch (_e) {}
|
|
55
59
|
expect(called).deep.equals(["welp", "wolp"]);
|
|
56
60
|
});
|
|
61
|
+
|
|
62
|
+
test("isDisposifiable", () => {
|
|
63
|
+
const disposable = {
|
|
64
|
+
[Symbol.dispose]() {},
|
|
65
|
+
} satisfies Disposable;
|
|
66
|
+
const unsubscribable = { unsubscribe() {} } satisfies Unsubscribable;
|
|
67
|
+
|
|
68
|
+
expect(isDisposifiable(disposable)).toBeTruthy();
|
|
69
|
+
expect(isDisposifiable(unsubscribable)).toBeTruthy();
|
|
70
|
+
expect(isDisposifiable(null)).toBeFalsy();
|
|
71
|
+
expect(isDisposifiable("nope")).toBeFalsy();
|
|
72
|
+
expect(isDisposifiable(() => {})).toBeTruthy();
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
74
|
+
expect(isDisposifiable((a: any, b: any, c: any) => {})).toBeTruthy();
|
|
75
|
+
expect(isDisposifiable({})).toBeFalsy();
|
|
76
|
+
});
|
package/src/disposable.ts
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { assert } from "./assert";
|
|
9
8
|
import type { Constructor } from "type-fest";
|
|
10
9
|
import type { Unsubscribable } from "type-fest/globals";
|
|
11
10
|
|
|
11
|
+
import { assert } from "./assert";
|
|
12
|
+
|
|
12
13
|
// @ts-ignore polyfill Symbol.dispose if missing
|
|
13
14
|
Symbol.dispose ??= Symbol.for("Symbol.dispose");
|
|
14
15
|
|
|
@@ -69,6 +70,24 @@ export function toDisposable(value: Disposifiable): Disposable {
|
|
|
69
70
|
};
|
|
70
71
|
}
|
|
71
72
|
|
|
73
|
+
function isUnsubscribable(value: unknown): value is Unsubscribable {
|
|
74
|
+
try {
|
|
75
|
+
return typeof (value as Unsubscribable).unsubscribe === "function";
|
|
76
|
+
} catch (_e) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Test whether something is {@link Disposifiable}.
|
|
83
|
+
*
|
|
84
|
+
* This is true if that something implements either of {@link Disposable} or
|
|
85
|
+
* {@link Unsubscribable}, or if it is a function.
|
|
86
|
+
*/
|
|
87
|
+
export function isDisposifiable(value: unknown): value is Disposifiable {
|
|
88
|
+
return isDisposable(value) || isUnsubscribable(value) || typeof value === "function";
|
|
89
|
+
}
|
|
90
|
+
|
|
72
91
|
/**
|
|
73
92
|
* Call {@link setTimeout} with the provided callback and return a
|
|
74
93
|
* {@link Disposable} which will cancel the timeout when disposed.
|
package/src/index.ts
CHANGED
|
@@ -9,8 +9,9 @@ import * as collections from "./collections";
|
|
|
9
9
|
import * as decorators from "./decorators";
|
|
10
10
|
import * as disposable from "./disposable";
|
|
11
11
|
import * as fun from "./fun";
|
|
12
|
+
import * as logger from "./logger";
|
|
12
13
|
import * as order from "./order";
|
|
13
14
|
import * as time from "./time";
|
|
14
15
|
import * as types from "./types";
|
|
15
16
|
|
|
16
|
-
export { assert, async, collections, decorators, disposable, fun, order, time, types };
|
|
17
|
+
export { assert, async, collections, decorators, disposable, fun, logger, order, time, types };
|