@webdeveric/utils 0.80.1 → 0.81.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.
- package/dist/cjs/delay.js +31 -5
- package/dist/mjs/delay.js +31 -5
- package/dist/types/delay.d.ts +2 -2
- package/package.json +4 -4
package/dist/cjs/delay.js
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.delay = delay;
|
|
4
|
-
|
|
4
|
+
const isPositiveInteger_js_1 = require("./predicate/isPositiveInteger.js");
|
|
5
|
+
const MAX_DELAY_MS = 2 ** 31 - 1;
|
|
6
|
+
function delay(milliseconds, value, signal) {
|
|
5
7
|
return new Promise((resolve, reject) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
reject(new TypeError(`milliseconds must be a number between 0 and ${maxDelayMs}`));
|
|
8
|
+
if (!(0, isPositiveInteger_js_1.isPositiveInteger)(milliseconds)) {
|
|
9
|
+
reject(new TypeError('milliseconds must be a positive integer'));
|
|
9
10
|
return;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
+
if (milliseconds > MAX_DELAY_MS) {
|
|
13
|
+
reject(new RangeError(`milliseconds must be between 0 and ${MAX_DELAY_MS}`));
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const valueIsAbortSignal = value instanceof AbortSignal;
|
|
17
|
+
const abortSignal = valueIsAbortSignal ? value : signal;
|
|
18
|
+
const returnValue = valueIsAbortSignal ? undefined : value;
|
|
19
|
+
if (abortSignal?.aborted) {
|
|
20
|
+
reject(abortSignal.reason);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
let timer;
|
|
24
|
+
if (abortSignal) {
|
|
25
|
+
const onAbort = () => {
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
reject(abortSignal.reason);
|
|
28
|
+
};
|
|
29
|
+
timer = setTimeout(() => {
|
|
30
|
+
abortSignal.removeEventListener('abort', onAbort);
|
|
31
|
+
resolve(returnValue);
|
|
32
|
+
}, milliseconds);
|
|
33
|
+
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
timer = setTimeout(resolve, milliseconds, returnValue);
|
|
37
|
+
}
|
|
12
38
|
});
|
|
13
39
|
}
|
package/dist/mjs/delay.js
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
import { isPositiveInteger } from './predicate/isPositiveInteger.js';
|
|
2
|
+
const MAX_DELAY_MS = 2 ** 31 - 1;
|
|
3
|
+
export function delay(milliseconds, value, signal) {
|
|
2
4
|
return new Promise((resolve, reject) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
reject(new TypeError(`milliseconds must be a number between 0 and ${maxDelayMs}`));
|
|
5
|
+
if (!isPositiveInteger(milliseconds)) {
|
|
6
|
+
reject(new TypeError('milliseconds must be a positive integer'));
|
|
6
7
|
return;
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
+
if (milliseconds > MAX_DELAY_MS) {
|
|
10
|
+
reject(new RangeError(`milliseconds must be between 0 and ${MAX_DELAY_MS}`));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const valueIsAbortSignal = value instanceof AbortSignal;
|
|
14
|
+
const abortSignal = valueIsAbortSignal ? value : signal;
|
|
15
|
+
const returnValue = valueIsAbortSignal ? undefined : value;
|
|
16
|
+
if (abortSignal?.aborted) {
|
|
17
|
+
reject(abortSignal.reason);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let timer;
|
|
21
|
+
if (abortSignal) {
|
|
22
|
+
const onAbort = () => {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
reject(abortSignal.reason);
|
|
25
|
+
};
|
|
26
|
+
timer = setTimeout(() => {
|
|
27
|
+
abortSignal.removeEventListener('abort', onAbort);
|
|
28
|
+
resolve(returnValue);
|
|
29
|
+
}, milliseconds);
|
|
30
|
+
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
timer = setTimeout(resolve, milliseconds, returnValue);
|
|
34
|
+
}
|
|
9
35
|
});
|
|
10
36
|
}
|
package/dist/types/delay.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function delay(milliseconds: number): Promise<undefined>;
|
|
2
|
-
export declare function delay<T>(milliseconds: number, value: T): Promise<T>;
|
|
1
|
+
export declare function delay(milliseconds: number, signal?: AbortSignal): Promise<undefined>;
|
|
2
|
+
export declare function delay<T>(milliseconds: number, value: T, signal?: AbortSignal): Promise<T>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.81.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"url": "https://github.com/webdeveric/utils/issues"
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
69
|
-
"packageManager": "pnpm@11.
|
|
69
|
+
"packageManager": "pnpm@11.9.0+sha512.bd682d5d03fe525ef7c9fd6780c6884d1e756ac4c9c9fe00c538782824310dcf90e3ddc4f53835f06dfaebd5085e41855e0bcbb3b60de2ac5bbab89e5036f03b",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
72
72
|
"prebuild": "pnpm clean",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"@commitlint/config-conventional": "^21.0.2",
|
|
89
89
|
"@commitlint/types": "^21.0.1",
|
|
90
90
|
"@types/node": "^24.13.2",
|
|
91
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
91
|
+
"@typescript/native-preview": "7.0.0-dev.20260622.1",
|
|
92
92
|
"@vitest/coverage-v8": "^4.1.9",
|
|
93
93
|
"@webdeveric/eslint-config-ts": "^0.12.0",
|
|
94
94
|
"@webdeveric/prettier-config": "^0.4.0",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"eslint-plugin-import": "^2.32.0",
|
|
103
103
|
"husky": "^9.1.7",
|
|
104
104
|
"jsdom": "^29.1.1",
|
|
105
|
-
"lint-staged": "^17.0.
|
|
105
|
+
"lint-staged": "^17.0.8",
|
|
106
106
|
"prettier": "^3.8.4",
|
|
107
107
|
"rimraf": "^6.1.3",
|
|
108
108
|
"semantic-release": "^25.0.5",
|