@alwatr/delay 5.5.10 → 6.0.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/CHANGELOG.md CHANGED
@@ -3,6 +3,47 @@
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
+ ## [6.0.0](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@5.5.11...@alwatr/delay@6.0.0) (2025-09-08)
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ * **delay:** The API has been completely redesigned. All previous standalone functions are removed and replaced by methods on the `delay` object.
11
+
12
+ - **REMOVED:**
13
+ - `untilNextAnimationFrame`
14
+ - `untilIdle`
15
+ - `untilDomEvent`
16
+ - `untilEvent`
17
+ - `immediate`
18
+ - `nextMicrotask`
19
+
20
+ - **ADDED:**
21
+ - `delay.animationFrame` (replaces `waitForAnimationFrame`)
22
+ - `delay.idleCallback` (replaces `waitForIdle`)
23
+ - `delay.domEvent` (replaces `waitForDomEvent`)
24
+ - `delay.event` (replaces `waitForEvent`)
25
+ - `delay.nextMacrotask` (replaces `waitForImmediate`)
26
+ - `delay.nextMicrotask` (replaces `waitForMicrotask`)
27
+
28
+ Users must update their code to import the `delay` object and use the new method names. For example, `delay.immediate()` should be changed to `delay.nextMacrotask()`.
29
+
30
+ ### ✨ Features
31
+
32
+ * **delay:** Overhaul delay module with improved API and corrected implementations ([7c12483](https://github.com/Alwatr/nanolib/commit/7c1248354f2535a65cb7981c42ad4e319badb4aa))
33
+
34
+ ### 🔨 Code Refactoring
35
+
36
+ * **polyfill:** rename global_ to globalThis for clarity and consistency ([7d1484f](https://github.com/Alwatr/nanolib/commit/7d1484fb91a66d46b62011d0fb7825f3089183f8))
37
+ * **polyfill:** streamline requestAnimationFrame and requestIdleCallback implementations ([d18443d](https://github.com/Alwatr/nanolib/commit/d18443d4dedddff8f54227aa7aff2bca5aaacdfa))
38
+
39
+ ### 🧹 Miscellaneous Chores
40
+
41
+ * **main:** export requestAnimationFrame and requestIdleCallback from main module ([ef80797](https://github.com/Alwatr/nanolib/commit/ef80797319e3bded5c36e98352b6317427d08a59))
42
+
43
+ ## [5.5.11](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@5.5.10...@alwatr/delay@5.5.11) (2025-09-06)
44
+
45
+ **Note:** Version bump only for package @alwatr/delay
46
+
6
47
  ## [5.5.10](https://github.com/Alwatr/nanolib/compare/@alwatr/delay@5.5.9...@alwatr/delay@5.5.10) (2025-09-05)
7
48
 
8
49
  ### 🔗 Dependencies update
package/README.md CHANGED
@@ -1,106 +1,109 @@
1
1
  # @alwatr/delay
2
2
 
3
- `@alwatr/delay` offers a collection of utility functions to handle asynchronous execution flow effectively. It allows you to pause your code until specific conditions are met or certain events occur. This functionality aids in managing complex asynchronous scenarios and crafting intricate flows with ease. The functions can be used independently or combined for robust control over asynchronous operations.
3
+ A robust and lightweight utility library for managing asynchronous operations in JavaScript and TypeScript. `@alwatr/delay` provides a collection of promise-based functions to pause execution until specific conditions are met, such as timeouts, animation frames, idle callbacks, or DOM events. This helps create clean, readable, and predictable asynchronous code.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
+ # Using npm
8
9
  npm install @alwatr/delay
9
- ```
10
10
 
11
- ```bash
11
+ # Using yarn
12
12
  yarn add @alwatr/delay
13
13
  ```
14
14
 
15
15
  ## Usage
16
16
 
17
- Each function within `@alwatr/delay` returns a Promise that resolves when the specified waiting condition is met. Here's a breakdown of the available functions:
18
-
19
- - **waitForTimeout(duration: number): Promise<void>**
20
-
21
- - Waits for a specified duration (in milliseconds) before resolving.
22
- - Example:
17
+ All functions are available under the `delay` object and return a `Promise`. You can use them with `async/await` for clean and linear code flow.
23
18
 
24
- ```typescript
25
- import {waitForTimeout} from '@alwatr/delay';
19
+ ```typescript
20
+ import {delay} from '@alwatr/delay';
26
21
 
27
- await waitForTimeout(1000); // Waits for 1 second
28
- ```
29
-
30
- - **waitForAnimationFrame(): Promise<DOMHighResTimeStamp>**
31
-
32
- - Pauses execution until the next animation frame is scheduled, resolving with the current timestamp.
33
- - Useful for synchronizing UI updates with browser rendering.
34
- - Example:
22
+ async function main() {
23
+ console.log('Waiting for 1 second...');
24
+ await delay.by('1s');
25
+ console.log('Done.');
26
+ }
27
+ ```
35
28
 
36
- ```typescript
37
- import {waitForAnimationFrame} from '@alwatr/delay';
29
+ ### API Reference
38
30
 
39
- await waitForAnimationFrame(); // Waits for next animation frame
40
- ```
31
+ - **`delay.by(duration: Duration): Promise<void>`**
41
32
 
42
- - **waitForIdle(timeout?: number): Promise<IdleDeadline>**
33
+ Pauses execution for a specified duration. The duration can be a number (in milliseconds) or a string (e.g., `'2s'`, `'500ms'`).
43
34
 
44
- - Waits for the next idle period (when the browser is not busy), resolving with an `IdleDeadline` object.
45
- - Optionally accepts a timeout value (in milliseconds) for maximum waiting time.
46
- - Ideal for executing tasks that don't impact user experience.
47
- - Example:
35
+ ```typescript
36
+ await delay.by(2000); // Waits for 2 seconds
37
+ await delay.by('5m'); // Waits for 5 minutes
38
+ ```
48
39
 
49
- ```typescript
50
- import {waitForIdle} from '@alwatr/delay';
40
+ - **`delay.animationFrame(): Promise<DOMHighResTimeStamp>`**
51
41
 
52
- await waitForIdle(); // Waits for next idle period
53
- ```
42
+ Resolves at the beginning of the next browser animation frame. Useful for synchronizing animations and DOM updates with the browser's rendering cycle to avoid layout thrashing.
54
43
 
55
- - **waitForDomEvent<T extends keyof HTMLElementEventMap>(element: HTMLElement, eventName: T): Promise<HTMLElementEventMap[T]>**
44
+ ```typescript
45
+ const timestamp = await delay.animationFrame();
46
+ console.log(`Rendering next frame at ${timestamp}`);
47
+ // Perform DOM updates here
48
+ ```
56
49
 
57
- - Pauses execution until a specific DOM event is triggered on a provided element, resolving with the event object.
58
- - Example:
50
+ - **`delay.idleCallback(options?: IdleRequestOptions): Promise<IdleDeadline>`**
59
51
 
60
- ```typescript
61
- import {waitForDomEvent} from '@alwatr/delay';
52
+ Resolves when the browser's event loop is idle. Ideal for deferring non-critical background tasks to avoid impacting user experience.
62
53
 
63
- const button = document.getElementById('myButton');
64
- await waitForDomEvent(button, 'click'); // Waits for click event on button
65
- ```
54
+ ```typescript
55
+ const deadline = await delay.idleCallback({timeout: 1000});
56
+ if (!deadline.didTimeout) {
57
+ console.log('Running background task during idle time.');
58
+ }
59
+ ```
66
60
 
67
- - **waitForEvent(target: HasAddEventListener, eventName: string): Promise<Event>**
61
+ - **`delay.domEvent<T extends keyof HTMLElementEventMap>(element: HTMLElement, eventName: T, options?: AddEventListenerOptions): Promise<HTMLElementEventMap[T]>`**
68
62
 
69
- - More generic version of `waitForDomEvent`, allowing waiting for any event on any object with an `addEventListener` method.
70
- - Example:
63
+ Waits for a specific DOM event to be dispatched on an `HTMLElement`.
71
64
 
72
- ```typescript
73
- import {waitForEvent} from '@alwatr/delay';
65
+ ```typescript
66
+ const button = document.getElementById('my-button');
67
+ if (button) {
68
+ const event = await delay.domEvent(button, 'click');
69
+ console.log('Button was clicked!', event);
70
+ }
71
+ ```
74
72
 
75
- const server = http.createServer();
76
- await waitForEvent(server, 'request'); // Waits for request event on server
77
- ```
73
+ - **`delay.event(target: EventTarget, eventName: string, options?: AddEventListenerOptions): Promise<Event>`**
78
74
 
79
- - **waitForImmediate(): Promise<void>**
75
+ A more generic version of `domEvent`. Waits for any event on any `EventTarget` (e.g., `window`, `document`, or custom event emitters).
80
76
 
81
- - Executes the next task in the microtask queue immediately after the current task finishes.
82
- - Example:
77
+ ```typescript
78
+ console.log('Waiting for window resize...');
79
+ await delay.event(window, 'resize');
80
+ console.log('Window was resized!');
81
+ ```
83
82
 
84
- ```typescript
85
- import {waitForImmediate} from '@alwatr/delay';
83
+ - **`delay.nextMacrotask(): Promise<void>`**
86
84
 
87
- await waitForImmediate(); // Executes next microtask
88
- ```
85
+ Schedules a macrotask to run in the next cycle of the event loop. This is useful for yielding control back to the browser, allowing it to handle rendering and other user-facing tasks. Implemented with `setTimeout(..., 0)`.
89
86
 
90
- - **waitForMicrotask(): Promise<void>**
87
+ ```typescript
88
+ console.log('A');
89
+ await delay.nextMacrotask();
90
+ console.log('B'); // This will log after 'A' and after the browser has had a chance to breathe.
91
+ ```
91
92
 
92
- - Similar to `waitForImmediate`, but waits specifically for the next microtask queue.
93
- - Example:
93
+ - **`delay.nextMicrotask(): Promise<void>`**
94
94
 
95
- ```typescript
96
- import {waitForMicrotask} from '@alwatr/delay';
95
+ Queues a microtask to be executed immediately after the current task completes, before the event loop proceeds to the next macrotask. Useful for scheduling work that needs to happen synchronously after an operation but without blocking the main thread.
97
96
 
98
- await waitForMicrotask(); // Waits for next microtask queue
99
- ```
97
+ ```typescript
98
+ console.log('A');
99
+ Promise.resolve().then(() => console.log('C'));
100
+ await delay.nextMicrotask();
101
+ console.log('B'); // Logs A, C, B
102
+ ```
100
103
 
101
104
  ## Contributing
102
105
 
103
- We welcome contributions to improve this package! Feel free to open bug reports, suggest new features, or submit pull requests following our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md).
106
+ Contributions are welcome\! Please feel free to open an issue or submit a pull request. Read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) to get started.
104
107
 
105
108
  ## Sponsors
106
109
 
package/dist/main.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /* @alwatr/delay v5.5.10 */
1
+ /* @alwatr/delay v6.0.0 */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -21,7 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/main.ts
22
22
  var main_exports = {};
23
23
  __export(main_exports, {
24
- delay: () => delay
24
+ delay: () => delay,
25
+ requestAnimationFrame: () => requestAnimationFrame,
26
+ requestIdleCallback: () => requestIdleCallback
25
27
  });
26
28
  module.exports = __toCommonJS(main_exports);
27
29
  var import_package_tracer = require("@alwatr/package-tracer");
@@ -29,127 +31,139 @@ var import_parse_duration = require("@alwatr/parse-duration");
29
31
 
30
32
  // src/polyfill.ts
31
33
  var import_global_this = require("@alwatr/global-this");
32
- var win = /* @__PURE__ */ (0, import_global_this.getGlobalThis)();
33
- var requestAnimationFrameFallback = (callback) => setTimeout(() => callback(Date.now()), 1e3 / 60);
34
- var requestAnimationFrame = /* @__PURE__ */ (() => win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback)();
35
- var requestIdleCallbackFallback = (callback, options) => setTimeout(callback, options?.timeout ?? 2e3);
36
- var requestIdleCallback = /* @__PURE__ */ (() => win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback)();
34
+ var globalThis = /* @__PURE__ */ (0, import_global_this.getGlobalThis)();
35
+ var requestAnimationFrame = /* @__PURE__ */ globalThis.requestAnimationFrame?.bind(globalThis) ?? ((callback) => setTimeout(() => callback(performance.now()), 1e3 / 60));
36
+ var requestIdleCallback = /* @__PURE__ */ globalThis.requestIdleCallback?.bind(globalThis) ?? ((callback, options) => {
37
+ const startTime = Date.now();
38
+ return setTimeout(() => {
39
+ callback({
40
+ didTimeout: !!options?.timeout,
41
+ timeRemaining: () => Math.max(0, 50 - (Date.now() - startTime))
42
+ });
43
+ }, options?.timeout ?? 20);
44
+ });
37
45
 
38
46
  // src/main.ts
39
- __dev_mode__: import_package_tracer.packageTracer.add("@alwatr/delay", "5.5.10");
47
+ __dev_mode__: import_package_tracer.packageTracer.add("@alwatr/delay", "6.0.0");
40
48
  var delay = {
41
49
  /**
42
- * Delays execution for a specified duration (in milliseconds).
50
+ * Pauses execution for a specified duration.
43
51
  *
44
- * @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.
52
+ * @param duration The duration to wait. Can be a number in milliseconds or a string like '2s', '100ms'.
45
53
  * @returns A Promise that resolves after the specified duration.
46
54
  *
47
55
  * @example
48
56
  * ```typescript
49
57
  * await delay.by('1m'); // Wait for 1 minute
58
+ * await delay.by('2s'); // Wait for 2 seconds
50
59
  * ```
51
60
  */
52
61
  by: (duration) => new Promise((resolve) => setTimeout(resolve, (0, import_parse_duration.parseDuration)(duration))),
53
62
  /**
54
- * Delays execution until the next animation frame.
63
+ * Pauses execution until the next animation frame.
55
64
  *
56
- * @returns A Promise that resolves with the current timestamp when the next animation frame is fired.
65
+ * @returns A Promise that resolves with the high-resolution timestamp of the next animation frame.
57
66
  *
58
67
  * @example
59
68
  * ```typescript
60
- * const timestamp = await delay.untilNextAnimationFrame();
69
+ * const timestamp = await delay.animationFrame();
70
+ * console.log(`Next frame at ${timestamp}`);
61
71
  * ```
62
72
  */
63
- untilNextAnimationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
73
+ animationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
64
74
  /**
65
- * Delays execution until the browser's idle period or the specified timeout.
75
+ * Pauses execution until the browser is idle.
66
76
  *
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.
77
+ * @param timeout An optional maximum duration to wait.
78
+ * @returns A Promise that resolves with an `IdleDeadline` object.
69
79
  *
70
80
  * @example
71
81
  * ```typescript
72
- * const deadline = await delay.untilIdle();
82
+ * const deadline = await delay.idleCallback({ timeout: 2000 });
83
+ * if (deadline.didTimeout) {
84
+ * console.log('Idle callback timed out.');
85
+ * }
73
86
  * ```
74
87
  */
75
- untilIdle: (timeout) => new Promise((resolve) => requestIdleCallback(resolve, timeout === void 0 ? void 0 : {
76
- timeout: (0, import_parse_duration.parseDuration)(timeout)
77
- })),
88
+ idleCallback: (options) => new Promise((resolve) => requestIdleCallback(resolve, options)),
78
89
  /**
79
- * Delays execution until a specific DOM event occurs on an HTMLElement.
90
+ * Pauses execution until a specific DOM event is dispatched on an element.
80
91
  *
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.
92
+ * @param element The HTMLElement to listen on.
93
+ * @param eventName The name of the event to wait for.
94
+ * @param options Optional event listener options.
95
+ * @template T The event map type for the element.
96
+ * @returns A Promise that resolves with the triggered event object.
85
97
  *
86
98
  * @example
87
99
  * ```typescript
88
- * const clickEvent = await delay.untilDomEvent(document.body, 'click');
100
+ * const button = document.getElementById('my-button');
101
+ * if (button) {
102
+ * const clickEvent = await delay.domEvent(button, 'click');
103
+ * console.log('Button clicked!', clickEvent);
104
+ * }
89
105
  * ```
90
106
  */
91
- untilDomEvent: (element, eventName) => new Promise(
92
- (resolve) => element.addEventListener(eventName, resolve, { once: true, passive: true })
107
+ domEvent: (element, eventName, options = { passive: true }) => new Promise(
108
+ (resolve) => element.addEventListener(eventName, resolve, {
109
+ ...options,
110
+ once: true
111
+ })
93
112
  ),
94
113
  /**
95
- * Delays execution until a specific event occurs on an object with an `addEventListener` method.
114
+ * Pauses execution until a specific event is dispatched on any event target.
96
115
  *
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.
116
+ * @param target The event target (e.g., window, document, or a custom event emitter).
117
+ * @param eventName The name of the event to wait for.
118
+ * @param options Optional event listener options.
119
+ * @returns A Promise that resolves with the triggered event object.
100
120
  *
101
121
  * @example
102
122
  * ```typescript
103
- * const server = http.createServer();
104
- * const requestEvent = await delay.untilEvent(server, 'request');
123
+ * const resizeEvent = await delay.event(window, 'resize');
124
+ * console.log('Window resized:', resizeEvent);
105
125
  * ```
106
126
  */
107
- untilEvent: (target, eventName) => new Promise(
108
- (resolve) => target.addEventListener(eventName, resolve, { once: true, passive: true })
127
+ event: (target, eventName, options = { passive: true }) => new Promise(
128
+ (resolve) => target.addEventListener(eventName, resolve, {
129
+ ...options,
130
+ once: true
131
+ })
109
132
  ),
110
133
  /**
111
- * Yields control to the event loop immediately.
112
- *
113
- * Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.
134
+ * Schedules a macrotask to run after the current event loop task completes.
135
+ * Uses `setTimeout(..., 0)`.
114
136
  *
115
- * @returns A Promise that resolves immediately after yielding control to the event loop.
137
+ * @returns A Promise that resolves when the macrotask is executed.
116
138
  *
117
139
  * @example
118
140
  * ```typescript
119
- * await delay.immediate();
141
+ * console.log('Start');
142
+ * await delay.nextMacrotask();
143
+ * console.log('End - after current task');
120
144
  * ```
121
145
  */
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
- },
146
+ nextMacrotask: () => new Promise((resolve) => setTimeout(resolve, 0)),
131
147
  /**
132
- * Delays execution until the next microtask queue is empty
148
+ * Queues a microtask to run after the current task completes but before the next macrotask.
133
149
  *
134
- * @returns A Promise that resolves when the next microtask queue is empty.
150
+ * @returns A Promise that resolves when the microtask is executed.
135
151
  *
136
152
  * @example
137
153
  * ```typescript
154
+ * console.log('Start');
138
155
  * await delay.nextMicrotask();
156
+ * console.log('End - immediately after current task');
139
157
  * ```
140
158
  */
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
- }
159
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
160
+ nextMicrotask: () => Promise.resolve().then(() => {
161
+ })
150
162
  };
151
163
  // Annotate the CommonJS export names for ESM import in node:
152
164
  0 && (module.exports = {
153
- delay
165
+ delay,
166
+ requestAnimationFrame,
167
+ requestIdleCallback
154
168
  });
155
169
  //# sourceMappingURL=main.cjs.map
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
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\n__dev_mode__: packageTracer.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 {getGlobalThis, type GlobalThis} from '@alwatr/global-this';\n\nexport const win = /* #__PURE__ */ getGlobalThis<DictionaryOpt<unknown>>();\n\n// prettier-ignore\nconst requestAnimationFrameFallback =\n (callback: FrameRequestCallback): ReturnType<typeof setTimeout> =>\n setTimeout(() => callback(Date.now()), 1000 / 60);\n\nexport const requestAnimationFrame: GlobalThis['requestAnimationFrame'] = /* #__PURE__ */ (() =>\n win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback)();\n\n// prettier-ignore\nconst requestIdleCallbackFallback =\n (callback: () => void, options?: IdleRequestOptions): ReturnType<typeof setTimeout> =>\n setTimeout(callback, options?.timeout ?? 2000);\n\nexport const requestIdleCallback: GlobalThis['requestIdleCallback'] = /* #__PURE__ */ (() =>\n win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback)();\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;AAC5B,4BAA2C;;;ACD3C,yBAA6C;AAEtC,IAAM,MAAsB,sDAAsC;AAGzE,IAAM,gCACJ,CAAC,aACC,WAAW,MAAM,SAAS,KAAK,IAAI,CAAC,GAAG,MAAO,EAAE;AAE7C,IAAM,wBAA8E,uBACzF,IAAI,yBAAyB,IAAI,+BAA+B,IAAI,4BAA4B,+BAA+B;AAGjI,IAAM,8BACJ,CAAC,UAAsB,YACrB,WAAW,UAAU,SAAS,WAAW,GAAI;AAE1C,IAAM,sBAA0E,uBACrF,IAAI,uBAAuB,IAAI,6BAA6B,IAAI,0BAA0B,6BAA6B;;;ADfzH,aAAc,qCAAc,IAAI,iBAAkB,QAAmB;AAO9D,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;",
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\nimport {requestAnimationFrame, requestIdleCallback} from './polyfill.js';\n\nexport {requestAnimationFrame, requestIdleCallback};\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * A utility module to help manage asynchronous operations and waiting for events or timeouts.\n */\nexport const delay = {\n /**\n * Pauses execution for a specified duration.\n *\n * @param duration The duration to wait. Can be a number in milliseconds or a string like '2s', '100ms'.\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 * await delay.by('2s'); // Wait for 2 seconds\n * ```\n */\n by: (duration: Duration): Promise<void> => new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),\n\n /**\n * Pauses execution until the next animation frame.\n *\n * @returns A Promise that resolves with the high-resolution timestamp of the next animation frame.\n *\n * @example\n * ```typescript\n * const timestamp = await delay.animationFrame();\n * console.log(`Next frame at ${timestamp}`);\n * ```\n */\n animationFrame: (): Promise<DOMHighResTimeStamp> => new Promise((resolve) => requestAnimationFrame(resolve)),\n\n /**\n * Pauses execution until the browser is idle.\n *\n * @param timeout An optional maximum duration to wait.\n * @returns A Promise that resolves with an `IdleDeadline` object.\n *\n * @example\n * ```typescript\n * const deadline = await delay.idleCallback({ timeout: 2000 });\n * if (deadline.didTimeout) {\n * console.log('Idle callback timed out.');\n * }\n * ```\n */\n idleCallback: (options?: IdleRequestOptions): Promise<IdleDeadline> => new Promise((resolve) => requestIdleCallback(resolve, options)),\n\n /**\n * Pauses execution until a specific DOM event is dispatched on an element.\n *\n * @param element The HTMLElement to listen on.\n * @param eventName The name of the event to wait for.\n * @param options Optional event listener options.\n * @template T The event map type for the element.\n * @returns A Promise that resolves with the triggered event object.\n *\n * @example\n * ```typescript\n * const button = document.getElementById('my-button');\n * if (button) {\n * const clickEvent = await delay.domEvent(button, 'click');\n * console.log('Button clicked!', clickEvent);\n * }\n * ```\n */\n domEvent: <T extends keyof HTMLElementEventMap>(\n element: HTMLElement,\n eventName: T,\n options: AddEventListenerOptions = {passive: true},\n ): Promise<HTMLElementEventMap[T]> =>\n new Promise((resolve) =>\n element.addEventListener(eventName, resolve, {\n ...options,\n once: true,\n }),\n ),\n\n /**\n * Pauses execution until a specific event is dispatched on any event target.\n *\n * @param target The event target (e.g., window, document, or a custom event emitter).\n * @param eventName The name of the event to wait for.\n * @param options Optional event listener options.\n * @returns A Promise that resolves with the triggered event object.\n *\n * @example\n * ```typescript\n * const resizeEvent = await delay.event(window, 'resize');\n * console.log('Window resized:', resizeEvent);\n * ```\n */\n event: (target: EventTarget, eventName: string, options: AddEventListenerOptions = {passive: true}): Promise<Event> =>\n new Promise((resolve) =>\n target.addEventListener(eventName, resolve, {\n ...options,\n once: true,\n }),\n ),\n\n /**\n * Schedules a macrotask to run after the current event loop task completes.\n * Uses `setTimeout(..., 0)`.\n *\n * @returns A Promise that resolves when the macrotask is executed.\n *\n * @example\n * ```typescript\n * console.log('Start');\n * await delay.nextMacrotask();\n * console.log('End - after current task');\n * ```\n */\n nextMacrotask: (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0)),\n\n /**\n * Queues a microtask to run after the current task completes but before the next macrotask.\n *\n * @returns A Promise that resolves when the microtask is executed.\n *\n * @example\n * ```typescript\n * console.log('Start');\n * await delay.nextMicrotask();\n * console.log('End - immediately after current task');\n * ```\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n nextMicrotask: (): Promise<void> => Promise.resolve().then(() => {}),\n} as const;\n", "import {getGlobalThis} from '@alwatr/global-this';\n\nconst globalThis = /* #__PURE__ */ getGlobalThis<DictionaryOpt<unknown>>();\n\n/**\n * Ensures compatibility for `requestAnimationFrame` by using the native API\n * available in `globalThis`. If it's not available, it falls back to a `setTimeout`\n * call that aims for a 60 frames per second refresh rate.\n *\n * @param callback The function to call when it's time to update your animation for the next repaint.\n * @returns A long integer value, the request ID, that uniquely identifies the entry in the callback list.\n */\nexport const requestAnimationFrame: (callback: FrameRequestCallback) => number =\n /* #__PURE__ */ globalThis.requestAnimationFrame?.bind(globalThis) ??\n /* #__PURE__ */ ((callback: FrameRequestCallback) => setTimeout(() => callback(performance.now()), 1000 / 60));\n\n/**\n * Ensures compatibility for `requestIdleCallback` by using the native API.\n * If unavailable, it falls back to a `setTimeout` that executes the callback\n * after a short delay, providing a mock `IdleDeadline` object.\n *\n * The mock `IdleDeadline` gives the task a 50ms budget to run.\n *\n * @param callback A reference to a function that should be called in the near future, when the event loop is idle.\n * @param options An optional object with configuration parameters.\n * @returns An ID which can be used to cancel the callback by calling `cancelIdleCallback()`.\n */\nexport const requestIdleCallback: (callback: (deadline: IdleDeadline) => void, options?: IdleRequestOptions) => number =\n /* #__PURE__ */ globalThis.requestIdleCallback?.bind(globalThis) ??\n /* #__PURE__ */ ((\n callback: (deadline: IdleDeadline) => void,\n // options is not used in the fallback but kept for API consistency\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n options?: IdleRequestOptions,\n ) => {\n const startTime = Date.now();\n return setTimeout(() => {\n callback({\n didTimeout: !!options?.timeout,\n timeRemaining: () => Math.max(0, 50 - (Date.now() - startTime)),\n });\n }, options?.timeout ?? 20);\n });\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;AAC5B,4BAA2C;;;ACD3C,yBAA4B;AAE5B,IAAM,aAA6B,sDAAsC;AAUlE,IAAM,wBACK,2BAAW,uBAAuB,KAAK,UAAU,MAChD,CAAC,aAAmC,WAAW,MAAM,SAAS,YAAY,IAAI,CAAC,GAAG,MAAO,EAAE;AAavG,IAAM,sBACK,2BAAW,qBAAqB,KAAK,UAAU,MAC9C,CACf,UAGA,YACG;AACH,QAAM,YAAY,KAAK,IAAI;AAC3B,SAAO,WAAW,MAAM;AACtB,aAAS;AAAA,MACP,YAAY,CAAC,CAAC,SAAS;AAAA,MACvB,eAAe,MAAM,KAAK,IAAI,GAAG,MAAM,KAAK,IAAI,IAAI,UAAU;AAAA,IAChE,CAAC;AAAA,EACH,GAAG,SAAS,WAAW,EAAE;AAC3B;;;ADnCF,aAAc,qCA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AAS,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAenF,eAAe,MAAqB,QAAQ,QAAQ,EAAE,KAAK,MAAM;AAAA,EAAC,CAAC;AACrE;",
6
6
  "names": []
7
7
  }
package/dist/main.d.ts CHANGED
@@ -1,92 +1,108 @@
1
1
  import { type Duration } from '@alwatr/parse-duration';
2
+ import { requestAnimationFrame, requestIdleCallback } from './polyfill.js';
3
+ export { requestAnimationFrame, requestIdleCallback };
2
4
  /**
3
5
  * A utility module to help manage asynchronous operations and waiting for events or timeouts.
4
6
  */
5
7
  export declare const delay: {
6
8
  /**
7
- * Delays execution for a specified duration (in milliseconds).
9
+ * Pauses execution for a specified duration.
8
10
  *
9
- * @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.
11
+ * @param duration The duration to wait. Can be a number in milliseconds or a string like '2s', '100ms'.
10
12
  * @returns A Promise that resolves after the specified duration.
11
13
  *
12
14
  * @example
13
15
  * ```typescript
14
16
  * await delay.by('1m'); // Wait for 1 minute
17
+ * await delay.by('2s'); // Wait for 2 seconds
15
18
  * ```
16
19
  */
17
20
  readonly by: (duration: Duration) => Promise<void>;
18
21
  /**
19
- * Delays execution until the next animation frame.
22
+ * Pauses execution until the next animation frame.
20
23
  *
21
- * @returns A Promise that resolves with the current timestamp when the next animation frame is fired.
24
+ * @returns A Promise that resolves with the high-resolution timestamp of the next animation frame.
22
25
  *
23
26
  * @example
24
27
  * ```typescript
25
- * const timestamp = await delay.untilNextAnimationFrame();
28
+ * const timestamp = await delay.animationFrame();
29
+ * console.log(`Next frame at ${timestamp}`);
26
30
  * ```
27
31
  */
28
- readonly untilNextAnimationFrame: () => Promise<DOMHighResTimeStamp>;
32
+ readonly animationFrame: () => Promise<DOMHighResTimeStamp>;
29
33
  /**
30
- * Delays execution until the browser's idle period or the specified timeout.
34
+ * Pauses execution until the browser is idle.
31
35
  *
32
- * @param timeout - Optional timeout (in milliseconds) for the idle callback.
33
- * @returns A Promise that resolves with the IdleDeadline object when the browser is idle or the timeout is reached.
36
+ * @param timeout An optional maximum duration to wait.
37
+ * @returns A Promise that resolves with an `IdleDeadline` object.
34
38
  *
35
39
  * @example
36
40
  * ```typescript
37
- * const deadline = await delay.untilIdle();
41
+ * const deadline = await delay.idleCallback({ timeout: 2000 });
42
+ * if (deadline.didTimeout) {
43
+ * console.log('Idle callback timed out.');
44
+ * }
38
45
  * ```
39
46
  */
40
- readonly untilIdle: (timeout?: Duration) => Promise<IdleDeadline>;
47
+ readonly idleCallback: (options?: IdleRequestOptions) => Promise<IdleDeadline>;
41
48
  /**
42
- * Delays execution until a specific DOM event occurs on an HTMLElement.
49
+ * Pauses execution until a specific DOM event is dispatched on an element.
43
50
  *
44
- * @param element - The HTMLElement to listen for the event on.
45
- * @param eventName - The name of the DOM event to wait for.
46
- * @template T The event map type.
47
- * @returns A Promise that resolves with the event object when the specified event occurs.
51
+ * @param element The HTMLElement to listen on.
52
+ * @param eventName The name of the event to wait for.
53
+ * @param options Optional event listener options.
54
+ * @template T The event map type for the element.
55
+ * @returns A Promise that resolves with the triggered event object.
48
56
  *
49
57
  * @example
50
58
  * ```typescript
51
- * const clickEvent = await delay.untilDomEvent(document.body, 'click');
59
+ * const button = document.getElementById('my-button');
60
+ * if (button) {
61
+ * const clickEvent = await delay.domEvent(button, 'click');
62
+ * console.log('Button clicked!', clickEvent);
63
+ * }
52
64
  * ```
53
65
  */
54
- readonly untilDomEvent: <T extends keyof HTMLElementEventMap>(element: HTMLElement, eventName: T) => Promise<HTMLElementEventMap[T]>;
66
+ readonly domEvent: <T extends keyof HTMLElementEventMap>(element: HTMLElement, eventName: T, options?: AddEventListenerOptions) => Promise<HTMLElementEventMap[T]>;
55
67
  /**
56
- * Delays execution until a specific event occurs on an object with an `addEventListener` method.
68
+ * Pauses execution until a specific event is dispatched on any event target.
57
69
  *
58
- * @param target - The target object to listen for the event on.
59
- * @param eventName - The name of the event to wait for.
60
- * @returns A Promise that resolves with the event object when the specified event occurs.
70
+ * @param target The event target (e.g., window, document, or a custom event emitter).
71
+ * @param eventName The name of the event to wait for.
72
+ * @param options Optional event listener options.
73
+ * @returns A Promise that resolves with the triggered event object.
61
74
  *
62
75
  * @example
63
76
  * ```typescript
64
- * const server = http.createServer();
65
- * const requestEvent = await delay.untilEvent(server, 'request');
77
+ * const resizeEvent = await delay.event(window, 'resize');
78
+ * console.log('Window resized:', resizeEvent);
66
79
  * ```
67
80
  */
68
- readonly untilEvent: (target: HasAddEventListener, eventName: string) => Promise<Event>;
81
+ readonly event: (target: EventTarget, eventName: string, options?: AddEventListenerOptions) => Promise<Event>;
69
82
  /**
70
- * Yields control to the event loop immediately.
83
+ * Schedules a macrotask to run after the current event loop task completes.
84
+ * Uses `setTimeout(..., 0)`.
71
85
  *
72
- * Uses `setImmediate` if available, falls back to `queueMicrotask`, and then to `setTimeout(0)`.
73
- *
74
- * @returns A Promise that resolves immediately after yielding control to the event loop.
86
+ * @returns A Promise that resolves when the macrotask is executed.
75
87
  *
76
88
  * @example
77
89
  * ```typescript
78
- * await delay.immediate();
90
+ * console.log('Start');
91
+ * await delay.nextMacrotask();
92
+ * console.log('End - after current task');
79
93
  * ```
80
94
  */
81
- readonly immediate: () => Promise<void>;
95
+ readonly nextMacrotask: () => Promise<void>;
82
96
  /**
83
- * Delays execution until the next microtask queue is empty
97
+ * Queues a microtask to run after the current task completes but before the next macrotask.
84
98
  *
85
- * @returns A Promise that resolves when the next microtask queue is empty.
99
+ * @returns A Promise that resolves when the microtask is executed.
86
100
  *
87
101
  * @example
88
102
  * ```typescript
103
+ * console.log('Start');
89
104
  * await delay.nextMicrotask();
105
+ * console.log('End - immediately after current task');
90
106
  * ```
91
107
  */
92
108
  readonly nextMicrotask: () => Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,QAAQ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AQlC;;;;;;;;;;;;;OAaG;6BACa,WAAW,aAAa,MAAM,YAAW,uBAAuB,KAAqB,OAAO,CAAC,KAAK,CAAC;IAQnH;;;;;;;;;;;;OAYG;kCACgB,OAAO,CAAC,IAAI,CAAC;IAEhC;;;;;;;;;;;OAWG;kCAEgB,OAAO,CAAC,IAAI,CAAC;CACxB,CAAC"}
package/dist/main.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /* @alwatr/delay v5.5.10 */
1
+ /* @alwatr/delay v6.0.0 */
2
2
 
3
3
  // src/main.ts
4
4
  import { packageTracer } from "@alwatr/package-tracer";
@@ -6,126 +6,138 @@ import { parseDuration } from "@alwatr/parse-duration";
6
6
 
7
7
  // src/polyfill.ts
8
8
  import { getGlobalThis } from "@alwatr/global-this";
9
- var win = /* @__PURE__ */ getGlobalThis();
10
- var requestAnimationFrameFallback = (callback) => setTimeout(() => callback(Date.now()), 1e3 / 60);
11
- var requestAnimationFrame = /* @__PURE__ */ (() => win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback)();
12
- var requestIdleCallbackFallback = (callback, options) => setTimeout(callback, options?.timeout ?? 2e3);
13
- var requestIdleCallback = /* @__PURE__ */ (() => win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback)();
9
+ var globalThis = /* @__PURE__ */ getGlobalThis();
10
+ var requestAnimationFrame = /* @__PURE__ */ globalThis.requestAnimationFrame?.bind(globalThis) ?? ((callback) => setTimeout(() => callback(performance.now()), 1e3 / 60));
11
+ var requestIdleCallback = /* @__PURE__ */ globalThis.requestIdleCallback?.bind(globalThis) ?? ((callback, options) => {
12
+ const startTime = Date.now();
13
+ return setTimeout(() => {
14
+ callback({
15
+ didTimeout: !!options?.timeout,
16
+ timeRemaining: () => Math.max(0, 50 - (Date.now() - startTime))
17
+ });
18
+ }, options?.timeout ?? 20);
19
+ });
14
20
 
15
21
  // src/main.ts
16
- __dev_mode__: packageTracer.add("@alwatr/delay", "5.5.10");
22
+ __dev_mode__: packageTracer.add("@alwatr/delay", "6.0.0");
17
23
  var delay = {
18
24
  /**
19
- * Delays execution for a specified duration (in milliseconds).
25
+ * Pauses execution for a specified duration.
20
26
  *
21
- * @param duration - The duration to wait (in milliseconds). Use `0` to yield control to the event loop.
27
+ * @param duration The duration to wait. Can be a number in milliseconds or a string like '2s', '100ms'.
22
28
  * @returns A Promise that resolves after the specified duration.
23
29
  *
24
30
  * @example
25
31
  * ```typescript
26
32
  * await delay.by('1m'); // Wait for 1 minute
33
+ * await delay.by('2s'); // Wait for 2 seconds
27
34
  * ```
28
35
  */
29
36
  by: (duration) => new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),
30
37
  /**
31
- * Delays execution until the next animation frame.
38
+ * Pauses execution until the next animation frame.
32
39
  *
33
- * @returns A Promise that resolves with the current timestamp when the next animation frame is fired.
40
+ * @returns A Promise that resolves with the high-resolution timestamp of the next animation frame.
34
41
  *
35
42
  * @example
36
43
  * ```typescript
37
- * const timestamp = await delay.untilNextAnimationFrame();
44
+ * const timestamp = await delay.animationFrame();
45
+ * console.log(`Next frame at ${timestamp}`);
38
46
  * ```
39
47
  */
40
- untilNextAnimationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
48
+ animationFrame: () => new Promise((resolve) => requestAnimationFrame(resolve)),
41
49
  /**
42
- * Delays execution until the browser's idle period or the specified timeout.
50
+ * Pauses execution until the browser is idle.
43
51
  *
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.
52
+ * @param timeout An optional maximum duration to wait.
53
+ * @returns A Promise that resolves with an `IdleDeadline` object.
46
54
  *
47
55
  * @example
48
56
  * ```typescript
49
- * const deadline = await delay.untilIdle();
57
+ * const deadline = await delay.idleCallback({ timeout: 2000 });
58
+ * if (deadline.didTimeout) {
59
+ * console.log('Idle callback timed out.');
60
+ * }
50
61
  * ```
51
62
  */
52
- untilIdle: (timeout) => new Promise((resolve) => requestIdleCallback(resolve, timeout === void 0 ? void 0 : {
53
- timeout: parseDuration(timeout)
54
- })),
63
+ idleCallback: (options) => new Promise((resolve) => requestIdleCallback(resolve, options)),
55
64
  /**
56
- * Delays execution until a specific DOM event occurs on an HTMLElement.
65
+ * Pauses execution until a specific DOM event is dispatched on an element.
57
66
  *
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.
67
+ * @param element The HTMLElement to listen on.
68
+ * @param eventName The name of the event to wait for.
69
+ * @param options Optional event listener options.
70
+ * @template T The event map type for the element.
71
+ * @returns A Promise that resolves with the triggered event object.
62
72
  *
63
73
  * @example
64
74
  * ```typescript
65
- * const clickEvent = await delay.untilDomEvent(document.body, 'click');
75
+ * const button = document.getElementById('my-button');
76
+ * if (button) {
77
+ * const clickEvent = await delay.domEvent(button, 'click');
78
+ * console.log('Button clicked!', clickEvent);
79
+ * }
66
80
  * ```
67
81
  */
68
- untilDomEvent: (element, eventName) => new Promise(
69
- (resolve) => element.addEventListener(eventName, resolve, { once: true, passive: true })
82
+ domEvent: (element, eventName, options = { passive: true }) => new Promise(
83
+ (resolve) => element.addEventListener(eventName, resolve, {
84
+ ...options,
85
+ once: true
86
+ })
70
87
  ),
71
88
  /**
72
- * Delays execution until a specific event occurs on an object with an `addEventListener` method.
89
+ * Pauses execution until a specific event is dispatched on any event target.
73
90
  *
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.
91
+ * @param target The event target (e.g., window, document, or a custom event emitter).
92
+ * @param eventName The name of the event to wait for.
93
+ * @param options Optional event listener options.
94
+ * @returns A Promise that resolves with the triggered event object.
77
95
  *
78
96
  * @example
79
97
  * ```typescript
80
- * const server = http.createServer();
81
- * const requestEvent = await delay.untilEvent(server, 'request');
98
+ * const resizeEvent = await delay.event(window, 'resize');
99
+ * console.log('Window resized:', resizeEvent);
82
100
  * ```
83
101
  */
84
- untilEvent: (target, eventName) => new Promise(
85
- (resolve) => target.addEventListener(eventName, resolve, { once: true, passive: true })
102
+ event: (target, eventName, options = { passive: true }) => new Promise(
103
+ (resolve) => target.addEventListener(eventName, resolve, {
104
+ ...options,
105
+ once: true
106
+ })
86
107
  ),
87
108
  /**
88
- * Yields control to the event loop immediately.
109
+ * Schedules a macrotask to run after the current event loop task completes.
110
+ * Uses `setTimeout(..., 0)`.
89
111
  *
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.
112
+ * @returns A Promise that resolves when the macrotask is executed.
93
113
  *
94
114
  * @example
95
115
  * ```typescript
96
- * await delay.immediate();
116
+ * console.log('Start');
117
+ * await delay.nextMacrotask();
118
+ * console.log('End - after current task');
97
119
  * ```
98
120
  */
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
- },
121
+ nextMacrotask: () => new Promise((resolve) => setTimeout(resolve, 0)),
108
122
  /**
109
- * Delays execution until the next microtask queue is empty
123
+ * Queues a microtask to run after the current task completes but before the next macrotask.
110
124
  *
111
- * @returns A Promise that resolves when the next microtask queue is empty.
125
+ * @returns A Promise that resolves when the microtask is executed.
112
126
  *
113
127
  * @example
114
128
  * ```typescript
129
+ * console.log('Start');
115
130
  * await delay.nextMicrotask();
131
+ * console.log('End - immediately after current task');
116
132
  * ```
117
133
  */
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
- }
134
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
135
+ nextMicrotask: () => Promise.resolve().then(() => {
136
+ })
127
137
  };
128
138
  export {
129
- delay
139
+ delay,
140
+ requestAnimationFrame,
141
+ requestIdleCallback
130
142
  };
131
143
  //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
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\n__dev_mode__: packageTracer.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 {getGlobalThis, type GlobalThis} from '@alwatr/global-this';\n\nexport const win = /* #__PURE__ */ getGlobalThis<DictionaryOpt<unknown>>();\n\n// prettier-ignore\nconst requestAnimationFrameFallback =\n (callback: FrameRequestCallback): ReturnType<typeof setTimeout> =>\n setTimeout(() => callback(Date.now()), 1000 / 60);\n\nexport const requestAnimationFrame: GlobalThis['requestAnimationFrame'] = /* #__PURE__ */ (() =>\n win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || requestAnimationFrameFallback)();\n\n// prettier-ignore\nconst requestIdleCallbackFallback =\n (callback: () => void, options?: IdleRequestOptions): ReturnType<typeof setTimeout> =>\n setTimeout(callback, options?.timeout ?? 2000);\n\nexport const requestIdleCallback: GlobalThis['requestIdleCallback'] = /* #__PURE__ */ (() =>\n win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback)();\n"],
5
- "mappings": ";;;AAAA,SAAQ,qBAAoB;AAC5B,SAAQ,qBAAmC;;;ACD3C,SAAQ,qBAAqC;AAEtC,IAAM,MAAsB,8BAAsC;AAGzE,IAAM,gCACJ,CAAC,aACC,WAAW,MAAM,SAAS,KAAK,IAAI,CAAC,GAAG,MAAO,EAAE;AAE7C,IAAM,wBAA8E,uBACzF,IAAI,yBAAyB,IAAI,+BAA+B,IAAI,4BAA4B,+BAA+B;AAGjI,IAAM,8BACJ,CAAC,UAAsB,YACrB,WAAW,UAAU,SAAS,WAAW,GAAI;AAE1C,IAAM,sBAA0E,uBACrF,IAAI,uBAAuB,IAAI,6BAA6B,IAAI,0BAA0B,6BAA6B;;;ADfzH,aAAc,eAAc,IAAI,iBAAkB,QAAmB;AAO9D,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,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IACnB;AACA,WAAO,IAAI,QAAQ,CAAC,YAAY,eAAe,OAAO,CAAC;AAAA,EACzD;AACF;",
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\nimport {requestAnimationFrame, requestIdleCallback} from './polyfill.js';\n\nexport {requestAnimationFrame, requestIdleCallback};\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * A utility module to help manage asynchronous operations and waiting for events or timeouts.\n */\nexport const delay = {\n /**\n * Pauses execution for a specified duration.\n *\n * @param duration The duration to wait. Can be a number in milliseconds or a string like '2s', '100ms'.\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 * await delay.by('2s'); // Wait for 2 seconds\n * ```\n */\n by: (duration: Duration): Promise<void> => new Promise((resolve) => setTimeout(resolve, parseDuration(duration))),\n\n /**\n * Pauses execution until the next animation frame.\n *\n * @returns A Promise that resolves with the high-resolution timestamp of the next animation frame.\n *\n * @example\n * ```typescript\n * const timestamp = await delay.animationFrame();\n * console.log(`Next frame at ${timestamp}`);\n * ```\n */\n animationFrame: (): Promise<DOMHighResTimeStamp> => new Promise((resolve) => requestAnimationFrame(resolve)),\n\n /**\n * Pauses execution until the browser is idle.\n *\n * @param timeout An optional maximum duration to wait.\n * @returns A Promise that resolves with an `IdleDeadline` object.\n *\n * @example\n * ```typescript\n * const deadline = await delay.idleCallback({ timeout: 2000 });\n * if (deadline.didTimeout) {\n * console.log('Idle callback timed out.');\n * }\n * ```\n */\n idleCallback: (options?: IdleRequestOptions): Promise<IdleDeadline> => new Promise((resolve) => requestIdleCallback(resolve, options)),\n\n /**\n * Pauses execution until a specific DOM event is dispatched on an element.\n *\n * @param element The HTMLElement to listen on.\n * @param eventName The name of the event to wait for.\n * @param options Optional event listener options.\n * @template T The event map type for the element.\n * @returns A Promise that resolves with the triggered event object.\n *\n * @example\n * ```typescript\n * const button = document.getElementById('my-button');\n * if (button) {\n * const clickEvent = await delay.domEvent(button, 'click');\n * console.log('Button clicked!', clickEvent);\n * }\n * ```\n */\n domEvent: <T extends keyof HTMLElementEventMap>(\n element: HTMLElement,\n eventName: T,\n options: AddEventListenerOptions = {passive: true},\n ): Promise<HTMLElementEventMap[T]> =>\n new Promise((resolve) =>\n element.addEventListener(eventName, resolve, {\n ...options,\n once: true,\n }),\n ),\n\n /**\n * Pauses execution until a specific event is dispatched on any event target.\n *\n * @param target The event target (e.g., window, document, or a custom event emitter).\n * @param eventName The name of the event to wait for.\n * @param options Optional event listener options.\n * @returns A Promise that resolves with the triggered event object.\n *\n * @example\n * ```typescript\n * const resizeEvent = await delay.event(window, 'resize');\n * console.log('Window resized:', resizeEvent);\n * ```\n */\n event: (target: EventTarget, eventName: string, options: AddEventListenerOptions = {passive: true}): Promise<Event> =>\n new Promise((resolve) =>\n target.addEventListener(eventName, resolve, {\n ...options,\n once: true,\n }),\n ),\n\n /**\n * Schedules a macrotask to run after the current event loop task completes.\n * Uses `setTimeout(..., 0)`.\n *\n * @returns A Promise that resolves when the macrotask is executed.\n *\n * @example\n * ```typescript\n * console.log('Start');\n * await delay.nextMacrotask();\n * console.log('End - after current task');\n * ```\n */\n nextMacrotask: (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0)),\n\n /**\n * Queues a microtask to run after the current task completes but before the next macrotask.\n *\n * @returns A Promise that resolves when the microtask is executed.\n *\n * @example\n * ```typescript\n * console.log('Start');\n * await delay.nextMicrotask();\n * console.log('End - immediately after current task');\n * ```\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n nextMicrotask: (): Promise<void> => Promise.resolve().then(() => {}),\n} as const;\n", "import {getGlobalThis} from '@alwatr/global-this';\n\nconst globalThis = /* #__PURE__ */ getGlobalThis<DictionaryOpt<unknown>>();\n\n/**\n * Ensures compatibility for `requestAnimationFrame` by using the native API\n * available in `globalThis`. If it's not available, it falls back to a `setTimeout`\n * call that aims for a 60 frames per second refresh rate.\n *\n * @param callback The function to call when it's time to update your animation for the next repaint.\n * @returns A long integer value, the request ID, that uniquely identifies the entry in the callback list.\n */\nexport const requestAnimationFrame: (callback: FrameRequestCallback) => number =\n /* #__PURE__ */ globalThis.requestAnimationFrame?.bind(globalThis) ??\n /* #__PURE__ */ ((callback: FrameRequestCallback) => setTimeout(() => callback(performance.now()), 1000 / 60));\n\n/**\n * Ensures compatibility for `requestIdleCallback` by using the native API.\n * If unavailable, it falls back to a `setTimeout` that executes the callback\n * after a short delay, providing a mock `IdleDeadline` object.\n *\n * The mock `IdleDeadline` gives the task a 50ms budget to run.\n *\n * @param callback A reference to a function that should be called in the near future, when the event loop is idle.\n * @param options An optional object with configuration parameters.\n * @returns An ID which can be used to cancel the callback by calling `cancelIdleCallback()`.\n */\nexport const requestIdleCallback: (callback: (deadline: IdleDeadline) => void, options?: IdleRequestOptions) => number =\n /* #__PURE__ */ globalThis.requestIdleCallback?.bind(globalThis) ??\n /* #__PURE__ */ ((\n callback: (deadline: IdleDeadline) => void,\n // options is not used in the fallback but kept for API consistency\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n options?: IdleRequestOptions,\n ) => {\n const startTime = Date.now();\n return setTimeout(() => {\n callback({\n didTimeout: !!options?.timeout,\n timeRemaining: () => Math.max(0, 50 - (Date.now() - startTime)),\n });\n }, options?.timeout ?? 20);\n });\n"],
5
+ "mappings": ";;;AAAA,SAAQ,qBAAoB;AAC5B,SAAQ,qBAAmC;;;ACD3C,SAAQ,qBAAoB;AAE5B,IAAM,aAA6B,8BAAsC;AAUlE,IAAM,wBACK,2BAAW,uBAAuB,KAAK,UAAU,MAChD,CAAC,aAAmC,WAAW,MAAM,SAAS,YAAY,IAAI,CAAC,GAAG,MAAO,EAAE;AAavG,IAAM,sBACK,2BAAW,qBAAqB,KAAK,UAAU,MAC9C,CACf,UAGA,YACG;AACH,QAAM,YAAY,KAAK,IAAI;AAC3B,SAAO,WAAW,MAAM;AACtB,aAAS;AAAA,MACP,YA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,MAAM;AAAA,IACR,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeF,eAAe,MAAqB,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,CAAC,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAenF,eAAe,MAAqB,QAAQ,QAAQ,EAAE,KAAK,MAAM;AAAA,EAAC,CAAC;AACrE;",
6
6
  "names": []
7
7
  }
@@ -1,5 +1,22 @@
1
- import { type GlobalThis } from '@alwatr/global-this';
2
- export declare const win: typeof globalThis & DictionaryOpt<unknown>;
3
- export declare const requestAnimationFrame: GlobalThis['requestAnimationFrame'];
4
- export declare const requestIdleCallback: GlobalThis['requestIdleCallback'];
1
+ /**
2
+ * Ensures compatibility for `requestAnimationFrame` by using the native API
3
+ * available in `globalThis`. If it's not available, it falls back to a `setTimeout`
4
+ * call that aims for a 60 frames per second refresh rate.
5
+ *
6
+ * @param callback The function to call when it's time to update your animation for the next repaint.
7
+ * @returns A long integer value, the request ID, that uniquely identifies the entry in the callback list.
8
+ */
9
+ export declare const requestAnimationFrame: (callback: FrameRequestCallback) => number;
10
+ /**
11
+ * Ensures compatibility for `requestIdleCallback` by using the native API.
12
+ * If unavailable, it falls back to a `setTimeout` that executes the callback
13
+ * after a short delay, providing a mock `IdleDeadline` object.
14
+ *
15
+ * The mock `IdleDeadline` gives the task a 50ms budget to run.
16
+ *
17
+ * @param callback A reference to a function that should be called in the near future, when the event loop is idle.
18
+ * @param options An optional object with configuration parameters.
19
+ * @returns An ID which can be used to cancel the callback by calling `cancelIdleCallback()`.
20
+ */
21
+ export declare const requestIdleCallback: (callback: (deadline: IdleDeadline) => void, options?: IdleRequestOptions) => number;
5
22
  //# sourceMappingURL=polyfill.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"polyfill.d.ts","sourceRoot":"","sources":["../src/polyfill.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAC,MAAM,qBAAqB,CAAC;AAEnE,eAAO,MAAM,GAAG,4CAA0D,CAAC;AAO3E,eAAO,MAAM,qBAAqB,EAAE,UAAU,CAAC,uBAAuB,CAC4D,CAAC;AAOnI,eAAO,MAAM,mBAAmB,EAAE,UAAU,CAAC,qBAAqB,CACwD,CAAC"}
1
+ {"version":3,"file":"polyfill.d.ts","sourceRoot":"","sources":["../src/polyfill.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,EAAE,CAAC,QAAQ,EAAE,oBAAoB,KAAK,MAEwC,CAAC;AAEjH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,YAAY,KAAK,IAAI,EAAE,OAAO,CAAC,EAAE,kBAAkB,KAAK,MAe5G,CAAC"}
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@alwatr/delay",
3
3
  "description": "Comprehensive toolkit for managing asynchronous operations.",
4
- "version": "5.5.10",
4
+ "version": "6.0.0",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "bugs": "https://github.com/Alwatr/nanolib/issues",
7
7
  "dependencies": {
8
- "@alwatr/global-this": "5.5.8",
9
- "@alwatr/package-tracer": "5.5.8",
10
- "@alwatr/parse-duration": "5.5.9"
8
+ "@alwatr/global-this": "5.5.9",
9
+ "@alwatr/package-tracer": "5.5.9",
10
+ "@alwatr/parse-duration": "5.5.10"
11
11
  },
12
12
  "devDependencies": {
13
- "@alwatr/nano-build": "6.0.3",
13
+ "@alwatr/nano-build": "6.1.0",
14
14
  "@alwatr/prettier-config": "5.0.2",
15
- "@alwatr/tsconfig-base": "5.0.2",
16
- "@alwatr/type-helper": "5.4.4",
15
+ "@alwatr/tsconfig-base": "6.0.0",
16
+ "@alwatr/type-helper": "6.0.0",
17
17
  "@types/node": "^22.18.1",
18
18
  "typescript": "^5.9.2"
19
19
  },
@@ -77,5 +77,5 @@
77
77
  },
78
78
  "type": "module",
79
79
  "types": "./dist/main.d.ts",
80
- "gitHead": "7b799c86f93bc18db2436035fcf40b58aaf43319"
80
+ "gitHead": "5d7482af60778578d7adedf6b89a9f938e6cd776"
81
81
  }