time-queues 1.0.5 → 1.1.1

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/README.md CHANGED
@@ -3,45 +3,18 @@
3
3
  [npm-img]: https://img.shields.io/npm/v/time-queues.svg
4
4
  [npm-url]: https://npmjs.org/package/time-queues
5
5
 
6
- `time-queues` is an efficient library for organizing asynchronous multitasking and scheduled tasks.
7
- It can be used in a browser and in server-side environments like `Node`, `Deno` and `Bun`.
8
- It depends only on [list-toolkit](https://github.com/uhop/list-toolkit), which is a no-dependency library for efficient task queues.
9
6
 
10
- The following features are provided:
7
+ `time-queues` is an efficient, lightweight library for organizing asynchronous multitasking and scheduled tasks in JavaScript applications. It works seamlessly in browsers and server-side environments like Node.js, Deno, and Bun.
11
8
 
12
- * All environments:
13
- * **Scheduler**: a `MinHeap`-based task queue that schedules time-based tasks in the future.
14
- * **repeat()**: a function that creates a repeatable task.
15
- * **defer()**: a function that executes a task at a later time in the next tick.
16
- * Browsers:
17
- * Efficient multitasking:
18
- * **IdleQueue**: a task queue that executes tasks in the next idle period.
19
- * Based on [requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback).
20
- * **defer()**: a function that executes a task at a later time in the next idle period.
21
- * **FrameQueue**: a task queue that executes tasks in the next frame.
22
- * Based on [requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).
23
- * Page state management:
24
- * **PageWatcher**: a task queue that executes tasks when the page state changes.
25
- * **watchStates()**: a helper that pauses and resumes queues when the page state changes.
26
- * **whenDomLoaded()**: a helper that executes tasks when the DOM is loaded.
27
- * **whenLoaded()**: a helper that executes tasks when the page is loaded.
9
+ The library provides elegant solutions for common timing and scheduling challenges, helping developers create responsive, efficient applications that follow best practices for resource management and user experience.
28
10
 
29
- Internally it uses [List Toolkit](https://github.com/uhop/list-toolkit) and leverages the following browser APIs:
11
+ ## Key Features
30
12
 
31
- * [requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
32
- * [requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
33
- * [queueMicrotask()](https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask)
34
- * [setTimeout()](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
35
- * Various events and properties.
36
-
37
- There are many articles on the subject that detail how to leverage the APIs writing efficient applications.
38
- Some of them are:
39
-
40
- * [Background Tasks API](https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API)
41
- * [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API)
42
- * [Page Lifecycle API](https://developer.chrome.com/docs/web-platform/page-lifecycle-api)
43
-
44
- This package eliminates the need to write code that you'll write anyway following best practices.
13
+ - **Efficient Task Scheduling**: Schedule tasks to run at specific times or after delays
14
+ - **Browser Performance Optimization**: Execute tasks during idle periods or animation frames
15
+ - **Page Lifecycle Management**: Respond intelligently to page visibility and focus changes
16
+ - **Resource Management**: Control execution rates with throttling and debouncing
17
+ - **Minimal Dependencies**: Relies only on [list-toolkit](https://www.npmjs.com/package/list-toolkit), a zero-dependency library
45
18
 
46
19
  ## Installation
47
20
 
@@ -52,277 +25,35 @@ npm install time-queues
52
25
  If you want to check out the source code, you can use the following command:
53
26
 
54
27
  ```sh
55
- git clone https://github.com/uhop/time-queues.git
28
+ git clone --recurse-submodules https://github.com/uhop/time-queues.git
56
29
  cd time-queues
57
30
  npm install
58
31
  ```
59
32
 
60
- Don't forget to look at a test web application that uses the library. For that you should start a server:
61
-
62
- ```sh
63
- npm start
64
- ```
65
-
66
- And navigate to [http://localhost:3000/tests/web/](http://localhost:3000/tests/web/) —
67
- don't forget to open the console and play around: switch tabs, make other window active,
68
- navigate away and come back, and so on.
69
- See how queues work in [tests/web/test.js](https://github.com/uhop/time-queues/blob/main/tests/web/test.js).
70
-
71
33
  ## Usage
72
34
 
73
- The full documentation is available in the project's [wiki](https://github.com/uhop/time-queues/wiki). Below is a cheat sheet of the API.
74
-
75
- ### ListQueue
76
-
77
- `ListQueue` is a list-based task queue that executes tasks in the order they were added.
78
- It serves as a base class for other task queues. The following methods are available:
79
-
80
- | Method | Description |
81
- |:---|:---|
82
- | `ListQueue(paused)` | Create a new list queue (paused optionally). |
83
- | `isEmpty` | Check if the list queue is empty. |
84
- | `pause()` | Pause the list queue. |
85
- | `resume()` | Resume the list queue. |
86
- | `enqueue(fn)` | Schedule a function to be executed. Returns the task. |
87
- | `dequeue(task)` | Remove a task from the list queue. |
88
- | `clear()` | Remove all tasks from the list queue. |
89
-
90
- Subclasses should implement `startQueue()`.
35
+ The full documentation is available in the project's [wiki](https://github.com/uhop/time-queues/wiki). Below is a summary of the most important parts of the documentation:
91
36
 
92
- ### Scheduler
37
+ ### Resource Management
93
38
 
94
- `Scheduler` is a `MinHeap`-based task queue that schedules time-based tasks in the future.
95
- It can used to run periodic updates or one-time events.
39
+ - [Scheduler](https://github.com/uhop/time-queues/wiki/Scheduler): Time-based task scheduling
40
+ - [Retainer](https://github.com/uhop/time-queues/wiki/Retainer): Manage resource lifecycle
41
+ - [Throttler](https://github.com/uhop/time-queues/wiki/Throttler): Control execution rate based on keys
96
42
 
97
- `Scheduler` is not based on `ListQueue`, but implements its API.
98
- The following additional methods are available:
43
+ ### Browser-Specific Components
99
44
 
100
- | Method | Description |
101
- |:---|:---|
102
- | `Scheduler(paused)` | Create a new scheduler (paused optionally). |
103
- | `nextTime` | Get the next scheduled time or 'Infinity` if the scheduler is empty. |
104
- | `enqueue(fn, time)` | Schedule a function to be executed at a later time. Returns the task. `time` can be a date or a number in milliseconds from now. |
105
-
106
- Scheduled functions are called once with the following arguments:
107
-
108
- * `fn(task, scheduler)`, where:
109
- * `fn` — the scheduled function.
110
- * `task` — the task object that corresponds to the scheduled function.
111
- * `scheduler` — the scheduler object.
112
-
113
- The return value is ignored.
114
-
115
- The module provides a singleton ready to be used:
116
-
117
- ```js
118
- import scheduler, {repeat} from 'time-queues/Scheduler.js';
119
-
120
- // schedule a task
121
- const task = scheduler.enqueue(() => console.log('The first task'), 1000);
122
-
123
- // run a task every second
124
- const hello = () => {
125
- console.log('Hello, world!');
126
- scheduler.enqueue(hello, 1000);
127
- };
128
- scheduler.enqueue(hello, 1000);
129
-
130
- // pause the scheduler
131
- scheduler.pause();
132
-
133
- // remove the first task
134
- scheduler.dequeue(task);
135
-
136
- // remove all tasks
137
- scheduler.clear();
138
- ```
45
+ - [IdleQueue](https://github.com/uhop/time-queues/wiki/IdleQueue): Execute tasks during browser idle periods
46
+ - [FrameQueue](https://github.com/uhop/time-queues/wiki/FrameQueue): Execute tasks during animation frames
47
+ - [PageWatcher](https://github.com/uhop/time-queues/wiki/PageWatcher): Monitor and respond to page lifecycle changes
139
48
 
140
- #### repeat()
141
-
142
- The module provides a helper function `repeat()` that creates a repeatable task:
143
-
144
- * `repeat(fn, interval)`, where:
145
- * `fn` — the scheduled function.
146
- * `interval` — the interval in milliseconds. If not specified the corresponding task delay is used.
147
-
148
- We can rewrite the above example using `repeat()`:
149
-
150
- ```js
151
- // run a task every second
152
- scheduler.enqueue(repeat(() => console.log('Hello, world!'), 1000));
153
- ```
154
-
155
- ### defer()
156
-
157
- `defer(fn)` is a function that executes an argument function at a later time in the next tick.
158
-
159
- Deferred functions are called with no arguments. The return value is ignored.
160
-
161
- ```js
162
- import defer from 'time-queues/defer.js';
163
-
164
- // run a task in the next tick
165
- defer(() => console.log('Goodbye, world!'));
166
-
167
- // run code now
168
- console.log('Hello, world!');
169
- ```
170
-
171
- ### IdleQueue
172
-
173
- `IdleQueue` is a task queue that executes tasks in the next idle period. It implements `ListQueue` and is based on [requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback).
174
-
175
- Efficient web applications should use `IdleQueue` to schedule computations required to prepare data and
176
- even create necessary DOM elements.
177
- See [Background Tasks API](https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API) for more information.
178
-
179
- Queued functions are called once with the following arguments:
180
-
181
- * `fn(deadline, task, idleQueue)`, where:
182
- * `fn` — the scheduled function.
183
- * `deadline` — the deadline object. See [requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) for more information.
184
- * `task` — the task object that corresponds to the scheduled function.
185
- * `idleQueue` — the idle queue object.
186
-
187
- The return value is ignored.
188
-
189
- The module provides a singleton ready to be used:
190
-
191
- ```js
192
- import idleQueue from 'time-queues/IdleQueue.js';
193
- import frameQueue from 'time-queues/FrameQueue.js';
194
-
195
- idleQueue.enqueue(() => {
196
- // prepare our data and generate DOM
197
- const div = document.createElement('div');
198
- div.appendChild(document.createTextNode('Hello, world!'));
199
- // now update the DOM in the next frame
200
- frameQueue.enqueue(() => document.body.appendChild(div));
201
- });
202
- ```
49
+ ### Utility Functions
203
50
 
204
- ### FrameQueue
205
-
206
- `FrameQueue` is a task queue that executes tasks in the next frame. It implements `ListQueue` and is based on [requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame).
207
-
208
- Efficient web applications should use `FrameQueue` to schedule DOM updates.
209
- See [Background Tasks API](https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API) for more information.
210
-
211
- Queued functions are called once with the following arguments:
212
-
213
- * `fn(timeStamp, task, frameQueue)`, where:
214
- * `fn` — the scheduled function.
215
- * `timeStamp` — the timestamp object. See [requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) for more information.
216
- * `task` — the task object that corresponds to the scheduled function.
217
- * `frameQueue` — the frame queue object.
218
-
219
- The return value is ignored.
220
-
221
- The module provides a singleton ready to be used. See the code snippet `IdleQueue` above for more information.
222
-
223
- ### PageWatcher
224
-
225
- `PageWatcher` is a task queue that executes tasks when the page state changes. It is based on [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API).
226
- You can find more information in [Page Lifecycle API](https://developer.chrome.com/docs/web-platform/page-lifecycle-api).
227
-
228
- Efficient web applications should use `PageWatcher` to watch for page visibility changes and react accordingly, for example, by suspending updates in the hidden state.
229
-
230
- `PageWatcher` implements `ListQueue`. The following additional/changed methods are available:
231
-
232
- | Method | Description |
233
- |:---|:---|
234
- | `PageWatcher(started)` | Create a new page watcher (started optionally). |
235
- | `currentState` | Get the current page state (see below). |
236
- | `enqueue(fn, initialize)` | Schedule a function to be executed. Returns the task. If `initialize` is truthy, the function will be queued and called immediately with the current state. |
237
-
238
- A page state can be one of the following strings:
239
-
240
- * `active` — the page is a current window, it is visible and the user can interact with it.
241
- * `passive` — the page is not a current window, it is visible, but the user cannot interact with it.
242
- * `hidden` — the page is not visible.
243
- * `frozen` — the page is suspended, no timers nor fetch callbacks can be executed.
244
- * `terminated` — the page is terminated, no new tasks can be started.
245
-
246
- Queued functions are called on every state change with the following arguments:
247
-
248
- * `fn(state, previousState, task, pageWatcher)`, where:
249
- * 'fn` — the scheduled function.
250
- * `state` — the new page state.
251
- * `previousState` — the previous page state.
252
- * `task` — the task object that corresponds to the scheduled function.
253
- * `pageWatcher` — the page watcher object.
254
-
255
- The return value is ignored.
256
-
257
- The module provides a singleton ready to be used.
258
-
259
- ```js
260
- import pageWatcher from 'time-queues/PageWatcher.js';
261
-
262
- pageWatcher.enqueue(state => console.log('state:', state), true);
263
- ```
264
-
265
- #### watchStates()
266
-
267
- `watchStates()` is a helper that pauses and resumes queues when the page state changes.
268
- It can be added to a `PageWatcher` object controlling a `Scheduler` object or any other queue
269
- to pause it depending on a page state.
270
-
271
- The function signature is:
272
-
273
- * `watchStates(queue, resumeStatesList = ['active'])`, where:
274
- * `queue` — the queue object to be controlled.
275
- * `resumeStatesList` — the iterable of page states to `resume()`. All other states will pause the queue. Defaults to 'active'.
276
-
277
- The return value is a function that is suitable for `PageWatcher.enqueue()`.
278
-
279
- ```js
280
- import pageWatcher, {watchStates} from 'time-queues/PageWatcher.js';
281
- import scheduler from 'time-queues/Scheduler.js';
282
-
283
- // do not process time-based tasks when the page is not visible
284
- pageWatcher.enqueue(watchStates(scheduler, ['active', 'passive']), true);
285
- ```
286
-
287
- ### whenDomLoaded()
288
-
289
- `whenDomLoaded()` is a helper that executes a function when the DOM is loaded.
290
- If the DOM is already loaded, the function will be executed with `queueMicrotask()`.
291
- Otherwise it'll be queued and executed when the DOM is loaded.
292
- See [DOMContentLoaded](https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event) for more information.
293
-
294
- The function signature is:
295
-
296
- * `whenDomLoaded(fn)`, where:
297
- * `fn` — the function to be executed when the DOM is loaded.
298
-
299
- It will be called with no arguments. The return value is ignored.
300
-
301
- ```js
302
- import whenDomLoaded from 'time-queues/whenDomLoaded.js';
303
-
304
- whenDomLoaded(() => console.log('The DOM is loaded'));
305
- ```
306
-
307
- ### whenLoaded()
308
-
309
- `whenLoaded()` is a helper that executes a function when the page is fully loaded.
310
- If the page is already loaded, the function will be executed with `queueMicrotask()`.
311
- Otherwise it'll be queued and executed when the page is loaded.
312
- See [load](https://developer.mozilla.org/en-US/docs/Web/Events/load) for more information.
313
-
314
- The function signature is:
315
-
316
- * `whenLoaded(fn)`, where:
317
- * `fn` — the function to be executed when the page is loaded.
318
-
319
- It will be called with no arguments. The return value is ignored.
320
-
321
- ```js
322
- import whenLoaded from 'time-queues/whenLoaded.js';
323
-
324
- whenLoaded(() => console.log('The page is loaded'));
325
- ```
51
+ - [defer()](<https://github.com/uhop/time-queues/wiki/defer()>): Execute tasks in the next tick
52
+ - [sleep()](<https://github.com/uhop/time-queues/wiki/sleep()>): Promise-based delay function
53
+ - [throttle()](<https://github.com/uhop/time-queues/wiki/throttle()>): Limit function execution rate
54
+ - [debounce()](<https://github.com/uhop/time-queues/wiki/debounce()>): Delay function execution until input stabilizes
55
+ - [sample()](<https://github.com/uhop/time-queues/wiki/sample()>): Execute function at regular intervals
56
+ - [audit()](<https://github.com/uhop/time-queues/wiki/audit()>): Execute function after specified delay
326
57
 
327
58
  ## License
328
59
 
@@ -330,6 +61,8 @@ This project is licensed under the BSD-3-Clause License.
330
61
 
331
62
  ## Release History
332
63
 
64
+ * 1.1.1 *Updates to TS typings.*
65
+ * 1.1.0 *Added `Throttler`, `Retainer`, promise-based convenience time methods.*
333
66
  * 1.0.5 *Technical release: updated deps, more tests.*
334
67
  * 1.0.4 *Bug fixes and code simplifications.*
335
68
  * 1.0.3 *Updated deps (`list-toolkit`) to fix a minor bug.*
package/package.json CHANGED
@@ -1,19 +1,25 @@
1
1
  {
2
2
  "name": "time-queues",
3
- "version": "1.0.5",
3
+ "version": "1.1.1",
4
4
  "description": "Time queues to organize multitasking and scheduled tasks.",
5
5
  "type": "module",
6
+ "types": "./src/index.d.ts",
6
7
  "exports": {
7
8
  "./*": "./src/*"
8
9
  },
9
10
  "scripts": {
10
11
  "test": "tape6 --flags FO",
11
12
  "test:bun": "tape6-bun --flags FO",
12
- "test:deno-original": "tape6-deno --flags FO",
13
- "test:deno": "deno run -A `tape6-runner main` --flags FO",
13
+ "test:deno": "tape6-deno --flags FO",
14
14
  "test:proc": "tape6-proc --flags FO",
15
15
  "test:proc:bun": "bun run `npx tape6-proc --self` --flags FO",
16
16
  "test:proc:deno": "deno run -A `npx tape6-proc --self` --flags FO --runFileArgs -A",
17
+ "ts-check": "tsc --noEmit",
18
+ "ts-test": "tape6 --flags FO '/ts-tests/test-*.*ts'",
19
+ "ts-test:bun": "tape6-bun --flags FO '/ts-tests/test-*.*ts'",
20
+ "ts-test:deno": "tape6-deno --flags FO '/ts-tests/test-*.*ts'",
21
+ "lint": "prettier --check src/ tests/ wiki/",
22
+ "lint:fix": "prettier --write src/ tests/ wiki/",
17
23
  "start": "tape6-server --trace"
18
24
  },
19
25
  "repository": {
@@ -33,7 +39,9 @@
33
39
  },
34
40
  "homepage": "https://github.com/uhop/time-queues#readme",
35
41
  "files": [
36
- "/src"
42
+ "/src",
43
+ "LICENSE",
44
+ "README.md"
37
45
  ],
38
46
  "tape6": {
39
47
  "tests": [
@@ -48,8 +56,10 @@
48
56
  }
49
57
  },
50
58
  "devDependencies": {
51
- "tape-six": "^1.0.2",
52
- "tape-six-proc": "^1.0.0"
59
+ "@types/node": "^22.13.10",
60
+ "tape-six": "^1.0.4",
61
+ "tape-six-proc": "^1.0.1",
62
+ "typescript": "^5.8.2"
53
63
  },
54
64
  "dependencies": {
55
65
  "list-toolkit": "^2.2.1"
@@ -0,0 +1,76 @@
1
+ import {ListQueue, Task} from './ListQueue';
2
+
3
+ /**
4
+ * A queue based on [requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame).
5
+ * Used in an animation loop or for drawing updates.
6
+ */
7
+ export declare class FrameQueue extends ListQueue {
8
+ /**
9
+ * Whether the queue is paused.
10
+ */
11
+ paused: boolean;
12
+
13
+ /**
14
+ * The batch size in milliseconds for running tasks.
15
+ * If `undefined`, all tasks are run in a single frame.
16
+ */
17
+ batch: number | undefined;
18
+
19
+ /**
20
+ * Creates a new frame queue.
21
+ * @param paused Whether the queue should start paused.
22
+ * @param batchInMs The batch size in milliseconds.
23
+ */
24
+ constructor(paused?: boolean, batchInMs?: number);
25
+
26
+ /**
27
+ * Whether the queue is empty.
28
+ */
29
+ get isEmpty(): boolean;
30
+
31
+ /**
32
+ * Enqueues a task.
33
+ * @param fn The function to execute.
34
+ * @returns The task object.
35
+ */
36
+ enqueue(fn: (timeStamp: number, task: Task, queue: FrameQueue) => void): Task;
37
+
38
+ /**
39
+ * Dequeues a task.
40
+ * @param task The task to dequeue.
41
+ * @returns The queue.
42
+ */
43
+ dequeue(task: Task): this;
44
+
45
+ /**
46
+ * Clears the queue.
47
+ * @returns The queue.
48
+ */
49
+ clear(): this;
50
+
51
+ /**
52
+ * Pauses the queue.
53
+ * @returns The queue.
54
+ */
55
+ pause(): this;
56
+
57
+ /**
58
+ * Resumes the queue.
59
+ * @returns The queue.
60
+ */
61
+ resume(): this;
62
+
63
+ /**
64
+ * Starts the queue.
65
+ * It is used internally by `resume()`.
66
+ * @returns The function that stops the queue.
67
+ */
68
+ startQueue(): (() => void) | null;
69
+ }
70
+
71
+ /**
72
+ * The default frame queue usually used as a global queue.
73
+ */
74
+ export const frameQueue: FrameQueue;
75
+
76
+ export default FrameQueue;
package/src/FrameQueue.js CHANGED
@@ -1,3 +1,5 @@
1
+ // @ts-self-types="./FrameQueue.d.ts"
2
+
1
3
  'use strict';
2
4
 
3
5
  import List from 'list-toolkit/list.js';
@@ -0,0 +1,88 @@
1
+ import {ListQueue, Task} from './ListQueue';
2
+
3
+ /**
4
+ * A queue based on [requestIdleCallback()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback).
5
+ * Used for background tasks.
6
+ */
7
+ export declare class IdleQueue extends ListQueue {
8
+ /**
9
+ * Whether the queue is paused.
10
+ */
11
+ paused: boolean;
12
+
13
+ /**
14
+ * The timeout batch size in milliseconds for running tasks.
15
+ * If `undefined`, all tasks are run in a single idle period.
16
+ * This timeout is used only when `deadline.didTimeout` passed by `requestIdleCallback()` is `true`.
17
+ */
18
+ timeoutBatch: number | undefined;
19
+
20
+ /**
21
+ * The options passed to `requestIdleCallback()`.
22
+ */
23
+ options: IdleCallbackOptions | undefined;
24
+
25
+ /**
26
+ * Creates a new idle queue.
27
+ * @param paused Whether the queue should start paused.
28
+ * @param timeoutBatchInMs The timeout batch size in milliseconds.
29
+ * @param options The options passed to `requestIdleCallback()`.
30
+ */
31
+ constructor(paused?: boolean, timeoutBatchInMs?: number, options?: IdleCallbackOptions);
32
+
33
+ /**
34
+ * Whether the queue is empty.
35
+ */
36
+ get isEmpty(): boolean;
37
+
38
+ /**
39
+ * Enqueues a task.
40
+ * @param fn The function to execute.
41
+ * @returns The task object.
42
+ */
43
+ enqueue(fn: (deadline: IdleDeadline, task: Task, queue: IdleQueue) => void): Task;
44
+
45
+ /**
46
+ * Dequeues a task.
47
+ * @param task The task to dequeue.
48
+ * @returns The queue.
49
+ */
50
+ dequeue(task: Task): this;
51
+
52
+ /**
53
+ * Clears the queue.
54
+ * @returns The queue.
55
+ */
56
+ clear(): this;
57
+
58
+ /**
59
+ * Pauses the queue.
60
+ * @returns The queue.
61
+ */
62
+ pause(): this;
63
+
64
+ /**
65
+ * Resumes the queue.
66
+ * @returns The queue.
67
+ */
68
+ resume(): this;
69
+
70
+ /**
71
+ * Starts the queue.
72
+ * It is used internally by `resume()`.
73
+ * @returns The function that stops the queue.
74
+ */
75
+ startQueue(): (() => void) | null;
76
+ }
77
+
78
+ /**
79
+ * The default idle queue usually used as a global queue.
80
+ */
81
+ export const idleQueue: IdleQueue;
82
+
83
+ /**
84
+ * A function that schedules a task to run in the next idle period.
85
+ */
86
+ export const defer: (fn: (deadline: IdleDeadline, task: Task, queue: IdleQueue) => void) => Task;
87
+
88
+ export default IdleQueue;
package/src/IdleQueue.js CHANGED
@@ -1,3 +1,5 @@
1
+ // @ts-self-types="./IdleQueue.d.ts"
2
+
1
3
  'use strict';
2
4
 
3
5
  import List from 'list-toolkit/list.js';
@@ -0,0 +1,77 @@
1
+ import {MicroTask} from './MicroTask';
2
+ import {List} from 'list-toolkit';
3
+
4
+ /**
5
+ * A list-based queue of microtasks that will be executed when scheduled.
6
+ * It is a base class for other list-based task queues.
7
+ */
8
+ export declare class ListQueue extends MicroTaskQueue {
9
+ /**
10
+ * Whether the queue is paused.
11
+ */
12
+ paused: boolean;
13
+
14
+ /**
15
+ * The function that stops the queue.
16
+ * It is used internally by `pause()` and `resume()`.
17
+ */
18
+ stopQueue: (() => void) | null;
19
+
20
+ /**
21
+ * Creates a new list queue.
22
+ * @param paused Whether the queue should start paused.
23
+ */
24
+ constructor(paused?: boolean);
25
+
26
+ /**
27
+ * Whether the queue is empty.
28
+ */
29
+ get isEmpty(): boolean;
30
+
31
+ /**
32
+ * Enqueues a microtask.
33
+ * @param fn The function to execute when the microtask is scheduled.
34
+ * @returns The enqueued microtask.
35
+ */
36
+ enqueue(fn: () => void): MicroTask;
37
+
38
+ /**
39
+ * Dequeues a microtask.
40
+ * @param task The microtask to dequeue.
41
+ * @returns The queue.
42
+ */
43
+ dequeue(task: MicroTask): this;
44
+
45
+ /**
46
+ * Clears the queue.
47
+ * @returns The queue.
48
+ */
49
+ clear(): this;
50
+
51
+ /**
52
+ * Pauses the queue.
53
+ * @returns The queue.
54
+ */
55
+ pause(): this;
56
+
57
+ /**
58
+ * Resumes the queue.
59
+ * @returns The queue.
60
+ */
61
+ resume(): this;
62
+
63
+ /**
64
+ * Starts the queue.
65
+ * It is used internally by `resume()`.
66
+ * It is meant to be overridden in subclasses.
67
+ * @returns The function that stops the queue.
68
+ */
69
+ startQueue(): (() => void) | null;
70
+ }
71
+
72
+ /**
73
+ * A task for list queues.
74
+ */
75
+ export declare type Task = MicroTask;
76
+
77
+ export default ListQueue;
package/src/ListQueue.js CHANGED
@@ -1,3 +1,5 @@
1
+ // @ts-self-types="./ListQueue.d.ts"
2
+
1
3
  'use strict';
2
4
 
3
5
  import List from 'list-toolkit/list.js';