@adviser/cement 0.4.68 → 0.4.70
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/cjs/timeouted.cjs +26 -10
- package/cjs/timeouted.cjs.map +1 -1
- package/cjs/timeouted.d.ts +10 -5
- package/cjs/timeouted.d.ts.map +1 -1
- package/cjs/timeouted.test.cjs +145 -23
- package/cjs/timeouted.test.cjs.map +1 -1
- package/cjs/version.cjs +1 -1
- package/deno.json +1 -1
- package/esm/timeouted.d.ts +10 -5
- package/esm/timeouted.d.ts.map +1 -1
- package/esm/timeouted.js +26 -10
- package/esm/timeouted.js.map +1 -1
- package/esm/timeouted.test.js +112 -23
- package/esm/timeouted.test.js.map +1 -1
- package/esm/version.js +1 -1
- package/package.json +1 -1
- package/src/timeouted.ts +40 -15
- package/ts/cjs/timeouted.d.ts +10 -5
- package/ts/cjs/timeouted.d.ts.map +1 -1
- package/ts/cjs/timeouted.js +26 -10
- package/ts/cjs/timeouted.js.map +1 -1
- package/ts/cjs/timeouted.test.js +145 -23
- package/ts/cjs/timeouted.test.js.map +1 -1
- package/ts/cjs/version.js +1 -1
- package/ts/esm/timeouted.d.ts +10 -5
- package/ts/esm/timeouted.d.ts.map +1 -1
- package/ts/esm/timeouted.js +26 -10
- package/ts/esm/timeouted.js.map +1 -1
- package/ts/esm/timeouted.test.js +112 -23
- package/ts/esm/timeouted.test.js.map +1 -1
- package/ts/esm/version.js +1 -1
package/src/timeouted.ts
CHANGED
|
@@ -2,19 +2,41 @@ import { Future } from "./future.js";
|
|
|
2
2
|
import { isPromise } from "./is-promise.js";
|
|
3
3
|
import { sleep } from "./promise-sleep.js";
|
|
4
4
|
|
|
5
|
-
export interface
|
|
5
|
+
export interface IsTimeouted {
|
|
6
|
+
isSuccess(): this is TimeoutResultSuccess<unknown>;
|
|
7
|
+
isTimeout(): this is TimeoutResultTimeout<unknown>;
|
|
8
|
+
isAborted(): this is TimeoutResultAborted<unknown>;
|
|
9
|
+
isError(): this is TimeoutResultError<unknown>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const IsTimeoutedImpl: IsTimeouted = {
|
|
13
|
+
isSuccess(): this is TimeoutResultSuccess<unknown> {
|
|
14
|
+
return (this as TimeoutResult<unknown>).state === "success";
|
|
15
|
+
},
|
|
16
|
+
isTimeout(): this is TimeoutResultTimeout<unknown> {
|
|
17
|
+
return (this as TimeoutResult<unknown>).state === "timeout";
|
|
18
|
+
},
|
|
19
|
+
isAborted(): this is TimeoutResultAborted<unknown> {
|
|
20
|
+
return (this as TimeoutResult<unknown>).state === "aborted";
|
|
21
|
+
},
|
|
22
|
+
isError(): this is TimeoutResultError<unknown> {
|
|
23
|
+
return (this as TimeoutResult<unknown>).state === "error";
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface PurTimeoutResultSuccess<T> extends IsTimeouted {
|
|
6
28
|
readonly state: "success";
|
|
7
29
|
readonly value: T;
|
|
8
30
|
}
|
|
9
31
|
|
|
10
|
-
export interface PurTimeoutResultTimeout {
|
|
32
|
+
export interface PurTimeoutResultTimeout extends IsTimeouted {
|
|
11
33
|
readonly state: "timeout";
|
|
12
34
|
}
|
|
13
|
-
export interface PurTimeoutResultAborted {
|
|
35
|
+
export interface PurTimeoutResultAborted extends IsTimeouted {
|
|
14
36
|
readonly state: "aborted";
|
|
15
37
|
readonly reason: unknown;
|
|
16
38
|
}
|
|
17
|
-
export interface PurTimeoutResultError {
|
|
39
|
+
export interface PurTimeoutResultError extends IsTimeouted {
|
|
18
40
|
readonly state: "error";
|
|
19
41
|
readonly error: Error;
|
|
20
42
|
}
|
|
@@ -54,7 +76,7 @@ export interface TimeoutActionOptions<CTX> {
|
|
|
54
76
|
onTimeout: () => void;
|
|
55
77
|
onAbort: (reason: unknown) => void;
|
|
56
78
|
onError: (error: Error) => void;
|
|
57
|
-
onAbortAction: (reason: unknown) => void;
|
|
79
|
+
// onAbortAction: (reason: unknown) => void;
|
|
58
80
|
}
|
|
59
81
|
|
|
60
82
|
// ============================================================================
|
|
@@ -184,7 +206,7 @@ export async function timeouted<T, CTX = void>(
|
|
|
184
206
|
action: TimeoutAction<T>,
|
|
185
207
|
options: Partial<TimeoutActionOptions<CTX>> = {},
|
|
186
208
|
): Promise<TimeoutResult<T>> {
|
|
187
|
-
const { timeout = 30000, signal, controller: externalController, ctx, onTimeout, onAbort, onError
|
|
209
|
+
const { timeout = 30000, signal, controller: externalController, ctx, onTimeout, onAbort, onError } = options;
|
|
188
210
|
|
|
189
211
|
const controller = externalController || new AbortController();
|
|
190
212
|
const startTime = Date.now();
|
|
@@ -207,7 +229,7 @@ export async function timeouted<T, CTX = void>(
|
|
|
207
229
|
}
|
|
208
230
|
toRemoveEventListeners.length = 0;
|
|
209
231
|
if (!skipCleanupAction) {
|
|
210
|
-
void onAbortAction?.(signal?.reason);
|
|
232
|
+
// void onAbortAction?.(signal?.reason);
|
|
211
233
|
controller.abort(new Error("Timeouted Abort Action"));
|
|
212
234
|
}
|
|
213
235
|
return {
|
|
@@ -227,13 +249,14 @@ export async function timeouted<T, CTX = void>(
|
|
|
227
249
|
return cleanup({
|
|
228
250
|
state: "error",
|
|
229
251
|
error: error instanceof Error ? error : new Error(error as string),
|
|
252
|
+
...IsTimeoutedImpl,
|
|
230
253
|
});
|
|
231
254
|
}
|
|
232
255
|
}
|
|
233
256
|
|
|
234
257
|
const abortToAwait = new Future<PurTimeoutResultAborted>();
|
|
235
258
|
function onAbortHandler(): void {
|
|
236
|
-
abortToAwait.resolve({ state: "aborted" as const, reason: controller.signal.reason as unknown });
|
|
259
|
+
abortToAwait.resolve({ state: "aborted" as const, reason: controller.signal.reason as unknown, ...IsTimeoutedImpl });
|
|
237
260
|
}
|
|
238
261
|
controller.signal.addEventListener("abort", onAbortHandler);
|
|
239
262
|
toRemoveEventListeners.push(() => {
|
|
@@ -241,7 +264,9 @@ export async function timeouted<T, CTX = void>(
|
|
|
241
264
|
});
|
|
242
265
|
|
|
243
266
|
const toRace: Promise<PurTimeoutResult<T>>[] = [
|
|
244
|
-
toAwait
|
|
267
|
+
toAwait
|
|
268
|
+
.then((value) => ({ state: "success" as const, value, ...IsTimeoutedImpl }))
|
|
269
|
+
.catch((error: Error) => ({ state: "error" as const, error, ...IsTimeoutedImpl })),
|
|
245
270
|
abortToAwait.asPromise(),
|
|
246
271
|
];
|
|
247
272
|
if (timeout > 0) {
|
|
@@ -249,11 +274,11 @@ export async function timeouted<T, CTX = void>(
|
|
|
249
274
|
sleep(timeout, controller.signal).then((r): PurTimeoutResult<T> => {
|
|
250
275
|
switch (true) {
|
|
251
276
|
case r.isOk:
|
|
252
|
-
return { state: "timeout" as const };
|
|
277
|
+
return { state: "timeout" as const, ...IsTimeoutedImpl };
|
|
253
278
|
case r.isErr:
|
|
254
|
-
return { state: "error" as const, error: r.error };
|
|
279
|
+
return { state: "error" as const, error: r.error, ...IsTimeoutedImpl };
|
|
255
280
|
case r.isAborted:
|
|
256
|
-
return { state: "aborted" as const, reason: r.reason };
|
|
281
|
+
return { state: "aborted" as const, reason: r.reason, ...IsTimeoutedImpl };
|
|
257
282
|
}
|
|
258
283
|
throw new Error("Unreachable code in timeoutAction");
|
|
259
284
|
}),
|
|
@@ -263,16 +288,16 @@ export async function timeouted<T, CTX = void>(
|
|
|
263
288
|
const res = await Promise.race(toRace);
|
|
264
289
|
switch (true) {
|
|
265
290
|
case res.state === "success":
|
|
266
|
-
return cleanup(res
|
|
291
|
+
return cleanup(res);
|
|
267
292
|
case res.state === "aborted":
|
|
268
293
|
onAbort?.(res.reason);
|
|
269
|
-
return cleanup(res);
|
|
294
|
+
return cleanup(res, true);
|
|
270
295
|
case res.state === "error":
|
|
271
296
|
onError?.(res.error);
|
|
272
297
|
return cleanup(res);
|
|
273
298
|
case res.state === "timeout":
|
|
274
299
|
onTimeout?.();
|
|
275
|
-
return cleanup(res);
|
|
300
|
+
return cleanup(res, true);
|
|
276
301
|
}
|
|
277
302
|
throw new Error("Unreachable code in timeoutAction");
|
|
278
303
|
}
|
package/ts/cjs/timeouted.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface IsTimeouted {
|
|
2
|
+
isSuccess(): this is TimeoutResultSuccess<unknown>;
|
|
3
|
+
isTimeout(): this is TimeoutResultTimeout<unknown>;
|
|
4
|
+
isAborted(): this is TimeoutResultAborted<unknown>;
|
|
5
|
+
isError(): this is TimeoutResultError<unknown>;
|
|
6
|
+
}
|
|
7
|
+
export interface PurTimeoutResultSuccess<T> extends IsTimeouted {
|
|
2
8
|
readonly state: "success";
|
|
3
9
|
readonly value: T;
|
|
4
10
|
}
|
|
5
|
-
export interface PurTimeoutResultTimeout {
|
|
11
|
+
export interface PurTimeoutResultTimeout extends IsTimeouted {
|
|
6
12
|
readonly state: "timeout";
|
|
7
13
|
}
|
|
8
|
-
export interface PurTimeoutResultAborted {
|
|
14
|
+
export interface PurTimeoutResultAborted extends IsTimeouted {
|
|
9
15
|
readonly state: "aborted";
|
|
10
16
|
readonly reason: unknown;
|
|
11
17
|
}
|
|
12
|
-
export interface PurTimeoutResultError {
|
|
18
|
+
export interface PurTimeoutResultError extends IsTimeouted {
|
|
13
19
|
readonly state: "error";
|
|
14
20
|
readonly error: Error;
|
|
15
21
|
}
|
|
@@ -33,7 +39,6 @@ export interface TimeoutActionOptions<CTX> {
|
|
|
33
39
|
onTimeout: () => void;
|
|
34
40
|
onAbort: (reason: unknown) => void;
|
|
35
41
|
onError: (error: Error) => void;
|
|
36
|
-
onAbortAction: (reason: unknown) => void;
|
|
37
42
|
}
|
|
38
43
|
export declare function isSuccess<T>(result: TimeoutResult<T>): result is TimeoutResultSuccess<T>;
|
|
39
44
|
export declare function isTimeout<T>(result: TimeoutResult<T>): result is TimeoutResultTimeout<T>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeouted.d.ts","sourceRoot":"","sources":["../../../src/timeouted.ts"],"names":[],"mappings":"AAIA,M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F;AASD,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,oBAAoB,CAAC,CAAC,CAAC,CAExF;AASD,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,oBAAoB,CAAC,CAAC,CAAC,CAExF;AASD,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,kBAAkB,CAAC,CAAC,CAAC,CAEpF;AAUD,wBAAgB,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAKrD;AAUD,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,CAExE;AAkDD,wBAAsB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,IAAI,EAC3C,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,EACxB,OAAO,GAAE,OAAO,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAM,GAC/C,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CA+F3B"}
|
package/ts/cjs/timeouted.js
CHANGED
|
@@ -10,6 +10,20 @@ exports.timeouted = timeouted;
|
|
|
10
10
|
const future_js_1 = require("./future.js");
|
|
11
11
|
const is_promise_js_1 = require("./is-promise.js");
|
|
12
12
|
const promise_sleep_js_1 = require("./promise-sleep.js");
|
|
13
|
+
const IsTimeoutedImpl = {
|
|
14
|
+
isSuccess() {
|
|
15
|
+
return this.state === "success";
|
|
16
|
+
},
|
|
17
|
+
isTimeout() {
|
|
18
|
+
return this.state === "timeout";
|
|
19
|
+
},
|
|
20
|
+
isAborted() {
|
|
21
|
+
return this.state === "aborted";
|
|
22
|
+
},
|
|
23
|
+
isError() {
|
|
24
|
+
return this.state === "error";
|
|
25
|
+
},
|
|
26
|
+
};
|
|
13
27
|
function isSuccess(result) {
|
|
14
28
|
return result.state === "success";
|
|
15
29
|
}
|
|
@@ -32,7 +46,7 @@ function unwrapOr(result, defaultValue) {
|
|
|
32
46
|
return isSuccess(result) ? result.value : defaultValue;
|
|
33
47
|
}
|
|
34
48
|
async function timeouted(action, options = {}) {
|
|
35
|
-
const { timeout = 30000, signal, controller: externalController, ctx, onTimeout, onAbort, onError
|
|
49
|
+
const { timeout = 30000, signal, controller: externalController, ctx, onTimeout, onAbort, onError } = options;
|
|
36
50
|
const controller = externalController || new AbortController();
|
|
37
51
|
const startTime = Date.now();
|
|
38
52
|
const toRemoveEventListeners = [];
|
|
@@ -51,7 +65,6 @@ async function timeouted(action, options = {}) {
|
|
|
51
65
|
}
|
|
52
66
|
toRemoveEventListeners.length = 0;
|
|
53
67
|
if (!skipCleanupAction) {
|
|
54
|
-
void onAbortAction?.(signal?.reason);
|
|
55
68
|
controller.abort(new Error("Timeouted Abort Action"));
|
|
56
69
|
}
|
|
57
70
|
return {
|
|
@@ -72,30 +85,33 @@ async function timeouted(action, options = {}) {
|
|
|
72
85
|
return cleanup({
|
|
73
86
|
state: "error",
|
|
74
87
|
error: error instanceof Error ? error : new Error(error),
|
|
88
|
+
...IsTimeoutedImpl,
|
|
75
89
|
});
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
92
|
const abortToAwait = new future_js_1.Future();
|
|
79
93
|
function onAbortHandler() {
|
|
80
|
-
abortToAwait.resolve({ state: "aborted", reason: controller.signal.reason });
|
|
94
|
+
abortToAwait.resolve({ state: "aborted", reason: controller.signal.reason, ...IsTimeoutedImpl });
|
|
81
95
|
}
|
|
82
96
|
controller.signal.addEventListener("abort", onAbortHandler);
|
|
83
97
|
toRemoveEventListeners.push(() => {
|
|
84
98
|
controller.signal.removeEventListener("abort", onAbortHandler);
|
|
85
99
|
});
|
|
86
100
|
const toRace = [
|
|
87
|
-
toAwait
|
|
101
|
+
toAwait
|
|
102
|
+
.then((value) => ({ state: "success", value, ...IsTimeoutedImpl }))
|
|
103
|
+
.catch((error) => ({ state: "error", error, ...IsTimeoutedImpl })),
|
|
88
104
|
abortToAwait.asPromise(),
|
|
89
105
|
];
|
|
90
106
|
if (timeout > 0) {
|
|
91
107
|
toRace.push((0, promise_sleep_js_1.sleep)(timeout, controller.signal).then((r) => {
|
|
92
108
|
switch (true) {
|
|
93
109
|
case r.isOk:
|
|
94
|
-
return { state: "timeout" };
|
|
110
|
+
return { state: "timeout", ...IsTimeoutedImpl };
|
|
95
111
|
case r.isErr:
|
|
96
|
-
return { state: "error", error: r.error };
|
|
112
|
+
return { state: "error", error: r.error, ...IsTimeoutedImpl };
|
|
97
113
|
case r.isAborted:
|
|
98
|
-
return { state: "aborted", reason: r.reason };
|
|
114
|
+
return { state: "aborted", reason: r.reason, ...IsTimeoutedImpl };
|
|
99
115
|
}
|
|
100
116
|
throw new Error("Unreachable code in timeoutAction");
|
|
101
117
|
}));
|
|
@@ -103,16 +119,16 @@ async function timeouted(action, options = {}) {
|
|
|
103
119
|
const res = await Promise.race(toRace);
|
|
104
120
|
switch (true) {
|
|
105
121
|
case res.state === "success":
|
|
106
|
-
return cleanup(res
|
|
122
|
+
return cleanup(res);
|
|
107
123
|
case res.state === "aborted":
|
|
108
124
|
onAbort?.(res.reason);
|
|
109
|
-
return cleanup(res);
|
|
125
|
+
return cleanup(res, true);
|
|
110
126
|
case res.state === "error":
|
|
111
127
|
onError?.(res.error);
|
|
112
128
|
return cleanup(res);
|
|
113
129
|
case res.state === "timeout":
|
|
114
130
|
onTimeout?.();
|
|
115
|
-
return cleanup(res);
|
|
131
|
+
return cleanup(res, true);
|
|
116
132
|
}
|
|
117
133
|
throw new Error("Unreachable code in timeoutAction");
|
|
118
134
|
}
|
package/ts/cjs/timeouted.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeouted.js","sourceRoot":"","sources":["../../../src/timeouted.ts"],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CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC9B,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;AAAA,CACtD"}
|
package/ts/cjs/timeouted.test.js
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const promise_sleep_js_1 = require("./promise-sleep.js");
|
|
37
|
+
const runtime_js_1 = require("./runtime.js");
|
|
3
38
|
const timeouted_js_1 = require("./timeouted.js");
|
|
4
39
|
describe("timeoutAction - Success Cases", () => {
|
|
5
40
|
it("should return success for a fast promise", async () => {
|
|
@@ -28,10 +63,10 @@ describe("timeoutAction - Success Cases", () => {
|
|
|
28
63
|
expect(result.ctx).toEqual(ctx);
|
|
29
64
|
});
|
|
30
65
|
it("should not call onAbortAction on success", async () => {
|
|
31
|
-
const
|
|
32
|
-
const result = await (0, timeouted_js_1.timeouted)(Promise.resolve("success"), { timeout: 1000,
|
|
66
|
+
const onAbort = vi.fn();
|
|
67
|
+
const result = await (0, timeouted_js_1.timeouted)(Promise.resolve("success"), { timeout: 1000, onAbort });
|
|
33
68
|
expect((0, timeouted_js_1.isSuccess)(result)).toBe(true);
|
|
34
|
-
expect(
|
|
69
|
+
expect(onAbort).not.toHaveBeenCalled();
|
|
35
70
|
});
|
|
36
71
|
it("should cleanup event listeners on success", async () => {
|
|
37
72
|
const controller = new AbortController();
|
|
@@ -56,19 +91,19 @@ describe("timeoutAction - Timeout Cases", () => {
|
|
|
56
91
|
expect(onTimeout).toHaveBeenCalledTimes(1);
|
|
57
92
|
});
|
|
58
93
|
it("should call onAbortAction on timeout", async () => {
|
|
59
|
-
const
|
|
94
|
+
const onAbort = vi.fn();
|
|
60
95
|
const result = await (0, timeouted_js_1.timeouted)(new Promise((resolve) => setTimeout(() => resolve("slow"), 200)), {
|
|
61
96
|
timeout: 50,
|
|
62
|
-
|
|
97
|
+
onAbort,
|
|
63
98
|
});
|
|
64
99
|
expect((0, timeouted_js_1.isTimeout)(result)).toBe(true);
|
|
65
|
-
expect(
|
|
100
|
+
expect(onAbort).toHaveBeenCalledTimes(0);
|
|
66
101
|
});
|
|
67
102
|
it("should abort the controller on timeout", async () => {
|
|
68
103
|
const controller = new AbortController();
|
|
69
104
|
const result = await (0, timeouted_js_1.timeouted)(new Promise((resolve) => setTimeout(() => resolve("slow"), 200)), { timeout: 50, controller });
|
|
70
105
|
expect((0, timeouted_js_1.isTimeout)(result)).toBe(true);
|
|
71
|
-
expect(controller.signal.aborted).toBe(
|
|
106
|
+
expect(controller.signal.aborted).toBe(false);
|
|
72
107
|
});
|
|
73
108
|
it("should allow action to use abort signal on timeout", async () => {
|
|
74
109
|
let wasAborted = false;
|
|
@@ -82,7 +117,7 @@ describe("timeoutAction - Timeout Cases", () => {
|
|
|
82
117
|
}, { timeout: 50 });
|
|
83
118
|
expect((0, timeouted_js_1.isTimeout)(result)).toBe(true);
|
|
84
119
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
85
|
-
expect(wasAborted).toBe(
|
|
120
|
+
expect(wasAborted).toBe(false);
|
|
86
121
|
});
|
|
87
122
|
});
|
|
88
123
|
describe("timeoutAction - Abort Cases", () => {
|
|
@@ -196,11 +231,11 @@ describe("timeoutAction - Error Cases", () => {
|
|
|
196
231
|
expect(onError).toHaveBeenCalledWith(testError);
|
|
197
232
|
});
|
|
198
233
|
it("should call onAbortAction on error", async () => {
|
|
199
|
-
const onAbortAction = vi.fn();
|
|
200
234
|
const testError = new Error("Test error");
|
|
201
|
-
const
|
|
235
|
+
const onAbort = vi.fn();
|
|
236
|
+
const result = await (0, timeouted_js_1.timeouted)(Promise.reject(testError), { timeout: 1000, onAbort });
|
|
202
237
|
expect((0, timeouted_js_1.isError)(result)).toBe(true);
|
|
203
|
-
expect(
|
|
238
|
+
expect(onAbort).toHaveBeenCalledTimes(0);
|
|
204
239
|
});
|
|
205
240
|
it("should convert non-Error rejection to Error", async () => {
|
|
206
241
|
const result = await (0, timeouted_js_1.timeouted)(Promise.reject(new Error("string error")), { timeout: 1000 });
|
|
@@ -360,25 +395,21 @@ describe("Callback Interactions", () => {
|
|
|
360
395
|
const onTimeout = vi.fn();
|
|
361
396
|
const onAbort = vi.fn();
|
|
362
397
|
const onError = vi.fn();
|
|
363
|
-
const onAbortAction = vi.fn();
|
|
364
398
|
const result = await (0, timeouted_js_1.timeouted)(new Promise((resolve) => setTimeout(() => resolve("slow"), 200)), {
|
|
365
399
|
timeout: 50,
|
|
366
400
|
onTimeout,
|
|
367
401
|
onAbort,
|
|
368
402
|
onError,
|
|
369
|
-
onAbortAction,
|
|
370
403
|
});
|
|
371
404
|
expect((0, timeouted_js_1.isTimeout)(result)).toBe(true);
|
|
372
405
|
expect(onTimeout).toHaveBeenCalledTimes(1);
|
|
373
406
|
expect(onAbort).not.toHaveBeenCalled();
|
|
374
407
|
expect(onError).not.toHaveBeenCalled();
|
|
375
|
-
expect(onAbortAction).toHaveBeenCalledTimes(1);
|
|
376
408
|
});
|
|
377
409
|
it("should call all appropriate callbacks on abort", async () => {
|
|
378
410
|
const onTimeout = vi.fn();
|
|
379
411
|
const onAbort = vi.fn();
|
|
380
412
|
const onError = vi.fn();
|
|
381
|
-
const onAbortAction = vi.fn();
|
|
382
413
|
const controller = new AbortController();
|
|
383
414
|
const promise = (0, timeouted_js_1.timeouted)(new Promise((resolve) => setTimeout(() => resolve("data"), 200)), {
|
|
384
415
|
timeout: 1000,
|
|
@@ -386,7 +417,6 @@ describe("Callback Interactions", () => {
|
|
|
386
417
|
onTimeout,
|
|
387
418
|
onAbort,
|
|
388
419
|
onError,
|
|
389
|
-
onAbortAction,
|
|
390
420
|
});
|
|
391
421
|
setTimeout(() => controller.abort("user action"), 50);
|
|
392
422
|
const result = await promise;
|
|
@@ -395,33 +425,89 @@ describe("Callback Interactions", () => {
|
|
|
395
425
|
expect(onAbort).toHaveBeenCalledTimes(1);
|
|
396
426
|
expect(onAbort).toHaveBeenCalledWith("user action");
|
|
397
427
|
expect(onError).not.toHaveBeenCalled();
|
|
398
|
-
expect(onAbortAction).toHaveBeenCalledTimes(1);
|
|
399
428
|
});
|
|
400
429
|
it("should call all appropriate callbacks on error", async () => {
|
|
401
430
|
const onTimeout = vi.fn();
|
|
402
431
|
const onAbort = vi.fn();
|
|
403
432
|
const onError = vi.fn();
|
|
404
|
-
const onAbortAction = vi.fn();
|
|
405
433
|
const testError = new Error("test");
|
|
406
|
-
const result = await (0, timeouted_js_1.timeouted)(Promise.reject(testError), { timeout: 1000, onTimeout, onAbort, onError
|
|
434
|
+
const result = await (0, timeouted_js_1.timeouted)(Promise.reject(testError), { timeout: 1000, onTimeout, onAbort, onError });
|
|
407
435
|
expect((0, timeouted_js_1.isError)(result)).toBe(true);
|
|
408
436
|
expect(onTimeout).not.toHaveBeenCalled();
|
|
409
437
|
expect(onAbort).not.toHaveBeenCalled();
|
|
410
438
|
expect(onError).toHaveBeenCalledTimes(1);
|
|
411
439
|
expect(onError).toHaveBeenCalledWith(testError);
|
|
412
|
-
expect(onAbortAction).toHaveBeenCalledTimes(1);
|
|
413
440
|
});
|
|
414
441
|
it("should not call any error callbacks on success", async () => {
|
|
415
442
|
const onTimeout = vi.fn();
|
|
416
443
|
const onAbort = vi.fn();
|
|
417
444
|
const onError = vi.fn();
|
|
418
|
-
const
|
|
419
|
-
const result = await (0, timeouted_js_1.timeouted)(Promise.resolve("success"), { timeout: 1000, onTimeout, onAbort, onError, onAbortAction });
|
|
445
|
+
const result = await (0, timeouted_js_1.timeouted)(Promise.resolve("success"), { timeout: 1000, onTimeout, onAbort, onError });
|
|
420
446
|
expect((0, timeouted_js_1.isSuccess)(result)).toBe(true);
|
|
421
447
|
expect(onTimeout).not.toHaveBeenCalled();
|
|
422
448
|
expect(onAbort).not.toHaveBeenCalled();
|
|
423
449
|
expect(onError).not.toHaveBeenCalled();
|
|
424
|
-
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
describe("isTimeouted isTypeGuard Consistency", () => {
|
|
453
|
+
it("isSuccess", async () => {
|
|
454
|
+
const timed = await (0, timeouted_js_1.timeouted)(Promise.resolve("data"), { timeout: 1000 });
|
|
455
|
+
expect((0, timeouted_js_1.isSuccess)(timed)).toBe(true);
|
|
456
|
+
expect(timed.isSuccess()).toBe(true);
|
|
457
|
+
expect((0, timeouted_js_1.isTimeout)(timed)).toBe(false);
|
|
458
|
+
expect(timed.isTimeout()).toBe(false);
|
|
459
|
+
expect((0, timeouted_js_1.isAborted)(timed)).toBe(false);
|
|
460
|
+
expect(timed.isAborted()).toBe(false);
|
|
461
|
+
expect((0, timeouted_js_1.isError)(timed)).toBe(false);
|
|
462
|
+
expect(timed.isError()).toBe(false);
|
|
463
|
+
});
|
|
464
|
+
it("isTimeout", async () => {
|
|
465
|
+
const timed = await (0, timeouted_js_1.timeouted)((0, promise_sleep_js_1.sleep)(10000), { timeout: 10 });
|
|
466
|
+
expect((0, timeouted_js_1.isSuccess)(timed)).toBe(false);
|
|
467
|
+
expect(timed.isSuccess()).toBe(false);
|
|
468
|
+
expect((0, timeouted_js_1.isTimeout)(timed)).toBe(true);
|
|
469
|
+
expect(timed.isTimeout()).toBe(true);
|
|
470
|
+
expect((0, timeouted_js_1.isAborted)(timed)).toBe(false);
|
|
471
|
+
expect(timed.isAborted()).toBe(false);
|
|
472
|
+
expect((0, timeouted_js_1.isError)(timed)).toBe(false);
|
|
473
|
+
expect(timed.isError()).toBe(false);
|
|
474
|
+
});
|
|
475
|
+
it("isAbort controller", async () => {
|
|
476
|
+
const controller = new AbortController();
|
|
477
|
+
setTimeout(() => controller.abort(), 10);
|
|
478
|
+
const timed = await (0, timeouted_js_1.timeouted)((0, promise_sleep_js_1.sleep)(10000), { timeout: 100000, controller: controller });
|
|
479
|
+
expect((0, timeouted_js_1.isSuccess)(timed)).toBe(false);
|
|
480
|
+
expect(timed.isSuccess()).toBe(false);
|
|
481
|
+
expect((0, timeouted_js_1.isTimeout)(timed)).toBe(false);
|
|
482
|
+
expect(timed.isTimeout()).toBe(false);
|
|
483
|
+
expect((0, timeouted_js_1.isAborted)(timed)).toBe(true);
|
|
484
|
+
expect(timed.isAborted()).toBe(true);
|
|
485
|
+
expect((0, timeouted_js_1.isError)(timed)).toBe(false);
|
|
486
|
+
expect(timed.isError()).toBe(false);
|
|
487
|
+
});
|
|
488
|
+
it("isAbort signal", async () => {
|
|
489
|
+
const controller = new AbortController();
|
|
490
|
+
setTimeout(() => controller.abort(), 10);
|
|
491
|
+
const timed = await (0, timeouted_js_1.timeouted)((0, promise_sleep_js_1.sleep)(10000), { timeout: 100000, signal: controller.signal });
|
|
492
|
+
expect((0, timeouted_js_1.isSuccess)(timed)).toBe(false);
|
|
493
|
+
expect(timed.isSuccess()).toBe(false);
|
|
494
|
+
expect((0, timeouted_js_1.isTimeout)(timed)).toBe(false);
|
|
495
|
+
expect(timed.isTimeout()).toBe(false);
|
|
496
|
+
expect((0, timeouted_js_1.isAborted)(timed)).toBe(true);
|
|
497
|
+
expect(timed.isAborted()).toBe(true);
|
|
498
|
+
expect((0, timeouted_js_1.isError)(timed)).toBe(false);
|
|
499
|
+
expect(timed.isError()).toBe(false);
|
|
500
|
+
});
|
|
501
|
+
it("isError", async () => {
|
|
502
|
+
const timed = await (0, timeouted_js_1.timeouted)(Promise.reject(new Error("data")), { timeout: 1000 });
|
|
503
|
+
expect((0, timeouted_js_1.isSuccess)(timed)).toBe(false);
|
|
504
|
+
expect(timed.isSuccess()).toBe(false);
|
|
505
|
+
expect((0, timeouted_js_1.isTimeout)(timed)).toBe(false);
|
|
506
|
+
expect(timed.isTimeout()).toBe(false);
|
|
507
|
+
expect((0, timeouted_js_1.isAborted)(timed)).toBe(false);
|
|
508
|
+
expect(timed.isAborted()).toBe(false);
|
|
509
|
+
expect((0, timeouted_js_1.isError)(timed)).toBe(true);
|
|
510
|
+
expect(timed.isError()).toBe(true);
|
|
425
511
|
});
|
|
426
512
|
});
|
|
427
513
|
describe("Race Conditions", () => {
|
|
@@ -578,4 +664,40 @@ describe("Memory Leak Prevention", () => {
|
|
|
578
664
|
expect(results.every(timeouted_js_1.isSuccess)).toBe(true);
|
|
579
665
|
});
|
|
580
666
|
});
|
|
667
|
+
describe("Node.js Exit Prevention", () => {
|
|
668
|
+
let runtime = undefined;
|
|
669
|
+
if ((0, runtime_js_1.runtimeFn)().isNodeIsh) {
|
|
670
|
+
runtime = ["tsx", "-e"];
|
|
671
|
+
}
|
|
672
|
+
if ((0, runtime_js_1.runtimeFn)().isDeno) {
|
|
673
|
+
runtime = ["deno", "eval"];
|
|
674
|
+
}
|
|
675
|
+
it("not block node from exiting on success", async () => {
|
|
676
|
+
if (runtime) {
|
|
677
|
+
const { $ } = await Promise.resolve().then(() => __importStar(require("zx")));
|
|
678
|
+
const start = Date.now();
|
|
679
|
+
await $ `${runtime} "import { timeouted } from './src/timeouted.ts'; timeouted(Promise.resolve('done'), { timeout: 10000 })"`;
|
|
680
|
+
const duration = Date.now() - start;
|
|
681
|
+
expect(duration).toBeLessThan(2000);
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
it("block node from exiting on error", async () => {
|
|
685
|
+
if (runtime) {
|
|
686
|
+
const { $ } = await Promise.resolve().then(() => __importStar(require("zx")));
|
|
687
|
+
const start = Date.now();
|
|
688
|
+
await $ `${runtime} "import { timeouted } from './src/timeouted.ts'; timeouted(Promise.reject(new Error('fail')), { timeout: 10000 })"`;
|
|
689
|
+
const duration = Date.now() - start;
|
|
690
|
+
expect(duration).toBeLessThan(2000);
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
it("block node from exiting on timeout", async () => {
|
|
694
|
+
if (runtime) {
|
|
695
|
+
const { $ } = await Promise.resolve().then(() => __importStar(require("zx")));
|
|
696
|
+
const start = Date.now();
|
|
697
|
+
await $ `${runtime} "import { timeouted } from './src/timeouted.ts'; timeouted(new Promise(() => {}), { timeout: 10 })"`;
|
|
698
|
+
const duration = Date.now() - start;
|
|
699
|
+
expect(duration).toBeLessThan(2000);
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
});
|
|
581
703
|
//# sourceMappingURL=timeouted.test.js.map
|