@alwatr/delay 1.0.2 → 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 CHANGED
@@ -3,6 +3,10 @@
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
+
6
10
  ## [1.0.2](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@1.0.1...@alwatr/delay@1.0.2) (2024-10-10)
7
11
 
8
12
  ### Dependencies update
package/dist/main.cjs CHANGED
@@ -1,3 +1,156 @@
1
- /* @alwatr/delay v1.0.2 */
2
- "use strict";var a=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var q=(e,t)=>{for(var i in t)a(e,i,{get:t[i],enumerable:!0})},b=(e,t,i,m)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of f(t))!k.call(e,n)&&n!==i&&a(e,n,{get:()=>t[n],enumerable:!(m=p(t,n))||m.enumerable});return e};var y=e=>b(a({},"__esModule",{value:!0}),e);var P={};q(P,{delay:()=>r});module.exports=y(P);var u=require("@alwatr/global-scope"),o=u.globalScope,v=e=>setTimeout(()=>e(Date.now()),1e3/60),l=o.requestAnimationFrame||o.webkitRequestAnimationFrame||o.mozRequestAnimationFrame||v,T=(e,t)=>setTimeout(e,t?.timeout??2e3),c=o.requestIdleCallback||o.webkitRequestIdleCallback||o.mozRequestIdleCallback||T;var d=require("@alwatr/package-tracer"),s=require("@alwatr/parse-duration");d.packageTracer.add("@alwatr/delay","1.0.2");var r={by:e=>new Promise(t=>setTimeout(t,(0,s.parseDuration)(e))),untilNextAnimationFrame:()=>new Promise(e=>l(e)),untilIdle:e=>new Promise(t=>c(t,e===void 0?void 0:{timeout:(0,s.parseDuration)(e)})),untilDomEvent:(e,t)=>new Promise(i=>e.addEventListener(t,i,{once:!0,passive:!0})),untilEvent:(e,t)=>new Promise(i=>e.addEventListener(t,i,{once:!0,passive:!0})),immediate:()=>typeof setImmediate!="function"?typeof queueMicrotask=="function"?r.nextMicrotask():r.by(0):new Promise(e=>setImmediate(e)),nextMicrotask:()=>typeof queueMicrotask!="function"?typeof setImmediate=="function"?r.immediate():r.by(0):new Promise(e=>queueMicrotask(e))};0&&(module.exports={delay});
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": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,WAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAA0B,gCAGbC,EAAM,cAGbC,EACHC,GACC,WAAW,IAAMA,EAAS,KAAK,IAAI,CAAC,EAAG,IAAO,EAAE,EAGvCC,EACXH,EAAI,uBACJA,EAAI,6BACJA,EAAI,0BACJC,EAGIG,EACJ,CAACF,EAAsBG,IACrB,WAAWH,EAAUG,GAAS,SAAW,GAAI,EAGpCC,EACXN,EAAI,qBACJA,EAAI,2BACJA,EAAI,wBACJI,ED3BF,IAAAG,EAA4B,kCAC5BC,EAA2C,kCAE3C,gBAAc,IAAI,gBAAkB,OAAmB,EAOhD,IAAMC,EAAQ,CAYnB,GAAKC,GACH,IAAI,QAASC,GAAY,WAAWA,KAAS,iBAAcD,CAAQ,CAAC,CAAC,EAYvE,wBAAyB,IACvB,IAAI,QAASC,GAAYC,EAAsBD,CAAO,CAAC,EAazD,UAAYE,GACV,IAAI,QAASF,GAAYG,EAAoBH,EAASE,IAAY,OAAY,OAAY,CACxF,WAAS,iBAAcA,CAAO,CAChC,CAAC,CAAC,EAeJ,cAAe,CACbE,EACAC,IAEA,IAAI,QAASL,GACXI,EAAQ,iBAAiBC,EAAWL,EAAS,CAAE,KAAM,GAAM,QAAS,EAAK,CAAC,CAC5E,EAeF,WAAY,CAACM,EAA6BD,IACxC,IAAI,QAASL,GACXM,EAAO,iBAAiBD,EAAWL,EAAS,CAAE,KAAM,GAAM,QAAS,EAAK,CAAC,CAC3E,EAcF,UAAW,IACL,OAAO,cAAiB,WACtB,OAAO,gBAAmB,WACrBF,EAAM,cAAc,EAItBA,EAAM,GAAG,CAAC,EAEZ,IAAI,QAASE,GAAY,aAAaA,CAAO,CAAC,EAavD,cAAe,IACT,OAAO,gBAAmB,WACxB,OAAO,cAAiB,WACnBF,EAAM,UAAU,EAIlBA,EAAM,GAAG,CAAC,EAEZ,IAAI,QAASE,GAAY,eAAeA,CAAO,CAAC,CAE3D",
6
- "names": ["main_exports", "__export", "delay", "__toCommonJS", "import_global_scope", "win", "requestAnimationFrameFallback", "callback", "requestAnimationFrame", "requestIdleCallbackFallback", "options", "requestIdleCallback", "import_package_tracer", "import_parse_duration", "delay", "duration", "resolve", "requestAnimationFrame", "timeout", "requestIdleCallback", "element", "eventName", "target"]
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;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AiB,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 */
2
- import{globalScope as m}from"@alwatr/global-scope";var i=m,u=e=>setTimeout(()=>e(Date.now()),1e3/60),r=i.requestAnimationFrame||i.webkitRequestAnimationFrame||i.mozRequestAnimationFrame||u,l=(e,t)=>setTimeout(e,t?.timeout??2e3),a=i.requestIdleCallback||i.webkitRequestIdleCallback||i.mozRequestIdleCallback||l;import{packageTracer as c}from"@alwatr/package-tracer";import{parseDuration as s}from"@alwatr/parse-duration";c.add("@alwatr/delay","1.0.2");var o={by:e=>new Promise(t=>setTimeout(t,s(e))),untilNextAnimationFrame:()=>new Promise(e=>r(e)),untilIdle:e=>new Promise(t=>a(t,e===void 0?void 0:{timeout:s(e)})),untilDomEvent:(e,t)=>new Promise(n=>e.addEventListener(t,n,{once:!0,passive:!0})),untilEvent:(e,t)=>new Promise(n=>e.addEventListener(t,n,{once:!0,passive:!0})),immediate:()=>typeof setImmediate!="function"?typeof queueMicrotask=="function"?o.nextMicrotask():o.by(0):new Promise(e=>setImmediate(e)),nextMicrotask:()=>typeof queueMicrotask!="function"?typeof setImmediate=="function"?o.immediate():o.by(0):new Promise(e=>queueMicrotask(e))};export{o as delay};
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/polyfill.ts", "../src/main.ts"],
4
- "sourcesContent": ["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", "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"],
5
- "mappings": ";AAAA,OAAQ,eAAAA,MAAkB,uBAGnB,IAAMC,EAAMD,EAGbE,EACHC,GACC,WAAW,IAAMA,EAAS,KAAK,IAAI,CAAC,EAAG,IAAO,EAAE,EAGvCC,EACXH,EAAI,uBACJA,EAAI,6BACJA,EAAI,0BACJC,EAGIG,EACJ,CAACF,EAAsBG,IACrB,WAAWH,EAAUG,GAAS,SAAW,GAAI,EAGpCC,EACXN,EAAI,qBACJA,EAAI,2BACJA,EAAI,wBACJI,EC3BF,OAAQ,iBAAAG,MAAoB,yBAC5B,OAAQ,iBAAAC,MAAmC,yBAE3CD,EAAc,IAAI,gBAAkB,OAAmB,EAOhD,IAAME,EAAQ,CAYnB,GAAKC,GACH,IAAI,QAASC,GAAY,WAAWA,EAASH,EAAcE,CAAQ,CAAC,CAAC,EAYvE,wBAAyB,IACvB,IAAI,QAASC,GAAYC,EAAsBD,CAAO,CAAC,EAazD,UAAYE,GACV,IAAI,QAASF,GAAYG,EAAoBH,EAASE,IAAY,OAAY,OAAY,CACxF,QAASL,EAAcK,CAAO,CAChC,CAAC,CAAC,EAeJ,cAAe,CACbE,EACAC,IAEA,IAAI,QAASL,GACXI,EAAQ,iBAAiBC,EAAWL,EAAS,CAAE,KAAM,GAAM,QAAS,EAAK,CAAC,CAC5E,EAeF,WAAY,CAACM,EAA6BD,IACxC,IAAI,QAASL,GACXM,EAAO,iBAAiBD,EAAWL,EAAS,CAAE,KAAM,GAAM,QAAS,EAAK,CAAC,CAC3E,EAcF,UAAW,IACL,OAAO,cAAiB,WACtB,OAAO,gBAAmB,WACrBF,EAAM,cAAc,EAItBA,EAAM,GAAG,CAAC,EAEZ,IAAI,QAASE,GAAY,aAAaA,CAAO,CAAC,EAavD,cAAe,IACT,OAAO,gBAAmB,WACxB,OAAO,cAAiB,WACnBF,EAAM,UAAU,EAIlBA,EAAM,GAAG,CAAC,EAEZ,IAAI,QAASE,GAAY,eAAeA,CAAO,CAAC,CAE3D",
6
- "names": ["globalScope", "win", "requestAnimationFrameFallback", "callback", "requestAnimationFrame", "requestIdleCallbackFallback", "options", "requestIdleCallback", "packageTracer", "parseDuration", "delay", "duration", "resolve", "requestAnimationFrame", "timeout", "requestIdleCallback", "element", "eventName", "target"]
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;AAA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A;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.2",
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.24",
70
- "@alwatr/package-tracer": "^1.0.2",
71
- "@alwatr/parse-duration": "^1.1.2"
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": "^1.6.0",
74
+ "@alwatr/nano-build": "^2.0.0",
75
75
  "@alwatr/prettier-config": "^1.0.5",
76
76
  "@alwatr/tsconfig-base": "^1.3.1",
77
77
  "@alwatr/type-helper": "^2.0.1",
78
78
  "@types/node": "^22.7.5",
79
79
  "typescript": "^5.6.3"
80
80
  },
81
- "gitHead": "6ad24764eae1b88d7d1bb19217578b02b8c22aaf"
81
+ "gitHead": "e0b3b4a47c02a395046982803b2e431c6ee3b0ab"
82
82
  }