@alwatr/delay 1.0.1 → 1.0.3
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/CHANGELOG.md +10 -0
- package/README.md +1 -1
- package/dist/main.cjs +155 -2
- package/dist/main.cjs.map +2 -2
- package/dist/main.mjs +131 -2
- package/dist/main.mjs.map +4 -4
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.0.3](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@1.0.2...@alwatr/delay@1.0.3) (2024-10-11)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @alwatr/delay
|
|
9
|
+
|
|
10
|
+
## [1.0.2](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@1.0.1...@alwatr/delay@1.0.2) (2024-10-10)
|
|
11
|
+
|
|
12
|
+
### Dependencies update
|
|
13
|
+
|
|
14
|
+
* bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @dependabot[bot]
|
|
15
|
+
|
|
6
16
|
## [1.0.1](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@1.0.0...@alwatr/delay@1.0.1) (2024-10-08)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @alwatr/delay
|
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ This package is distributed under the [MIT License](https://alimd.mit-license.or
|
|
|
101
101
|
|
|
102
102
|
## Sponsors
|
|
103
103
|
|
|
104
|
-
The following companies, organizations, and individuals support
|
|
104
|
+
The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.
|
|
105
105
|
|
|
106
106
|
[](https://exirstudio.com)
|
|
107
107
|
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
/* @alwatr/delay v1.0.
|
|
2
|
-
"use strict";
|
|
1
|
+
/* @alwatr/delay v1.0.3 */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/main.ts
|
|
22
|
+
var main_exports = {};
|
|
23
|
+
__export(main_exports, {
|
|
24
|
+
delay: () => delay
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(main_exports);
|
|
27
|
+
var import_package_tracer = require("@alwatr/package-tracer");
|
|
28
|
+
var import_parse_duration = require("@alwatr/parse-duration");
|
|
29
|
+
|
|
30
|
+
// src/polyfill.ts
|
|
31
|
+
var import_global_scope = require("@alwatr/global-scope");
|
|
32
|
+
var win = import_global_scope.globalScope;
|
|
33
|
+
var requestAnimationFrameFallback = (callback) => setTimeout(() => callback(Date.now()), 1e3 / 60);
|
|
34
|
+
var requestAnimationFrame = win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback;
|
|
35
|
+
var requestIdleCallbackFallback = (callback, options) => setTimeout(callback, options?.timeout ?? 2e3);
|
|
36
|
+
var requestIdleCallback = win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback;
|
|
37
|
+
|
|
38
|
+
// src/main.ts
|
|
39
|
+
import_package_tracer.packageTracer.add("@alwatr/delay", "1.0.3");
|
|
40
|
+
var delay = {
|
|
41
|
+
/**
|
|
42
|
+
* Delays execution for a specified duration (in milliseconds).
|
|
43
|
+
*
|
|
44
|
+
* @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.
|
|
45
|
+
* @returns A Promise that resolves after the specified duration.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* await delay.by('1m'); // Wait for 1 minute
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
by: (duration) => new Promise((resolve) => setTimeout(resolve, (0, import_parse_duration.parseDuration)(duration))),
|
|
53
|
+
/**
|
|
54
|
+
* Delays execution until the next animation frame.
|
|
55
|
+
*
|
|
56
|
+
* @returns A Promise that resolves with the current timestamp when the next animation frame is fired.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const timestamp = await delay.untilNextAnimationFrame();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
untilNextAnimationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
|
|
64
|
+
/**
|
|
65
|
+
* Delays execution until the browser's idle period or the specified timeout.
|
|
66
|
+
*
|
|
67
|
+
* @param timeout - Optional timeout (in milliseconds) for the idle callback.
|
|
68
|
+
* @returns A Promise that resolves with the IdleDeadline object when the browser is idle or the timeout is reached.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const deadline = await delay.untilIdle();
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
untilIdle: (timeout) => new Promise((resolve) => requestIdleCallback(resolve, timeout === void 0 ? void 0 : {
|
|
76
|
+
timeout: (0, import_parse_duration.parseDuration)(timeout)
|
|
77
|
+
})),
|
|
78
|
+
/**
|
|
79
|
+
* Delays execution until a specific DOM event occurs on an HTMLElement.
|
|
80
|
+
*
|
|
81
|
+
* @param element - The HTMLElement to listen for the event on.
|
|
82
|
+
* @param eventName - The name of the DOM event to wait for.
|
|
83
|
+
* @template T The event map type.
|
|
84
|
+
* @returns A Promise that resolves with the event object when the specified event occurs.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const clickEvent = await delay.untilDomEvent(document.body, 'click');
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
untilDomEvent: (element, eventName) => new Promise(
|
|
92
|
+
(resolve) => element.addEventListener(eventName, resolve, { once: true, passive: true })
|
|
93
|
+
),
|
|
94
|
+
/**
|
|
95
|
+
* Delays execution until a specific event occurs on an object with an `addEventListener` method.
|
|
96
|
+
*
|
|
97
|
+
* @param target - The target object to listen for the event on.
|
|
98
|
+
* @param eventName - The name of the event to wait for.
|
|
99
|
+
* @returns A Promise that resolves with the event object when the specified event occurs.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* const server = http.createServer();
|
|
104
|
+
* const requestEvent = await delay.untilEvent(server, 'request');
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
untilEvent: (target, eventName) => new Promise(
|
|
108
|
+
(resolve) => target.addEventListener(eventName, resolve, { once: true, passive: true })
|
|
109
|
+
),
|
|
110
|
+
/**
|
|
111
|
+
* Yields control to the event loop immediately.
|
|
112
|
+
*
|
|
113
|
+
* Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.
|
|
114
|
+
*
|
|
115
|
+
* @returns A Promise that resolves immediately after yielding control to the event loop.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* await delay.immediate();
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
immediate: () => {
|
|
123
|
+
if (typeof setImmediate !== "function") {
|
|
124
|
+
if (typeof queueMicrotask === "function") {
|
|
125
|
+
return delay.nextMicrotask();
|
|
126
|
+
}
|
|
127
|
+
return delay.by(0);
|
|
128
|
+
}
|
|
129
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
130
|
+
},
|
|
131
|
+
/**
|
|
132
|
+
* Delays execution until the next microtask queue is empty
|
|
133
|
+
*
|
|
134
|
+
* @returns A Promise that resolves when the next microtask queue is empty.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* await delay.nextMicrotask();
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
nextMicrotask: () => {
|
|
142
|
+
if (typeof queueMicrotask !== "function") {
|
|
143
|
+
if (typeof setImmediate === "function") {
|
|
144
|
+
return delay.immediate();
|
|
145
|
+
}
|
|
146
|
+
return delay.by(0);
|
|
147
|
+
}
|
|
148
|
+
return new Promise((resolve) => queueMicrotask(resolve));
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
152
|
+
0 && (module.exports = {
|
|
153
|
+
delay
|
|
154
|
+
});
|
|
155
|
+
/*! For license information please see main.cjs.LEGAL.txt */
|
|
3
156
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/polyfill.ts"],
|
|
4
4
|
"sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\npackageTracer.add(__package_name__, __package_version__);\n\nimport {requestAnimationFrame, requestIdleCallback} from './polyfill.js';\n\n/**\n * A utility module to help manage asynchronous operations and waiting for events or timeouts.\n */\nexport const delay = {\n /**\n * Delays execution for a specified duration (in milliseconds).\n *\n * @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.\n * @returns A Promise that resolves after the specified duration.\n *\n * @example\n * ```typescript\n * await delay.by('1m'); // Wait for 1 minute\n * ```\n */\n by: (duration: Duration): Promise<void> =>\n new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),\n\n /**\n * Delays execution until the next animation frame.\n *\n * @returns A Promise that resolves with the current timestamp when the next animation frame is fired.\n *\n * @example\n * ```typescript\n * const timestamp = await delay.untilNextAnimationFrame();\n * ```\n */\n untilNextAnimationFrame: (): Promise<DOMHighResTimeStamp> =>\n new Promise((resolve) => requestAnimationFrame(resolve)),\n\n /**\n * Delays execution until the browser's idle period or the specified timeout.\n *\n * @param timeout - Optional timeout (in milliseconds) for the idle callback.\n * @returns A Promise that resolves with the IdleDeadline object when the browser is idle or the timeout is reached.\n *\n * @example\n * ```typescript\n * const deadline = await delay.untilIdle();\n * ```\n */\n untilIdle: (timeout?: Duration): Promise<IdleDeadline> =>\n new Promise((resolve) => requestIdleCallback(resolve, timeout === undefined ? undefined : {\n timeout: parseDuration(timeout)\n })),\n\n /**\n * Delays execution until a specific DOM event occurs on an HTMLElement.\n *\n * @param element - The HTMLElement to listen for the event on.\n * @param eventName - The name of the DOM event to wait for.\n * @template T The event map type.\n * @returns A Promise that resolves with the event object when the specified event occurs.\n *\n * @example\n * ```typescript\n * const clickEvent = await delay.untilDomEvent(document.body, 'click');\n * ```\n */\n untilDomEvent: <T extends keyof HTMLElementEventMap>(\n element: HTMLElement,\n eventName: T\n ): Promise<HTMLElementEventMap[T]> =>\n new Promise((resolve) =>\n element.addEventListener(eventName, resolve, { once: true, passive: true })\n ),\n\n /**\n * Delays execution until a specific event occurs on an object with an `addEventListener` method.\n *\n * @param target - The target object to listen for the event on.\n * @param eventName - The name of the event to wait for.\n * @returns A Promise that resolves with the event object when the specified event occurs.\n *\n * @example\n * ```typescript\n * const server = http.createServer();\n * const requestEvent = await delay.untilEvent(server, 'request');\n * ```\n */\n untilEvent: (target: HasAddEventListener, eventName: string): Promise<Event> =>\n new Promise((resolve) =>\n target.addEventListener(eventName, resolve, { once: true, passive: true })\n ),\n\n /**\n * Yields control to the event loop immediately.\n *\n * Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.\n *\n * @returns A Promise that resolves immediately after yielding control to the event loop.\n *\n * @example\n * ```typescript\n * await delay.immediate();\n * ```\n */\n immediate: (): Promise<void> => {\n if (typeof setImmediate !== 'function') {\n if (typeof queueMicrotask === 'function') {\n return delay.nextMicrotask();\n }\n\n // else\n return delay.by(0);\n }\n return new Promise((resolve) => setImmediate(resolve));\n },\n\n /**\n * Delays execution until the next microtask queue is empty\n *\n * @returns A Promise that resolves when the next microtask queue is empty.\n *\n * @example\n * ```typescript\n * await delay.nextMicrotask();\n * ```\n */\n nextMicrotask: (): Promise<void> => {\n if (typeof queueMicrotask !== 'function') {\n if (typeof setImmediate === 'function') {\n return delay.immediate();\n }\n\n // else\n return delay.by(0);\n }\n return new Promise((resolve) => queueMicrotask(resolve));\n },\n} as const;\n", "import {globalScope} from '@alwatr/global-scope';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const win = globalScope as DictionaryOpt<any>;\n\n// prettier-ignore\nconst requestAnimationFrameFallback =\n (callback: FrameRequestCallback): ReturnType<typeof setTimeout> =>\n setTimeout(() => callback(Date.now()), 1000 / 60);\n\n// prettier-ignore\nexport const requestAnimationFrame: typeof globalScope.requestAnimationFrame =\n win.requestAnimationFrame ||\n win.webkitRequestAnimationFrame ||\n win.mozRequestAnimationFrame ||\n requestAnimationFrameFallback;\n\n// prettier-ignore\nconst requestIdleCallbackFallback =\n (callback: () => void, options?: IdleRequestOptions): ReturnType<typeof setTimeout> =>\n setTimeout(callback, options?.timeout ?? 2000);\n\n// prettier-ignore\nexport const requestIdleCallback: typeof globalScope.requestIdleCallback =\n win.requestIdleCallback ||\n win.webkitRequestIdleCallback ||\n win.mozRequestIdleCallback ||\n requestIdleCallbackFallback;\n"],
|
|
5
|
-
"mappings": ";
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;AAC5B,4BAA2C;;;ACD3C,0BAA0B;AAGnB,IAAM,MAAM;AAGnB,IAAM,gCACJ,CAAC,aACC,WAAW,MAAM,SAAS,KAAK,IAAI,CAAC,GAAG,MAAO,EAAE;AAG7C,IAAM,wBACX,IAAI,yBACJ,IAAI,+BACJ,IAAI,4BACJ;AAGF,IAAM,8BACJ,CAAC,UAAsB,YACrB,WAAW,UAAU,SAAS,WAAW,GAAI;AAG1C,IAAM,sBACX,IAAI,uBACJ,IAAI,6BACJ,IAAI,0BACJ;;;ADxBF,oCAAc,IAAI,iBAAkB,OAAmB;AAOhD,IAAM,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYnB,IAAI,CAAC,aACH,IAAI,QAAQ,CAAC,YAAY,WAAW,aAAS,qCAAc,QAAQ,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYvE,yBAAyB,MACvB,IAAI,QAAQ,CAAC,YAAY,sBAAsB,OAAO,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAazD,WAAW,CAAC,YACV,IAAI,QAAQ,CAAC,YAAY,oBAAoB,SAAS,YAAY,SAAY,SAAY;AAAA,IACxF,aAAS,qCAAc,OAAO;AAAA,EAChC,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeJ,eAAe,CACb,SACA,cAEA,IAAI;AAAA,IAAQ,CAAC,YACX,QAAQ,iBAAiB,WAAW,SAAS,EAAE,MAAM,MAAM,SAAS,KAAK,CAAC;AAAA,EAC5E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeF,YAAY,CAAC,QAA6B,cACxC,IAAI;AAAA,IAAQ,CAAC,YACX,OAAO,iBAAiB,WAAW,SAAS,EAAE,MAAM,MAAM,SAAS,KAAK,CAAC;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcF,WAAW,MAAqB;AAC9B,QAAI,OAAO,iBAAiB,YAAY;AACtC,UAAI,OAAO,mBAAmB,YAAY;AACxC,eAAO,MAAM,cAAc;AAAA,MAC7B;AAGA,aAAO,MAAM,GAAG,CAAC;AAAA,IACnB;AACA,WAAO,IAAI,QAAQ,CAAC,YAAY,aAAa,OAAO,CAAC;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,eAAe,MAAqB;AAClC,QAAI,OAAO,mBAAmB,YAAY;AACxC,UAAI,OAAO,iBAAiB,YAAY;AACtC,eAAO,MAAM,UAAU;AAAA,MACzB;AAGA,aAAO,MAAM,GAAG,CAAC;AAAA,IACnB;AACA,WAAO,IAAI,QAAQ,CAAC,YAAY,eAAe,OAAO,CAAC;AAAA,EACzD;AACF;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/main.mjs
CHANGED
|
@@ -1,3 +1,132 @@
|
|
|
1
|
-
/* @alwatr/delay v1.0.
|
|
2
|
-
|
|
1
|
+
/* @alwatr/delay v1.0.3 */
|
|
2
|
+
|
|
3
|
+
// src/main.ts
|
|
4
|
+
import { packageTracer } from "@alwatr/package-tracer";
|
|
5
|
+
import { parseDuration } from "@alwatr/parse-duration";
|
|
6
|
+
|
|
7
|
+
// src/polyfill.ts
|
|
8
|
+
import { globalScope } from "@alwatr/global-scope";
|
|
9
|
+
var win = globalScope;
|
|
10
|
+
var requestAnimationFrameFallback = (callback) => setTimeout(() => callback(Date.now()), 1e3 / 60);
|
|
11
|
+
var requestAnimationFrame = win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback;
|
|
12
|
+
var requestIdleCallbackFallback = (callback, options) => setTimeout(callback, options?.timeout ?? 2e3);
|
|
13
|
+
var requestIdleCallback = win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback;
|
|
14
|
+
|
|
15
|
+
// src/main.ts
|
|
16
|
+
packageTracer.add("@alwatr/delay", "1.0.3");
|
|
17
|
+
var delay = {
|
|
18
|
+
/**
|
|
19
|
+
* Delays execution for a specified duration (in milliseconds).
|
|
20
|
+
*
|
|
21
|
+
* @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.
|
|
22
|
+
* @returns A Promise that resolves after the specified duration.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* await delay.by('1m'); // Wait for 1 minute
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
by: (duration) => new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),
|
|
30
|
+
/**
|
|
31
|
+
* Delays execution until the next animation frame.
|
|
32
|
+
*
|
|
33
|
+
* @returns A Promise that resolves with the current timestamp when the next animation frame is fired.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const timestamp = await delay.untilNextAnimationFrame();
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
untilNextAnimationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
|
|
41
|
+
/**
|
|
42
|
+
* Delays execution until the browser's idle period or the specified timeout.
|
|
43
|
+
*
|
|
44
|
+
* @param timeout - Optional timeout (in milliseconds) for the idle callback.
|
|
45
|
+
* @returns A Promise that resolves with the IdleDeadline object when the browser is idle or the timeout is reached.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const deadline = await delay.untilIdle();
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
untilIdle: (timeout) => new Promise((resolve) => requestIdleCallback(resolve, timeout === void 0 ? void 0 : {
|
|
53
|
+
timeout: parseDuration(timeout)
|
|
54
|
+
})),
|
|
55
|
+
/**
|
|
56
|
+
* Delays execution until a specific DOM event occurs on an HTMLElement.
|
|
57
|
+
*
|
|
58
|
+
* @param element - The HTMLElement to listen for the event on.
|
|
59
|
+
* @param eventName - The name of the DOM event to wait for.
|
|
60
|
+
* @template T The event map type.
|
|
61
|
+
* @returns A Promise that resolves with the event object when the specified event occurs.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const clickEvent = await delay.untilDomEvent(document.body, 'click');
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
untilDomEvent: (element, eventName) => new Promise(
|
|
69
|
+
(resolve) => element.addEventListener(eventName, resolve, { once: true, passive: true })
|
|
70
|
+
),
|
|
71
|
+
/**
|
|
72
|
+
* Delays execution until a specific event occurs on an object with an `addEventListener` method.
|
|
73
|
+
*
|
|
74
|
+
* @param target - The target object to listen for the event on.
|
|
75
|
+
* @param eventName - The name of the event to wait for.
|
|
76
|
+
* @returns A Promise that resolves with the event object when the specified event occurs.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const server = http.createServer();
|
|
81
|
+
* const requestEvent = await delay.untilEvent(server, 'request');
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
untilEvent: (target, eventName) => new Promise(
|
|
85
|
+
(resolve) => target.addEventListener(eventName, resolve, { once: true, passive: true })
|
|
86
|
+
),
|
|
87
|
+
/**
|
|
88
|
+
* Yields control to the event loop immediately.
|
|
89
|
+
*
|
|
90
|
+
* Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.
|
|
91
|
+
*
|
|
92
|
+
* @returns A Promise that resolves immediately after yielding control to the event loop.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* await delay.immediate();
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
immediate: () => {
|
|
100
|
+
if (typeof setImmediate !== "function") {
|
|
101
|
+
if (typeof queueMicrotask === "function") {
|
|
102
|
+
return delay.nextMicrotask();
|
|
103
|
+
}
|
|
104
|
+
return delay.by(0);
|
|
105
|
+
}
|
|
106
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
107
|
+
},
|
|
108
|
+
/**
|
|
109
|
+
* Delays execution until the next microtask queue is empty
|
|
110
|
+
*
|
|
111
|
+
* @returns A Promise that resolves when the next microtask queue is empty.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```typescript
|
|
115
|
+
* await delay.nextMicrotask();
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
nextMicrotask: () => {
|
|
119
|
+
if (typeof queueMicrotask !== "function") {
|
|
120
|
+
if (typeof setImmediate === "function") {
|
|
121
|
+
return delay.immediate();
|
|
122
|
+
}
|
|
123
|
+
return delay.by(0);
|
|
124
|
+
}
|
|
125
|
+
return new Promise((resolve) => queueMicrotask(resolve));
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
export {
|
|
129
|
+
delay
|
|
130
|
+
};
|
|
131
|
+
/*! For license information please see main.mjs.LEGAL.txt */
|
|
3
132
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
3
|
+
"sources": ["../src/main.ts", "../src/polyfill.ts"],
|
|
4
|
+
"sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\npackageTracer.add(__package_name__, __package_version__);\n\nimport {requestAnimationFrame, requestIdleCallback} from './polyfill.js';\n\n/**\n * A utility module to help manage asynchronous operations and waiting for events or timeouts.\n */\nexport const delay = {\n /**\n * Delays execution for a specified duration (in milliseconds).\n *\n * @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.\n * @returns A Promise that resolves after the specified duration.\n *\n * @example\n * ```typescript\n * await delay.by('1m'); // Wait for 1 minute\n * ```\n */\n by: (duration: Duration): Promise<void> =>\n new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),\n\n /**\n * Delays execution until the next animation frame.\n *\n * @returns A Promise that resolves with the current timestamp when the next animation frame is fired.\n *\n * @example\n * ```typescript\n * const timestamp = await delay.untilNextAnimationFrame();\n * ```\n */\n untilNextAnimationFrame: (): Promise<DOMHighResTimeStamp> =>\n new Promise((resolve) => requestAnimationFrame(resolve)),\n\n /**\n * Delays execution until the browser's idle period or the specified timeout.\n *\n * @param timeout - Optional timeout (in milliseconds) for the idle callback.\n * @returns A Promise that resolves with the IdleDeadline object when the browser is idle or the timeout is reached.\n *\n * @example\n * ```typescript\n * const deadline = await delay.untilIdle();\n * ```\n */\n untilIdle: (timeout?: Duration): Promise<IdleDeadline> =>\n new Promise((resolve) => requestIdleCallback(resolve, timeout === undefined ? undefined : {\n timeout: parseDuration(timeout)\n })),\n\n /**\n * Delays execution until a specific DOM event occurs on an HTMLElement.\n *\n * @param element - The HTMLElement to listen for the event on.\n * @param eventName - The name of the DOM event to wait for.\n * @template T The event map type.\n * @returns A Promise that resolves with the event object when the specified event occurs.\n *\n * @example\n * ```typescript\n * const clickEvent = await delay.untilDomEvent(document.body, 'click');\n * ```\n */\n untilDomEvent: <T extends keyof HTMLElementEventMap>(\n element: HTMLElement,\n eventName: T\n ): Promise<HTMLElementEventMap[T]> =>\n new Promise((resolve) =>\n element.addEventListener(eventName, resolve, { once: true, passive: true })\n ),\n\n /**\n * Delays execution until a specific event occurs on an object with an `addEventListener` method.\n *\n * @param target - The target object to listen for the event on.\n * @param eventName - The name of the event to wait for.\n * @returns A Promise that resolves with the event object when the specified event occurs.\n *\n * @example\n * ```typescript\n * const server = http.createServer();\n * const requestEvent = await delay.untilEvent(server, 'request');\n * ```\n */\n untilEvent: (target: HasAddEventListener, eventName: string): Promise<Event> =>\n new Promise((resolve) =>\n target.addEventListener(eventName, resolve, { once: true, passive: true })\n ),\n\n /**\n * Yields control to the event loop immediately.\n *\n * Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.\n *\n * @returns A Promise that resolves immediately after yielding control to the event loop.\n *\n * @example\n * ```typescript\n * await delay.immediate();\n * ```\n */\n immediate: (): Promise<void> => {\n if (typeof setImmediate !== 'function') {\n if (typeof queueMicrotask === 'function') {\n return delay.nextMicrotask();\n }\n\n // else\n return delay.by(0);\n }\n return new Promise((resolve) => setImmediate(resolve));\n },\n\n /**\n * Delays execution until the next microtask queue is empty\n *\n * @returns A Promise that resolves when the next microtask queue is empty.\n *\n * @example\n * ```typescript\n * await delay.nextMicrotask();\n * ```\n */\n nextMicrotask: (): Promise<void> => {\n if (typeof queueMicrotask !== 'function') {\n if (typeof setImmediate === 'function') {\n return delay.immediate();\n }\n\n // else\n return delay.by(0);\n }\n return new Promise((resolve) => queueMicrotask(resolve));\n },\n} as const;\n", "import {globalScope} from '@alwatr/global-scope';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const win = globalScope as DictionaryOpt<any>;\n\n// prettier-ignore\nconst requestAnimationFrameFallback =\n (callback: FrameRequestCallback): ReturnType<typeof setTimeout> =>\n setTimeout(() => callback(Date.now()), 1000 / 60);\n\n// prettier-ignore\nexport const requestAnimationFrame: typeof globalScope.requestAnimationFrame =\n win.requestAnimationFrame ||\n win.webkitRequestAnimationFrame ||\n win.mozRequestAnimationFrame ||\n requestAnimationFrameFallback;\n\n// prettier-ignore\nconst requestIdleCallbackFallback =\n (callback: () => void, options?: IdleRequestOptions): ReturnType<typeof setTimeout> =>\n setTimeout(callback, options?.timeout ?? 2000);\n\n// prettier-ignore\nexport const requestIdleCallback: typeof globalScope.requestIdleCallback =\n win.requestIdleCallback ||\n win.webkitRequestIdleCallback ||\n win.mozRequestIdleCallback ||\n requestIdleCallbackFallback;\n"],
|
|
5
|
+
"mappings": ";;;AAAA,SAAQ,qBAAoB;AAC5B,SAAQ,qBAAmC;;;ACD3C,SAAQ,mBAAkB;AAGnB,IAAM,MAAM;AAGnB,IAAM,gCACJ,CAAC,aACC,WAAW,MAAM,SAAS,KAAK,IAAI,CAAC,GAAG,MAAO,EAAE;AAG7C,IAAM,wBACX,IAAI,yBACJ,IAAI,+BACJ,IAAI,4BACJ;AAGF,IAAM,8BACJ,CAAC,UAAsB,YACrB,WAAW,UAAU,SAAS,WAAW,GAAI;AAG1C,IAAM,sBACX,IAAI,uBACJ,IAAI,6BACJ,IAAI,0BACJ;;;ADxBF,cAAc,IAAI,iBAAkB,OAAmB;AAOhD,IAAM,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYnB,IAAI,CAAC,aACH,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,cAAc,QAAQ,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYvE,yBAAyB,MACvB,IAAI,QAAQ,CAAC,YAAY,sBAAsB,OAAO,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAazD,WAAW,CAAC,YACV,IAAI,QAAQ,CAAC,YAAY,oBAAoB,SAAS,YAAY,SAAY,SAAY;AAAA,IACxF,SAAS,cAAc,OAAO;AAAA,EAChC,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeJ,eAAe,CACb,SACA,cAEA,IAAI;AAAA,IAAQ,CAAC,YACX,QAAQ,iBAAiB,WAAW,SAAS,EAAE,MAAM,MAAM,SAAS,KAAK,CAAC;AAAA,EAC5E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeF,YAAY,CAAC,QAA6B,cACxC,IAAI;AAAA,IAAQ,CAAC,YACX,OAAO,iBAAiB,WAAW,SAAS,EAAE,MAAM,MAAM,SAAS,KAAK,CAAC;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcF,WAAW,MAAqB;AAC9B,QAAI,OAAO,iBAAiB,YAAY;AACtC,UAAI,OAAO,mBAAmB,YAAY;AACxC,eAAO,MAAM,cAAc;AAAA,MAC7B;AAGA,aAAO,MAAM,GAAG,CAAC;AAAA,IACnB;AACA,WAAO,IAAI,QAAQ,CAAC,YAAY,aAAa,OAAO,CAAC;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,eAAe,MAAqB;AAClC,QAAI,OAAO,mBAAmB,YAAY;AACxC,UAAI,OAAO,iBAAiB,YAAY;AACtC,eAAO,MAAM,UAAU;AAAA,MACzB;AAGA,aAAO,MAAM,GAAG,CAAC;AAAA,IACnB;AACA,WAAO,IAAI,QAAQ,CAAC,YAAY,eAAe,OAAO,CAAC;AAAA,EACzD;AACF;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/delay",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Comprehensive toolkit for managing asynchronous operations.",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"keywords": [
|
|
@@ -66,17 +66,17 @@
|
|
|
66
66
|
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@alwatr/global-scope": "^1.1.
|
|
70
|
-
"@alwatr/package-tracer": "^1.0.
|
|
71
|
-
"@alwatr/parse-duration": "^1.1.
|
|
69
|
+
"@alwatr/global-scope": "^1.1.25",
|
|
70
|
+
"@alwatr/package-tracer": "^1.0.3",
|
|
71
|
+
"@alwatr/parse-duration": "^1.1.3"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@alwatr/nano-build": "^
|
|
74
|
+
"@alwatr/nano-build": "^2.0.0",
|
|
75
75
|
"@alwatr/prettier-config": "^1.0.5",
|
|
76
|
-
"@alwatr/tsconfig-base": "^1.3.
|
|
77
|
-
"@alwatr/type-helper": "^2.0.
|
|
78
|
-
"@types/node": "^22.7.
|
|
79
|
-
"typescript": "^5.6.
|
|
76
|
+
"@alwatr/tsconfig-base": "^1.3.1",
|
|
77
|
+
"@alwatr/type-helper": "^2.0.1",
|
|
78
|
+
"@types/node": "^22.7.5",
|
|
79
|
+
"typescript": "^5.6.3"
|
|
80
80
|
},
|
|
81
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "e0b3b4a47c02a395046982803b2e431c6ee3b0ab"
|
|
82
82
|
}
|