@matter/general 0.16.7 → 0.16.8-alpha.0-20260123-dff2cae52
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/cjs/MatterError.d.ts +12 -1
- package/dist/cjs/MatterError.d.ts.map +1 -1
- package/dist/cjs/MatterError.js +26 -1
- package/dist/cjs/MatterError.js.map +1 -1
- package/dist/cjs/environment/SharedEnvironmentServices.d.ts +2 -2
- package/dist/cjs/environment/SharedEnvironmentServices.js +2 -2
- package/dist/cjs/storage/StringifyTools.d.ts +1 -1
- package/dist/cjs/storage/StringifyTools.d.ts.map +1 -1
- package/dist/cjs/storage/StringifyTools.js.map +1 -1
- package/dist/cjs/util/Abort.d.ts +11 -5
- package/dist/cjs/util/Abort.d.ts.map +1 -1
- package/dist/cjs/util/Abort.js +71 -29
- package/dist/cjs/util/Abort.js.map +1 -1
- package/dist/cjs/util/DataReadQueue.d.ts +4 -1
- package/dist/cjs/util/DataReadQueue.d.ts.map +1 -1
- package/dist/cjs/util/DataReadQueue.js +27 -5
- package/dist/cjs/util/DataReadQueue.js.map +1 -1
- package/dist/cjs/util/Semaphore.d.ts.map +1 -1
- package/dist/cjs/util/Semaphore.js +5 -5
- package/dist/cjs/util/Semaphore.js.map +1 -1
- package/dist/esm/MatterError.d.ts +12 -1
- package/dist/esm/MatterError.d.ts.map +1 -1
- package/dist/esm/MatterError.js +26 -1
- package/dist/esm/MatterError.js.map +1 -1
- package/dist/esm/environment/SharedEnvironmentServices.d.ts +2 -2
- package/dist/esm/environment/SharedEnvironmentServices.js +2 -2
- package/dist/esm/storage/StringifyTools.d.ts +1 -1
- package/dist/esm/storage/StringifyTools.d.ts.map +1 -1
- package/dist/esm/storage/StringifyTools.js.map +1 -1
- package/dist/esm/util/Abort.d.ts +11 -5
- package/dist/esm/util/Abort.d.ts.map +1 -1
- package/dist/esm/util/Abort.js +71 -29
- package/dist/esm/util/Abort.js.map +1 -1
- package/dist/esm/util/DataReadQueue.d.ts +4 -1
- package/dist/esm/util/DataReadQueue.d.ts.map +1 -1
- package/dist/esm/util/DataReadQueue.js +28 -6
- package/dist/esm/util/DataReadQueue.js.map +1 -1
- package/dist/esm/util/Semaphore.d.ts.map +1 -1
- package/dist/esm/util/Semaphore.js +6 -6
- package/dist/esm/util/Semaphore.js.map +1 -1
- package/package.json +2 -2
- package/src/MatterError.ts +31 -2
- package/src/environment/SharedEnvironmentServices.ts +2 -2
- package/src/storage/StringifyTools.ts +2 -1
- package/src/util/Abort.ts +94 -32
- package/src/util/DataReadQueue.ts +33 -6
- package/src/util/Semaphore.ts +8 -7
package/src/util/Abort.ts
CHANGED
|
@@ -23,13 +23,15 @@ import { SafePromise } from "./Promises.js";
|
|
|
23
23
|
* Optionally will register for abort with an outer {@link AbortController} and/or add a timeout. You must abort or
|
|
24
24
|
* invoke {@link close} if you use either of these options.
|
|
25
25
|
*/
|
|
26
|
-
export class Abort
|
|
26
|
+
export class Abort
|
|
27
|
+
extends Callable<[reason?: string | Error]>
|
|
28
|
+
implements AbortController, AbortSignal, PromiseLike<Error>
|
|
29
|
+
{
|
|
27
30
|
// The native controller implementation
|
|
28
31
|
#controller: AbortController;
|
|
29
32
|
|
|
30
33
|
// Optional abort chaining
|
|
31
|
-
#
|
|
32
|
-
#listener?: (reason: any) => void;
|
|
34
|
+
#unregisterDependencies?: () => void;
|
|
33
35
|
|
|
34
36
|
// Optional PromiseLike behavior
|
|
35
37
|
#aborted?: Promise<Error>;
|
|
@@ -38,49 +40,106 @@ export class Abort extends Callable<[reason?: Error]> implements AbortController
|
|
|
38
40
|
// Optional timeout
|
|
39
41
|
#timeout?: Timer;
|
|
40
42
|
|
|
41
|
-
constructor({ abort, timeout, handler }: Abort.Options = {}) {
|
|
42
|
-
|
|
43
|
+
constructor({ abort: aborts, timeout, handler, timeoutHandler }: Abort.Options = {}) {
|
|
44
|
+
const abort = (reason?: Error | string) => {
|
|
45
|
+
if (typeof reason === "string") {
|
|
46
|
+
reason = new AbortedError(reason);
|
|
47
|
+
}
|
|
48
|
+
this.abort(reason);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
super(abort);
|
|
43
52
|
|
|
44
53
|
this.#controller = new AbortController();
|
|
45
54
|
|
|
55
|
+
const throwIfAborted = this.#controller.signal.throwIfAborted.bind(this.#controller.signal);
|
|
56
|
+
this.#controller.signal.throwIfAborted = () => {
|
|
57
|
+
try {
|
|
58
|
+
throwIfAborted();
|
|
59
|
+
} catch (e) {
|
|
60
|
+
const error = new AbortedError();
|
|
61
|
+
|
|
62
|
+
// Remove stack lines for this abort logic
|
|
63
|
+
error.stack = error.stack
|
|
64
|
+
?.split("\n")
|
|
65
|
+
.filter(line => !line.match(/\.throwIfAborted/))
|
|
66
|
+
.join("\n");
|
|
67
|
+
|
|
68
|
+
error.cause = e;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
46
73
|
const self = (reason?: any) => {
|
|
47
74
|
this.abort(reason);
|
|
48
75
|
};
|
|
49
76
|
Object.setPrototypeOf(self, Object.getPrototypeOf(this));
|
|
50
77
|
|
|
51
|
-
if (
|
|
52
|
-
|
|
78
|
+
if (aborts && !Array.isArray(aborts)) {
|
|
79
|
+
aborts = [aborts];
|
|
53
80
|
}
|
|
54
81
|
|
|
55
|
-
if (
|
|
56
|
-
const
|
|
57
|
-
|
|
82
|
+
if (aborts?.length) {
|
|
83
|
+
const dependencies = aborts.map(abort => abort && ("signal" in abort ? abort.signal : abort));
|
|
84
|
+
|
|
85
|
+
for (const dependency of dependencies) {
|
|
86
|
+
if (dependency === undefined) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
58
89
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
90
|
+
const listener = () => this.abort(asError(dependency.reason));
|
|
91
|
+
dependency.addEventListener("abort", listener);
|
|
92
|
+
const unregisterPrev = this.#unregisterDependencies;
|
|
93
|
+
this.#unregisterDependencies = () => {
|
|
94
|
+
unregisterPrev?.();
|
|
95
|
+
dependency.removeEventListener("abort", listener);
|
|
96
|
+
};
|
|
62
97
|
}
|
|
63
98
|
}
|
|
64
99
|
|
|
65
|
-
if (timeout) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
100
|
+
if (timeout !== undefined) {
|
|
101
|
+
if (timeoutHandler) {
|
|
102
|
+
const original = timeoutHandler;
|
|
103
|
+
timeoutHandler = () => {
|
|
104
|
+
try {
|
|
105
|
+
original.call(this);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
this.abort(asError(e));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
} else {
|
|
111
|
+
timeoutHandler = () => this.abort(new TimeoutError());
|
|
112
|
+
}
|
|
70
113
|
|
|
71
|
-
|
|
72
|
-
|
|
114
|
+
if (timeout <= 0) {
|
|
115
|
+
timeoutHandler.call(this);
|
|
116
|
+
} else {
|
|
117
|
+
this.#timeout = Time.getPeriodicTimer("subtask timeout", timeout, () => {
|
|
118
|
+
if (this.aborted) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
timeoutHandler!.call(this);
|
|
123
|
+
});
|
|
73
124
|
|
|
74
|
-
|
|
125
|
+
this.#timeout.start();
|
|
126
|
+
}
|
|
75
127
|
}
|
|
76
128
|
|
|
77
129
|
if (handler) {
|
|
78
|
-
|
|
130
|
+
if (this.aborted) {
|
|
131
|
+
handler.call(this, this.reason);
|
|
132
|
+
} else {
|
|
133
|
+
this.addEventListener("abort", () => handler.call(this, this.reason));
|
|
134
|
+
}
|
|
79
135
|
}
|
|
80
136
|
}
|
|
81
137
|
|
|
82
|
-
abort(reason?:
|
|
83
|
-
|
|
138
|
+
abort(reason?: Error | string) {
|
|
139
|
+
if (typeof reason === "string") {
|
|
140
|
+
reason = new AbortedError(reason);
|
|
141
|
+
}
|
|
142
|
+
this.#controller.abort(reason ?? new AbortedError("Operation aborted with no reason given"));
|
|
84
143
|
}
|
|
85
144
|
|
|
86
145
|
get signal() {
|
|
@@ -100,7 +159,7 @@ export class Abort extends Callable<[reason?: Error]> implements AbortController
|
|
|
100
159
|
* Race with throw on abort.
|
|
101
160
|
*/
|
|
102
161
|
async attempt<T>(...promises: Array<T | PromiseLike<T>>) {
|
|
103
|
-
return Abort.attempt(this, ...promises);
|
|
162
|
+
return await Abort.attempt(this, ...promises);
|
|
104
163
|
}
|
|
105
164
|
|
|
106
165
|
/**
|
|
@@ -111,11 +170,7 @@ export class Abort extends Callable<[reason?: Error]> implements AbortController
|
|
|
111
170
|
*/
|
|
112
171
|
close() {
|
|
113
172
|
this.#timeout?.stop();
|
|
114
|
-
|
|
115
|
-
for (const dependent of this.#dependents) {
|
|
116
|
-
dependent.removeEventListener("abort", this.#listener);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
173
|
+
this.#unregisterDependencies?.();
|
|
119
174
|
}
|
|
120
175
|
|
|
121
176
|
[Symbol.dispose]() {
|
|
@@ -208,7 +263,7 @@ export namespace Abort {
|
|
|
208
263
|
*
|
|
209
264
|
* This functions similarly to {@link AbortSignal.any} but has additional protection against memory leaks.
|
|
210
265
|
*/
|
|
211
|
-
abort?: Signal | Signal[];
|
|
266
|
+
abort?: Signal | (Signal | undefined)[];
|
|
212
267
|
|
|
213
268
|
/**
|
|
214
269
|
* An abort timeout.
|
|
@@ -220,7 +275,14 @@ export namespace Abort {
|
|
|
220
275
|
/**
|
|
221
276
|
* Adds a default abort handler.
|
|
222
277
|
*/
|
|
223
|
-
handler?: (reason?: Error) => void;
|
|
278
|
+
handler?: (this: Abort, reason?: Error) => void;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Replaces the default timeout handler.
|
|
282
|
+
*
|
|
283
|
+
* The default implementation aborts with {@link TimeoutError}.
|
|
284
|
+
*/
|
|
285
|
+
timeoutHandler?: (this: Abort) => void;
|
|
224
286
|
}
|
|
225
287
|
|
|
226
288
|
/**
|
|
@@ -8,8 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
import { Duration } from "#time/Duration.js";
|
|
10
10
|
import { Minutes } from "#time/TimeUnit.js";
|
|
11
|
-
import { MatterFlowError } from "../MatterError.js";
|
|
11
|
+
import { AbortedError, MatterFlowError } from "../MatterError.js";
|
|
12
12
|
import { Time, Timer } from "../time/Time.js";
|
|
13
|
+
import { Abort } from "./Abort.js";
|
|
13
14
|
import { asError } from "./Error.js";
|
|
14
15
|
import { createPromise } from "./Promises.js";
|
|
15
16
|
import { EndOfStreamError, NoResponseTimeoutError } from "./Streams.js";
|
|
@@ -19,7 +20,7 @@ export class DataReadQueue<T> {
|
|
|
19
20
|
#pendingRead?: { resolver: (data: T) => void; rejecter: (reason: any) => void; timeoutTimer?: Timer };
|
|
20
21
|
#closed = false;
|
|
21
22
|
|
|
22
|
-
async read(timeout = Minutes.one): Promise<T> {
|
|
23
|
+
async read({ timeout = Minutes.one, abort }: { timeout?: Duration; abort?: AbortSignal } = {}): Promise<T> {
|
|
23
24
|
const { promise, resolver, rejecter } = createPromise<T>();
|
|
24
25
|
if (this.#closed) throw new EndOfStreamError();
|
|
25
26
|
const data = this.#queue.shift();
|
|
@@ -39,22 +40,39 @@ export class DataReadQueue<T> {
|
|
|
39
40
|
).start(),
|
|
40
41
|
};
|
|
41
42
|
|
|
43
|
+
let localAbort: Abort | undefined;
|
|
42
44
|
try {
|
|
45
|
+
if (abort) {
|
|
46
|
+
localAbort = new Abort({
|
|
47
|
+
abort,
|
|
48
|
+
|
|
49
|
+
handler: reason => {
|
|
50
|
+
this.#clearPendingRead();
|
|
51
|
+
rejecter(reason);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
43
56
|
return await promise;
|
|
44
57
|
} catch (e) {
|
|
58
|
+
if (e instanceof AbortedError) {
|
|
59
|
+
throw e;
|
|
60
|
+
}
|
|
61
|
+
|
|
45
62
|
// The stack trace where we created the error is useless (either a timer or close()) so replace here
|
|
46
63
|
const error = asError(e);
|
|
47
64
|
error.stack = new Error().stack;
|
|
48
65
|
throw error;
|
|
66
|
+
} finally {
|
|
67
|
+
localAbort?.close();
|
|
49
68
|
}
|
|
50
69
|
}
|
|
51
70
|
|
|
52
71
|
write(data: T) {
|
|
53
72
|
if (this.#closed) throw new EndOfStreamError();
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.#pendingRead = undefined;
|
|
73
|
+
const pendingRead = this.#pendingRead;
|
|
74
|
+
this.#clearPendingRead();
|
|
75
|
+
if (pendingRead) {
|
|
58
76
|
pendingRead.resolver(data);
|
|
59
77
|
return;
|
|
60
78
|
}
|
|
@@ -72,4 +90,13 @@ export class DataReadQueue<T> {
|
|
|
72
90
|
this.#pendingRead.timeoutTimer?.stop();
|
|
73
91
|
this.#pendingRead.rejecter(new EndOfStreamError());
|
|
74
92
|
}
|
|
93
|
+
|
|
94
|
+
#clearPendingRead() {
|
|
95
|
+
if (this.#pendingRead === undefined) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
this.#pendingRead.timeoutTimer?.stop();
|
|
100
|
+
this.#pendingRead = undefined;
|
|
101
|
+
}
|
|
75
102
|
}
|
package/src/util/Semaphore.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { AbortedError } from "#MatterError.js";
|
|
7
|
+
import { AbortedError, ClosedError } from "#MatterError.js";
|
|
8
8
|
import { Duration } from "#time/Duration.js";
|
|
9
9
|
import { Time, Timer } from "#time/Time.js";
|
|
10
10
|
import { Instant } from "#time/TimeUnit.js";
|
|
@@ -64,7 +64,7 @@ export class Semaphore {
|
|
|
64
64
|
async obtainSlot(abort?: Abort.Signal): Promise<WorkSlot> {
|
|
65
65
|
// Check if closed or already aborted before proceeding
|
|
66
66
|
if (this.#closed) {
|
|
67
|
-
throw new
|
|
67
|
+
throw new ClosedError("Queue is closed");
|
|
68
68
|
}
|
|
69
69
|
if (abort) {
|
|
70
70
|
const signal = "signal" in abort ? abort.signal : abort;
|
|
@@ -110,9 +110,10 @@ export class Semaphore {
|
|
|
110
110
|
this.#queue.length,
|
|
111
111
|
);
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
combinedAbort.throwIfAborted();
|
|
114
|
+
|
|
115
|
+
// Should not get here
|
|
116
|
+
throw new AbortedError("Aborted without reason");
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
return result;
|
|
@@ -201,7 +202,7 @@ export class Semaphore {
|
|
|
201
202
|
clear(): void {
|
|
202
203
|
if (this.#queue.length > 0) {
|
|
203
204
|
// Abort current waiters and create fresh abort for future requests
|
|
204
|
-
this.#abort.abort(new
|
|
205
|
+
this.#abort.abort(new ClosedError("Queue cleared"));
|
|
205
206
|
this.#abort = new Abort();
|
|
206
207
|
}
|
|
207
208
|
this.#queue.length = 0;
|
|
@@ -226,7 +227,7 @@ export class Semaphore {
|
|
|
226
227
|
*/
|
|
227
228
|
close(): void {
|
|
228
229
|
this.#closed = true;
|
|
229
|
-
this.#abort.abort(new
|
|
230
|
+
this.#abort.abort(new ClosedError("Queue is closed"));
|
|
230
231
|
this.clear();
|
|
231
232
|
this.#delayTimer.stop();
|
|
232
233
|
}
|