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 +27 -294
- package/package.json +16 -6
- package/src/FrameQueue.d.ts +76 -0
- package/src/FrameQueue.js +2 -0
- package/src/IdleQueue.d.ts +88 -0
- package/src/IdleQueue.js +2 -0
- package/src/ListQueue.d.ts +77 -0
- package/src/ListQueue.js +2 -0
- package/src/MicroTask.d.ts +17 -0
- package/src/MicroTask.js +2 -0
- package/src/MicroTaskQueue.d.ts +63 -0
- package/src/MicroTaskQueue.js +2 -0
- package/src/PageWatcher.d.ts +87 -0
- package/src/PageWatcher.js +2 -0
- package/src/Retainer.d.ts +68 -0
- package/src/Retainer.js +48 -0
- package/src/Scheduler.d.ts +119 -0
- package/src/Scheduler.js +11 -3
- package/src/Throttler.d.ts +95 -0
- package/src/Throttler.js +71 -0
- package/src/audit.d.ts +15 -0
- package/src/audit.js +20 -0
- package/src/debounce.d.ts +14 -0
- package/src/debounce.js +16 -0
- package/src/defer.d.ts +13 -0
- package/src/defer.js +2 -0
- package/src/index.d.ts +22 -0
- package/src/sample.d.ts +17 -0
- package/src/sample.js +25 -0
- package/src/sleep.d.ts +9 -0
- package/src/sleep.js +13 -0
- package/src/throttle.d.ts +15 -0
- package/src/throttle.js +14 -0
- package/src/when-dom-loaded.d.ts +17 -0
- package/src/when-dom-loaded.js +3 -1
- package/src/when-loaded.d.ts +17 -0
- package/src/when-loaded.js +3 -1
package/src/defer.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delays the execution of a function until the next available time.
|
|
3
|
+
* It will use available system functions in the following order:
|
|
4
|
+
* - `requestIdleCallback()`
|
|
5
|
+
* - `setImmediate()`
|
|
6
|
+
* - `setTimeout()`
|
|
7
|
+
*
|
|
8
|
+
* @param fn The function to delay.
|
|
9
|
+
* @returns A function that, when called, will execute the provided function.
|
|
10
|
+
*/
|
|
11
|
+
export declare function defer(fn: () => void): () => void;
|
|
12
|
+
|
|
13
|
+
export default defer;
|
package/src/defer.js
CHANGED
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare module 'time-queues' {
|
|
2
|
+
export * from './audit';
|
|
3
|
+
export * from './debounce';
|
|
4
|
+
export * from './defer';
|
|
5
|
+
export * from './sample';
|
|
6
|
+
export * from './sleep';
|
|
7
|
+
export * from './throttle';
|
|
8
|
+
|
|
9
|
+
export * from './MicroTask';
|
|
10
|
+
export * from './MicroTaskQueue';
|
|
11
|
+
export * from './ListQueue';
|
|
12
|
+
export * from './FrameQueue';
|
|
13
|
+
export * from './IdleQueue';
|
|
14
|
+
|
|
15
|
+
export * from './PageWatcher';
|
|
16
|
+
export * from './Scheduler';
|
|
17
|
+
export * from './Retainer';
|
|
18
|
+
export * from './Throttler';
|
|
19
|
+
|
|
20
|
+
export * from './when-dom-loaded';
|
|
21
|
+
export * from './when-loaded';
|
|
22
|
+
}
|
package/src/sample.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Samples a function by ensuring it is not called more often than the specified interval.
|
|
3
|
+
* The last seen arguments are passed to the function after the specified time interval.
|
|
4
|
+
* Unlike `audit()`, intervals are not reset by the function calls and never stops.
|
|
5
|
+
* If no call was made to the sampled function within the specified time interval,
|
|
6
|
+
* no calls are made after the specified time interval.
|
|
7
|
+
*
|
|
8
|
+
* @param fn The function to sample.
|
|
9
|
+
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
10
|
+
* @returns A sampled version of the function.
|
|
11
|
+
*/
|
|
12
|
+
export declare function sample<T extends (...args: any[]) => void>(
|
|
13
|
+
fn: T,
|
|
14
|
+
ms: number
|
|
15
|
+
): (...args: Parameters<T>) => void;
|
|
16
|
+
|
|
17
|
+
export default sample;
|
package/src/sample.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-self-types="./sample.d.ts"
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
export const sample = (fn, ms) => {
|
|
6
|
+
const started = Date.now();
|
|
7
|
+
let handle = null,
|
|
8
|
+
lastSeenArgs = null,
|
|
9
|
+
timeout = ms;
|
|
10
|
+
return (...args) => {
|
|
11
|
+
lastSeenArgs = [...args];
|
|
12
|
+
if (handle) return;
|
|
13
|
+
const diff = (Date.now() - started) % ms,
|
|
14
|
+
delay = diff ? ms - diff : timeout;
|
|
15
|
+
timeout = 0;
|
|
16
|
+
handle = setTimeout(() => {
|
|
17
|
+
handle = null;
|
|
18
|
+
const args = lastSeenArgs;
|
|
19
|
+
lastSeenArgs = null;
|
|
20
|
+
fn(...args);
|
|
21
|
+
}, delay);
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default sample;
|
package/src/sleep.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suspends the execution for a specified time.
|
|
3
|
+
*
|
|
4
|
+
* @param ms The time to suspend the execution for, in milliseconds or a date as an absolute time.
|
|
5
|
+
* @returns A promise that resolves after the specified time.
|
|
6
|
+
*/
|
|
7
|
+
export declare function sleep(ms: number | Date): Promise<void>;
|
|
8
|
+
|
|
9
|
+
export default sleep;
|
package/src/sleep.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @ts-self-types="./sleep.d.ts"
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
export const sleep = ms => {
|
|
6
|
+
if (ms instanceof Date) {
|
|
7
|
+
ms = ms.getTime() - Date.now();
|
|
8
|
+
}
|
|
9
|
+
ms = Math.max(0, ms);
|
|
10
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default sleep;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throttles a function by ensuring it is not called more often than the specified interval.
|
|
3
|
+
* The first call calls the function and starts a timeout. Until the timeout expires, any subsequent calls are ignored.
|
|
4
|
+
* This function is similar to `audit()`, but the first seen arguments are passed to the function.
|
|
5
|
+
*
|
|
6
|
+
* @param fn The function to throttle.
|
|
7
|
+
* @param ms The minimum time interval between function calls, in milliseconds.
|
|
8
|
+
* @returns A throttled version of the function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function throttle<T extends (...args: any[]) => void>(
|
|
11
|
+
fn: T,
|
|
12
|
+
ms: number
|
|
13
|
+
): (...args: Parameters<T>) => void;
|
|
14
|
+
|
|
15
|
+
export default throttle;
|
package/src/throttle.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @ts-self-types="./throttle.d.ts"
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
export const throttle = (fn, ms) => {
|
|
6
|
+
let handle = null;
|
|
7
|
+
return (...args) => {
|
|
8
|
+
if (handle) return;
|
|
9
|
+
handle = setTimeout(() => (handle = null), ms);
|
|
10
|
+
fn(...args);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default throttle;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds a function to the list of functions to be called when the DOM is loaded.
|
|
3
|
+
* If the DOM is already loaded, the function is called immediately.
|
|
4
|
+
*
|
|
5
|
+
* @param fn The function to add.
|
|
6
|
+
*/
|
|
7
|
+
export declare function whenDomLoaded(fn: () => void): void;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Removes a function from the list of functions to be called when the DOM is loaded.
|
|
11
|
+
*
|
|
12
|
+
* @param fn The function to remove.
|
|
13
|
+
* @returns `true` if the function was removed, `false` otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export declare function remove(fn: () => void): boolean;
|
|
16
|
+
|
|
17
|
+
export default whenDomLoaded;
|
package/src/when-dom-loaded.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-self-types="./when-dom-loaded.d.ts"
|
|
2
|
+
|
|
1
3
|
'use strict';
|
|
2
4
|
|
|
3
5
|
import ValueList from 'list-toolkit/value-list.js';
|
|
@@ -12,7 +14,7 @@ export const remove = fn => {
|
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
return false;
|
|
15
|
-
}
|
|
17
|
+
};
|
|
16
18
|
|
|
17
19
|
const handleDomLoaded = () => {
|
|
18
20
|
while (!waitingForDom.isEmpty) waitingForDom.pop()();
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds a function to the list of functions to be called when the document is loaded.
|
|
3
|
+
* If the document is already loaded, the function is called immediately.
|
|
4
|
+
*
|
|
5
|
+
* @param fn The function to add.
|
|
6
|
+
*/
|
|
7
|
+
export declare function whenLoaded(fn: () => void): void;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Removes a function from the list of functions to be called when the document is loaded.
|
|
11
|
+
*
|
|
12
|
+
* @param fn The function to remove.
|
|
13
|
+
* @returns `true` if the function was removed, `false` otherwise.
|
|
14
|
+
*/
|
|
15
|
+
export declare function remove(fn: () => void): boolean;
|
|
16
|
+
|
|
17
|
+
export default whenLoaded;
|
package/src/when-loaded.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @ts-self-types="./when-loaded.d.ts"
|
|
2
|
+
|
|
1
3
|
'use strict';
|
|
2
4
|
|
|
3
5
|
import ValueList from 'list-toolkit/value-list.js';
|
|
@@ -12,7 +14,7 @@ export const remove = fn => {
|
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
return false;
|
|
15
|
-
}
|
|
17
|
+
};
|
|
16
18
|
|
|
17
19
|
const handleLoaded = () => {
|
|
18
20
|
while (!waitingForLoad.isEmpty) waitingForLoad.pop()();
|