time-queues 1.2.2 → 1.2.4
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 +2 -0
- package/package.json +6 -6
- package/src/CancelTaskError.d.ts +8 -0
- package/src/CancelTaskError.js +15 -0
- package/src/FrameQueue.d.ts +10 -1
- package/src/FrameQueue.js +2 -2
- package/src/IdleQueue.d.ts +13 -2
- package/src/IdleQueue.js +3 -3
- package/src/ListQueue.d.ts +14 -1
- package/src/ListQueue.js +5 -1
- package/src/MicroTask.d.ts +35 -2
- package/src/MicroTask.js +34 -0
- package/src/MicroTaskQueue.d.ts +10 -2
- package/src/MicroTaskQueue.js +21 -0
- package/src/PageWatcher.d.ts +6 -0
- package/src/PageWatcher.js +4 -0
- package/src/Scheduler.d.ts +45 -8
- package/src/Scheduler.js +5 -9
- package/src/audit.d.ts +3 -3
- package/src/debounce.d.ts +3 -3
- package/src/defer.d.ts +17 -1
- package/src/defer.js +15 -0
- package/src/index.d.ts +1 -0
- package/src/sample.d.ts +3 -3
- package/src/throttle.d.ts +3 -3
- package/src/when-dom-loaded.d.ts +20 -0
- package/src/when-dom-loaded.js +15 -0
- package/src/when-loaded.d.ts +20 -0
- package/src/when-loaded.js +15 -0
package/README.md
CHANGED
|
@@ -61,6 +61,8 @@ This project is licensed under the BSD-3-Clause License.
|
|
|
61
61
|
|
|
62
62
|
## Release History
|
|
63
63
|
|
|
64
|
+
- 1.2.4 _Updated dependencies._
|
|
65
|
+
- 1.2.3 _Updated dependencies._
|
|
64
66
|
- 1.2.2 _`Counter`: separated old waiter from new waiters before notifying them._
|
|
65
67
|
- 1.2.1 _Minor release: updated formal TS dependencies in `index.d.ts`._
|
|
66
68
|
- 1.2.0 _Added `Counter`. Updated dev dependencies._
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "time-queues",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "Time queues to organize multitasking and scheduled tasks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@types/node": "^
|
|
60
|
-
"tape-six": "^1.
|
|
61
|
-
"tape-six-proc": "^1.
|
|
62
|
-
"typescript": "^5.
|
|
59
|
+
"@types/node": "^25.0.10",
|
|
60
|
+
"tape-six": "^1.4.5",
|
|
61
|
+
"tape-six-proc": "^1.1.6",
|
|
62
|
+
"typescript": "^5.9.3"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"list-toolkit": "^2.2.
|
|
65
|
+
"list-toolkit": "^2.2.5"
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @ts-self-types="./CancelTaskError.d.ts"
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
export class CancelTaskError extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Task was canceled');
|
|
8
|
+
this.name = 'CancelTaskError';
|
|
9
|
+
if (Error.captureStackTrace) {
|
|
10
|
+
Error.captureStackTrace(this, CancelTaskError);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default CancelTaskError;
|
package/src/FrameQueue.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare class FrameQueue extends ListQueue {
|
|
|
33
33
|
* @param fn The function to execute.
|
|
34
34
|
* @returns The task object.
|
|
35
35
|
*/
|
|
36
|
-
enqueue(fn: (timeStamp: number, task: Task, queue: FrameQueue) =>
|
|
36
|
+
enqueue(fn: ({timeStamp: number, task: Task, queue: FrameQueue}) => unknown): Task;
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* Dequeues a task.
|
|
@@ -42,6 +42,15 @@ export declare class FrameQueue extends ListQueue {
|
|
|
42
42
|
*/
|
|
43
43
|
dequeue(task: Task): this;
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Schedules a task to run in the next frame.
|
|
47
|
+
* @param fn The function to execute. If `undefined` or `null`, the task's promise will be resolved with function's arguments. Otherwise, it is resolved with the function's return value.
|
|
48
|
+
* @returns The task object.
|
|
49
|
+
*/
|
|
50
|
+
schedule(
|
|
51
|
+
fn: (({timeStamp: number, task: Task, queue: FrameQueue}) => unknown) | null | undefined
|
|
52
|
+
): Task;
|
|
53
|
+
|
|
45
54
|
/**
|
|
46
55
|
* Clears the queue.
|
|
47
56
|
* @returns The queue.
|
package/src/FrameQueue.js
CHANGED
|
@@ -26,14 +26,14 @@ export class FrameQueue extends ListQueue {
|
|
|
26
26
|
const start = Date.now();
|
|
27
27
|
while (Date.now() - start < this.batch && !this.list.isEmpty) {
|
|
28
28
|
const task = this.list.popFront();
|
|
29
|
-
task.fn(timeStamp, task, this);
|
|
29
|
+
task.fn({timeStamp, task, queue: this});
|
|
30
30
|
}
|
|
31
31
|
} else {
|
|
32
32
|
const list = this.list;
|
|
33
33
|
this.list = new List();
|
|
34
34
|
while (!list.isEmpty) {
|
|
35
35
|
const task = list.popFront();
|
|
36
|
-
task.fn(timeStamp, task, this);
|
|
36
|
+
task.fn({timeStamp, task, queue: this});
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
package/src/IdleQueue.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export declare class IdleQueue extends ListQueue {
|
|
|
40
40
|
* @param fn The function to execute.
|
|
41
41
|
* @returns The task object.
|
|
42
42
|
*/
|
|
43
|
-
enqueue(fn: (deadline: IdleDeadline, task: Task, queue: IdleQueue) =>
|
|
43
|
+
enqueue(fn: ({deadline: IdleDeadline, task: Task, queue: IdleQueue}) => unknown): Task;
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Dequeues a task.
|
|
@@ -49,6 +49,15 @@ export declare class IdleQueue extends ListQueue {
|
|
|
49
49
|
*/
|
|
50
50
|
dequeue(task: Task): this;
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Schedules a task to run in the next idle period.
|
|
54
|
+
* @param fn The function to execute. If `undefined` or `null`, the task's promise will be resolved with function's arguments. Otherwise, it is resolved with the function's return value.
|
|
55
|
+
* @returns The task object.
|
|
56
|
+
*/
|
|
57
|
+
schedule(
|
|
58
|
+
fn: (({deadline: IdleDeadline, task: Task, queue: IdleQueue}) => unknown) | null | undefined
|
|
59
|
+
): Task;
|
|
60
|
+
|
|
52
61
|
/**
|
|
53
62
|
* Clears the queue.
|
|
54
63
|
* @returns The queue.
|
|
@@ -83,6 +92,8 @@ export const idleQueue: IdleQueue;
|
|
|
83
92
|
/**
|
|
84
93
|
* A function that schedules a task to run in the next idle period.
|
|
85
94
|
*/
|
|
86
|
-
export const defer: (
|
|
95
|
+
export const defer: (
|
|
96
|
+
fn: ({deadline: IdleDeadline, task: Task, queue: IdleQueue}) => unknown
|
|
97
|
+
) => Task;
|
|
87
98
|
|
|
88
99
|
export default IdleQueue;
|
package/src/IdleQueue.js
CHANGED
|
@@ -30,20 +30,20 @@ export class IdleQueue extends ListQueue {
|
|
|
30
30
|
const start = Date.now();
|
|
31
31
|
while (Date.now() - start < this.timeoutBatch && !this.list.isEmpty) {
|
|
32
32
|
const task = this.list.popFront();
|
|
33
|
-
task.fn(deadline, task, this);
|
|
33
|
+
task.fn({deadline, task, queue: this});
|
|
34
34
|
}
|
|
35
35
|
} else {
|
|
36
36
|
const list = this.list;
|
|
37
37
|
this.list = new List();
|
|
38
38
|
while (!list.isEmpty) {
|
|
39
39
|
const task = list.popFront();
|
|
40
|
-
task.fn(deadline, task, this);
|
|
40
|
+
task.fn({deadline, task, queue: this});
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
} else {
|
|
44
44
|
while (deadline.timeRemaining() > 0 && !this.list.isEmpty) {
|
|
45
45
|
const task = this.list.popFront();
|
|
46
|
-
task.fn(deadline, task, this);
|
|
46
|
+
task.fn({deadline, task, queue: this});
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
package/src/ListQueue.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {MicroTask} from './MicroTask';
|
|
2
|
+
import {MicroTaskQueue} from './MicroTaskQueue';
|
|
2
3
|
import {List} from 'list-toolkit';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -33,7 +34,7 @@ export declare class ListQueue extends MicroTaskQueue {
|
|
|
33
34
|
* @param fn The function to execute when the microtask is scheduled.
|
|
34
35
|
* @returns The enqueued microtask.
|
|
35
36
|
*/
|
|
36
|
-
enqueue(fn: () =>
|
|
37
|
+
enqueue(fn: () => unknown): MicroTask;
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
40
|
* Dequeues a microtask.
|
|
@@ -42,6 +43,13 @@ export declare class ListQueue extends MicroTaskQueue {
|
|
|
42
43
|
*/
|
|
43
44
|
dequeue(task: MicroTask): this;
|
|
44
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Schedules a microtask.
|
|
48
|
+
* @param fn The function to execute. If `undefined` or `null`, the task's promise will be resolved with function's arguments. Otherwise, it is resolved with the function's return value.
|
|
49
|
+
* @returns The task object.
|
|
50
|
+
*/
|
|
51
|
+
schedule(fn: (() => unknown) | null | undefined): MicroTask;
|
|
52
|
+
|
|
45
53
|
/**
|
|
46
54
|
* Clears the queue.
|
|
47
55
|
* @returns The queue.
|
|
@@ -74,4 +82,9 @@ export declare class ListQueue extends MicroTaskQueue {
|
|
|
74
82
|
*/
|
|
75
83
|
export declare type Task = MicroTask;
|
|
76
84
|
|
|
85
|
+
/**
|
|
86
|
+
* A task for list queues with a promise.
|
|
87
|
+
*/
|
|
88
|
+
export declare type TaskWithPromise = MicroTaskWithPromise;
|
|
89
|
+
|
|
77
90
|
export default ListQueue;
|
package/src/ListQueue.js
CHANGED
|
@@ -42,6 +42,7 @@ export class ListQueue extends MicroTaskQueue {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
dequeue(task) {
|
|
45
|
+
task.cancel();
|
|
45
46
|
this.list.removeNode(task);
|
|
46
47
|
if (!this.paused && this.list.isEmpty && this.stopQueue)
|
|
47
48
|
this.stopQueue = (this.stopQueue(), null);
|
|
@@ -51,7 +52,10 @@ export class ListQueue extends MicroTaskQueue {
|
|
|
51
52
|
clear() {
|
|
52
53
|
const paused = this.paused;
|
|
53
54
|
if (!paused) this.pause();
|
|
54
|
-
this.list.
|
|
55
|
+
while (!this.list.isEmpty) {
|
|
56
|
+
const task = this.list.popFront();
|
|
57
|
+
task.cancel();
|
|
58
|
+
}
|
|
55
59
|
if (!paused) this.resume();
|
|
56
60
|
return this;
|
|
57
61
|
}
|
package/src/MicroTask.d.ts
CHANGED
|
@@ -5,13 +5,46 @@ export declare class MicroTask {
|
|
|
5
5
|
/**
|
|
6
6
|
* The function to execute when the microtask is scheduled.
|
|
7
7
|
*/
|
|
8
|
-
fn: () =>
|
|
8
|
+
fn: () => unknown;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether the microtask has been canceled.
|
|
12
|
+
*/
|
|
13
|
+
isCanceled: boolean;
|
|
9
14
|
|
|
10
15
|
/**
|
|
11
16
|
* Creates a new microtask.
|
|
12
17
|
* @param fn The function to execute when the microtask is scheduled.
|
|
13
18
|
*/
|
|
14
|
-
constructor(fn: () =>
|
|
19
|
+
constructor(fn: () => unknown);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Makes a promise that will be resolved when the microtask is executed.
|
|
23
|
+
* @returns The microtask.
|
|
24
|
+
*/
|
|
25
|
+
makePromise(): this;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The promise that could be resolved when the microtask is executed.
|
|
29
|
+
* This is a queue-specific promise. It may be created when there is an associated asynchronous task.
|
|
30
|
+
* If the microtask is canceled, the promise will be rejected with a CancelTaskError.
|
|
31
|
+
*/
|
|
32
|
+
get promise(): Promise<unknown> | null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the microtask, if a promise is created.
|
|
36
|
+
* @param value The value to resolve the microtask with.
|
|
37
|
+
* @returns The microtask.
|
|
38
|
+
*/
|
|
39
|
+
resolve(value: unknown): this;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Cancels the microtask, if a promise is created.
|
|
43
|
+
* If the microtask is canceled, the promise will be rejected with a CancelTaskError.
|
|
44
|
+
* It can be overridden in subclasses.
|
|
45
|
+
* @returns The microtask.
|
|
46
|
+
*/
|
|
47
|
+
cancel(): this;
|
|
15
48
|
}
|
|
16
49
|
|
|
17
50
|
export default MicroTask;
|
package/src/MicroTask.js
CHANGED
|
@@ -2,9 +2,43 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
|
+
import CancelTaskError from './CancelTaskError.js';
|
|
6
|
+
|
|
5
7
|
export class MicroTask {
|
|
8
|
+
#promise;
|
|
9
|
+
#resolve;
|
|
10
|
+
#reject;
|
|
6
11
|
constructor(fn) {
|
|
7
12
|
this.fn = fn;
|
|
13
|
+
this.#promise = null;
|
|
14
|
+
this.#resolve = null;
|
|
15
|
+
this.#reject = null;
|
|
16
|
+
this.isCanceled = false;
|
|
17
|
+
}
|
|
18
|
+
get promise() {
|
|
19
|
+
return this.#promise;
|
|
20
|
+
}
|
|
21
|
+
makePromise() {
|
|
22
|
+
this.#promise = new Promise((resolve, reject) => {
|
|
23
|
+
this.#resolve = resolve;
|
|
24
|
+
this.#reject = reject;
|
|
25
|
+
});
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
resolve(value) {
|
|
29
|
+
if (!this.#resolve) return;
|
|
30
|
+
this.#resolve(value);
|
|
31
|
+
this.#resolve = null;
|
|
32
|
+
this.#reject = null;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
cancel() {
|
|
36
|
+
if (!this.#reject) return;
|
|
37
|
+
this.isCanceled = true;
|
|
38
|
+
this.#reject(new CancelTaskError());
|
|
39
|
+
this.#resolve = null;
|
|
40
|
+
this.#reject = null;
|
|
41
|
+
return this;
|
|
8
42
|
}
|
|
9
43
|
}
|
|
10
44
|
|
package/src/MicroTaskQueue.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import MicroTask from './MicroTask';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A queue of microtasks that will be executed when scheduled.
|
|
@@ -28,7 +28,7 @@ export declare class MicroTaskQueue {
|
|
|
28
28
|
* @param fn The function to execute when the microtask is scheduled.
|
|
29
29
|
* @returns The enqueued microtask.
|
|
30
30
|
*/
|
|
31
|
-
enqueue(fn: () =>
|
|
31
|
+
enqueue(fn: () => unknown): MicroTask;
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Dequeues a microtask.
|
|
@@ -38,6 +38,14 @@ export declare class MicroTaskQueue {
|
|
|
38
38
|
*/
|
|
39
39
|
dequeue(task: MicroTask): this;
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Schedules a microtask with a promise.
|
|
43
|
+
* It can be overridden in subclasses, if more arguments are needed.
|
|
44
|
+
* @param fn The function to execute when the microtask is scheduled, it can be an async function.
|
|
45
|
+
* @returns The scheduled microtask.
|
|
46
|
+
*/
|
|
47
|
+
schedule(fn: (() => unknown) | null | undefined): MicroTask;
|
|
48
|
+
|
|
41
49
|
/**
|
|
42
50
|
* Clears the queue.
|
|
43
51
|
* It is meant to be overridden in subclasses.
|
package/src/MicroTaskQueue.js
CHANGED
|
@@ -25,11 +25,32 @@ export class MicroTaskQueue {
|
|
|
25
25
|
return task;
|
|
26
26
|
}
|
|
27
27
|
dequeue(task) {
|
|
28
|
+
task.cancel();
|
|
28
29
|
return this;
|
|
29
30
|
}
|
|
30
31
|
clear() {
|
|
31
32
|
return this;
|
|
32
33
|
}
|
|
34
|
+
// Generic API
|
|
35
|
+
schedule(fn, ...args) {
|
|
36
|
+
fn ||= MicroTaskQueue.returnArgs;
|
|
37
|
+
const task = this.enqueue(
|
|
38
|
+
(...args) => {
|
|
39
|
+
try {
|
|
40
|
+
task.resolve(fn(...args));
|
|
41
|
+
} catch (error) {
|
|
42
|
+
task.cancel();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
...args
|
|
46
|
+
);
|
|
47
|
+
task.makePromise();
|
|
48
|
+
task.fn = fn;
|
|
49
|
+
return task;
|
|
50
|
+
}
|
|
51
|
+
static returnArgs(...args) {
|
|
52
|
+
return args;
|
|
53
|
+
}
|
|
33
54
|
}
|
|
34
55
|
|
|
35
56
|
export default MicroTaskQueue;
|
package/src/PageWatcher.d.ts
CHANGED
|
@@ -48,6 +48,12 @@ export declare class PageWatcher extends ListQueue {
|
|
|
48
48
|
*/
|
|
49
49
|
dequeue(task: Task): this;
|
|
50
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Schedules a task to be executed when the page state changes.
|
|
53
|
+
* @throws Always throws an error.
|
|
54
|
+
*/
|
|
55
|
+
schedule(): never;
|
|
56
|
+
|
|
51
57
|
/**
|
|
52
58
|
* Clears the queue.
|
|
53
59
|
* @returns The queue.
|
package/src/PageWatcher.js
CHANGED
package/src/Scheduler.d.ts
CHANGED
|
@@ -8,7 +8,12 @@ export declare class Task extends MicroTask {
|
|
|
8
8
|
/**
|
|
9
9
|
* The function to execute.
|
|
10
10
|
*/
|
|
11
|
-
fn: (task: Task, scheduler: Scheduler) =>
|
|
11
|
+
fn: ({task: Task, scheduler: Scheduler}) => unknown;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Whether the task has been canceled.
|
|
15
|
+
*/
|
|
16
|
+
isCanceled: boolean;
|
|
12
17
|
|
|
13
18
|
/**
|
|
14
19
|
* The time in milliseconds (Unix timestamp) when the task is scheduled to run.
|
|
@@ -25,7 +30,35 @@ export declare class Task extends MicroTask {
|
|
|
25
30
|
* @param delay The delay before the task is executed. It can be a number of milliseconds or a `Date` object as an absolute time.
|
|
26
31
|
* @param fn The function to execute.
|
|
27
32
|
*/
|
|
28
|
-
constructor(delay: number | Date, fn: (task: Task, scheduler: Scheduler) =>
|
|
33
|
+
constructor(delay: number | Date, fn: ({task: Task, scheduler: Scheduler}) => unknown);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Makes a promise that will be resolved when the microtask is executed.
|
|
37
|
+
* @returns The microtask.
|
|
38
|
+
*/
|
|
39
|
+
makePromise(): this;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The promise that could be resolved when the microtask is executed.
|
|
43
|
+
* This is a queue-specific promise. It may be created when there is an associated asynchronous task.
|
|
44
|
+
* If the microtask is canceled, the promise will be rejected with a CancelTaskError.
|
|
45
|
+
*/
|
|
46
|
+
get promise(): Promise<unknown> | null;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Resolves the microtask, if a promise is created.
|
|
50
|
+
* @param value The value to resolve the microtask with.
|
|
51
|
+
* @returns The microtask.
|
|
52
|
+
*/
|
|
53
|
+
resolve(value: unknown): this;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Cancels the microtask, if a promise is created.
|
|
57
|
+
* If the microtask is canceled, the promise will be rejected with a CancelTaskError.
|
|
58
|
+
* It can be overridden in subclasses.
|
|
59
|
+
* @returns The microtask.
|
|
60
|
+
*/
|
|
61
|
+
cancel(): this;
|
|
29
62
|
}
|
|
30
63
|
|
|
31
64
|
/**
|
|
@@ -65,7 +98,7 @@ export declare class Scheduler extends MicroTaskQueue {
|
|
|
65
98
|
* @param delay The delay before the task is executed. It can be a number of milliseconds or a `Date` object as an absolute time.
|
|
66
99
|
* @returns The task object.
|
|
67
100
|
*/
|
|
68
|
-
enqueue(fn: (task: Task, scheduler: Scheduler) =>
|
|
101
|
+
enqueue(fn: ({task: Task, scheduler: Scheduler}) => unknown, delay: number | Date): Task;
|
|
69
102
|
|
|
70
103
|
/**
|
|
71
104
|
* Removes a task from the scheduler.
|
|
@@ -75,11 +108,15 @@ export declare class Scheduler extends MicroTaskQueue {
|
|
|
75
108
|
dequeue(task: Task): this;
|
|
76
109
|
|
|
77
110
|
/**
|
|
78
|
-
*
|
|
111
|
+
* Schedules a task to run in the future.
|
|
112
|
+
* @param fn The function to execute. If `undefined` or `null`, the task's promise will be resolved with function's arguments. Otherwise, it is resolved with the function's return value.
|
|
79
113
|
* @param delay The delay before the task is executed. It can be a number of milliseconds or a `Date` object as an absolute time.
|
|
80
|
-
* @returns
|
|
114
|
+
* @returns The task object.
|
|
81
115
|
*/
|
|
82
|
-
schedule(
|
|
116
|
+
schedule(
|
|
117
|
+
fn: (({task: Task, scheduler: Scheduler}) => unknown) | null | undefined,
|
|
118
|
+
delay: number | Date
|
|
119
|
+
): Task;
|
|
83
120
|
|
|
84
121
|
/**
|
|
85
122
|
* Clears the queue.
|
|
@@ -107,9 +144,9 @@ export declare class Scheduler extends MicroTaskQueue {
|
|
|
107
144
|
* @returns A function that can be used to enqueue the task to the scheduler.
|
|
108
145
|
*/
|
|
109
146
|
export declare const repeat: (
|
|
110
|
-
fn: (task: Task, scheduler: Scheduler) => void,
|
|
147
|
+
fn: ({task: Task, scheduler: Scheduler}) => void,
|
|
111
148
|
delay: number | Date
|
|
112
|
-
) => (task: Task, scheduler: Scheduler) => void;
|
|
149
|
+
) => ({task: Task, scheduler: Scheduler}) => void;
|
|
113
150
|
|
|
114
151
|
/**
|
|
115
152
|
* A scheduler instance usually used as a global scheduler.
|
package/src/Scheduler.js
CHANGED
|
@@ -70,6 +70,7 @@ export class Scheduler extends MicroTaskQueue {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
dequeue(task) {
|
|
73
|
+
task.cancel();
|
|
73
74
|
if (this.queue.isEmpty) return this;
|
|
74
75
|
if (this.paused || this.queue.top !== task) {
|
|
75
76
|
this.queue.remove(task);
|
|
@@ -83,15 +84,10 @@ export class Scheduler extends MicroTaskQueue {
|
|
|
83
84
|
return this;
|
|
84
85
|
}
|
|
85
86
|
|
|
86
|
-
schedule(delay) {
|
|
87
|
-
return new Promise(resolve =>
|
|
88
|
-
this.enqueue((task, scheduler) => resolve({task, scheduler}), delay)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
87
|
clear() {
|
|
93
88
|
const paused = this.paused;
|
|
94
89
|
if (!paused) this.pause();
|
|
90
|
+
this.queue.array.forEach(task => task.cancel());
|
|
95
91
|
this.queue.clear();
|
|
96
92
|
if (!paused) this.resume();
|
|
97
93
|
}
|
|
@@ -113,7 +109,7 @@ export class Scheduler extends MicroTaskQueue {
|
|
|
113
109
|
this.queue.top.time <= Date.now() + this.tolerance
|
|
114
110
|
) {
|
|
115
111
|
const task = this.queue.pop();
|
|
116
|
-
task.fn(task, this);
|
|
112
|
+
task.fn({task, scheduler: this});
|
|
117
113
|
}
|
|
118
114
|
|
|
119
115
|
if (!this.paused && !this.queue.isEmpty) this.stopQueue = this.startQueue();
|
|
@@ -121,8 +117,8 @@ export class Scheduler extends MicroTaskQueue {
|
|
|
121
117
|
}
|
|
122
118
|
|
|
123
119
|
export const repeat = (fn, delay) => {
|
|
124
|
-
const repeatableFunction = (task, scheduler) => {
|
|
125
|
-
fn(task, scheduler);
|
|
120
|
+
const repeatableFunction = ({task, scheduler}) => {
|
|
121
|
+
fn({task, scheduler});
|
|
126
122
|
scheduler.enqueue(repeatableFunction, isNaN(delay) ? task.delay : delay);
|
|
127
123
|
};
|
|
128
124
|
return repeatableFunction;
|
package/src/audit.d.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
8
8
|
* @returns An audited version of the function.
|
|
9
9
|
*/
|
|
10
|
-
export declare function audit<
|
|
11
|
-
fn:
|
|
10
|
+
export declare function audit<A extends unknown[]>(
|
|
11
|
+
fn: (...args: A) => void,
|
|
12
12
|
ms: number
|
|
13
|
-
): (...args:
|
|
13
|
+
): (...args: A) => void;
|
|
14
14
|
|
|
15
15
|
export default audit;
|
package/src/debounce.d.ts
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
7
7
|
* @returns A debounced version of the function.
|
|
8
8
|
*/
|
|
9
|
-
export declare function debounce<
|
|
10
|
-
fn:
|
|
9
|
+
export declare function debounce<A extends unknown[]>(
|
|
10
|
+
fn: (...args: A) => void,
|
|
11
11
|
ms: number
|
|
12
|
-
): (...args:
|
|
12
|
+
): (...args: A) => void;
|
|
13
13
|
|
|
14
14
|
export default debounce;
|
package/src/defer.d.ts
CHANGED
|
@@ -8,6 +8,22 @@
|
|
|
8
8
|
* @param fn The function to delay.
|
|
9
9
|
* @returns A function that, when called, will execute the provided function.
|
|
10
10
|
*/
|
|
11
|
-
export declare function defer(fn: () => void): () => void;
|
|
11
|
+
export declare function defer<A extends unknown[]>(fn: (...args: A) => void): (...args: A) => void;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Schedules a function to be called when the next available time.
|
|
15
|
+
*
|
|
16
|
+
* @param fn The function to schedule.
|
|
17
|
+
* @returns A promise that resolves when the function is called.
|
|
18
|
+
*/
|
|
19
|
+
export declare function scheduleDefer<R extends unknown>(fn: () => R): Promise<Awaited<R>>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Schedules a function to be called when the next available time.
|
|
23
|
+
*
|
|
24
|
+
* @param fn The function to schedule.
|
|
25
|
+
* @returns A promise that resolves when the function is called.
|
|
26
|
+
*/
|
|
27
|
+
export declare function scheduleDefer(fn: null | undefined): Promise<void>;
|
|
12
28
|
|
|
13
29
|
export default defer;
|
package/src/defer.js
CHANGED
|
@@ -12,4 +12,19 @@ if (typeof requestIdleCallback == 'function') {
|
|
|
12
12
|
|
|
13
13
|
export const defer = fn => void deferImplementation(() => fn());
|
|
14
14
|
|
|
15
|
+
const returnArgs = (...args) => args;
|
|
16
|
+
|
|
17
|
+
export const scheduleDefer = fn => {
|
|
18
|
+
fn ||= returnArgs;
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
defer(() => {
|
|
21
|
+
try {
|
|
22
|
+
resolve(fn());
|
|
23
|
+
} catch (error) {
|
|
24
|
+
reject(error);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
15
30
|
export default defer;
|
package/src/index.d.ts
CHANGED
package/src/sample.d.ts
CHANGED
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
10
10
|
* @returns A sampled version of the function.
|
|
11
11
|
*/
|
|
12
|
-
export declare function sample<
|
|
13
|
-
fn:
|
|
12
|
+
export declare function sample<A extends unknown[]>(
|
|
13
|
+
fn: (...args: A) => void,
|
|
14
14
|
ms: number
|
|
15
|
-
): (...args:
|
|
15
|
+
): (...args: A) => void;
|
|
16
16
|
|
|
17
17
|
export default sample;
|
package/src/throttle.d.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
8
8
|
* @returns A throttled version of the function.
|
|
9
9
|
*/
|
|
10
|
-
export declare function throttle<
|
|
11
|
-
fn:
|
|
10
|
+
export declare function throttle<A extends unknown[]>(
|
|
11
|
+
fn: (...args: A) => void,
|
|
12
12
|
ms: number
|
|
13
|
-
): (...args:
|
|
13
|
+
): (...args: A) => void;
|
|
14
14
|
|
|
15
15
|
export default throttle;
|
package/src/when-dom-loaded.d.ts
CHANGED
|
@@ -14,4 +14,24 @@ export declare function whenDomLoaded(fn: () => void): void;
|
|
|
14
14
|
*/
|
|
15
15
|
export declare function remove(fn: () => void): boolean;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Schedules a function to be called when the DOM is loaded.
|
|
19
|
+
*
|
|
20
|
+
* @param fn The function to schedule.
|
|
21
|
+
* @returns A promise that resolves when the DOM is loaded.
|
|
22
|
+
*/
|
|
23
|
+
export declare function scheduleWhenDomLoaded<R extends unknown>(
|
|
24
|
+
fn: () => R
|
|
25
|
+
): Promise<Awaited<R>>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Schedules a function to be called when the DOM is loaded.
|
|
29
|
+
*
|
|
30
|
+
* @param fn The function to schedule.
|
|
31
|
+
* @returns A promise that resolves when the DOM is loaded.
|
|
32
|
+
*/
|
|
33
|
+
export declare function scheduleWhenDomLoaded(
|
|
34
|
+
fn: null | undefined
|
|
35
|
+
): Promise<void>;
|
|
36
|
+
|
|
17
37
|
export default whenDomLoaded;
|
package/src/when-dom-loaded.js
CHANGED
|
@@ -34,4 +34,19 @@ export const whenDomLoaded = fn => {
|
|
|
34
34
|
if (wasEmpty) document.addEventListener('DOMContentLoaded', handleDomLoaded);
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
const returnArgs = (...args) => args;
|
|
38
|
+
|
|
39
|
+
export const scheduleWhenDomLoaded = fn => {
|
|
40
|
+
fn ||= returnArgs;
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
whenDomLoaded(() => {
|
|
43
|
+
try {
|
|
44
|
+
resolve(fn());
|
|
45
|
+
} catch (error) {
|
|
46
|
+
reject(error);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
37
52
|
export default whenDomLoaded;
|
package/src/when-loaded.d.ts
CHANGED
|
@@ -14,4 +14,24 @@ export declare function whenLoaded(fn: () => void): void;
|
|
|
14
14
|
*/
|
|
15
15
|
export declare function remove(fn: () => void): boolean;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Schedules a function to be called when the document is loaded.
|
|
19
|
+
*
|
|
20
|
+
* @param fn The function to schedule.
|
|
21
|
+
* @returns A promise that resolves when the document is loaded.
|
|
22
|
+
*/
|
|
23
|
+
export declare function scheduleWhenLoaded<R extends unknown>(
|
|
24
|
+
fn: () => R
|
|
25
|
+
): Promise<Awaited<R>>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Schedules a function to be called when the document is loaded.
|
|
29
|
+
*
|
|
30
|
+
* @param fn The function to schedule.
|
|
31
|
+
* @returns A promise that resolves when the document is loaded.
|
|
32
|
+
*/
|
|
33
|
+
export declare function scheduleWhenLoaded(
|
|
34
|
+
fn: null | undefined
|
|
35
|
+
): Promise<void>;
|
|
36
|
+
|
|
17
37
|
export default whenLoaded;
|
package/src/when-loaded.js
CHANGED
|
@@ -32,4 +32,19 @@ export const whenLoaded = fn => {
|
|
|
32
32
|
if (wasEmpty) window.addEventListener('load', handleLoaded);
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
const returnArgs = (...args) => args;
|
|
36
|
+
|
|
37
|
+
export const scheduleWhenLoaded = fn => {
|
|
38
|
+
fn ||= returnArgs;
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
whenLoaded(() => {
|
|
41
|
+
try {
|
|
42
|
+
resolve(fn());
|
|
43
|
+
} catch (error) {
|
|
44
|
+
reject(error);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
35
50
|
export default whenLoaded;
|