@shuvi/utils 1.0.61 → 1.0.62
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/lib/idleCallback.d.ts +12 -0
- package/lib/idleCallback.js +83 -0
- package/package.json +1 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const requestIdleCallback: ((callback: IdleRequestCallback, options?: IdleRequestOptions) => number) & typeof globalThis.requestIdleCallback;
|
|
2
|
+
export declare const cancelIdleCallback: ((handle: number) => void) & typeof globalThis.cancelIdleCallback;
|
|
3
|
+
/**
|
|
4
|
+
* awaitPageLoadAndIdle - Invokes the callback after:
|
|
5
|
+
* 1. The page has finished loading
|
|
6
|
+
* 2. Idle time remaining is >= specified `remainingTime` (default 49ms)
|
|
7
|
+
* 3. Timeout of `timeout` duration (default 2000ms) if idle condition is not met
|
|
8
|
+
*/
|
|
9
|
+
export declare function awaitPageLoadAndIdle({ remainingTime, timeout }?: {
|
|
10
|
+
remainingTime: number;
|
|
11
|
+
timeout: number;
|
|
12
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cancelIdleCallback = exports.requestIdleCallback = void 0;
|
|
4
|
+
exports.awaitPageLoadAndIdle = awaitPageLoadAndIdle;
|
|
5
|
+
/**
|
|
6
|
+
* Force execution of callback after the specified timeout, default 3000ms
|
|
7
|
+
*/
|
|
8
|
+
function _requestIdleCallbackPolyfill(cb, options = { timeout: 3000 }) {
|
|
9
|
+
return setTimeout(() => {
|
|
10
|
+
cb({ didTimeout: false, timeRemaining: () => 50 });
|
|
11
|
+
}, options.timeout);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* For server side, invoke the callback immediately
|
|
15
|
+
*/
|
|
16
|
+
function _requestIdleCallbackServerSide(cb) {
|
|
17
|
+
cb({ didTimeout: false, timeRemaining: () => 50 });
|
|
18
|
+
return NaN;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Assume the polyfill is implemented by setTimeout
|
|
22
|
+
*/
|
|
23
|
+
function _cancelIdleCallbackPolyfill(id) {
|
|
24
|
+
clearTimeout(id);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Do nothing on server side
|
|
28
|
+
*/
|
|
29
|
+
function _cancelIdleCallbackServerSide(_id) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
exports.requestIdleCallback = typeof window !== 'undefined'
|
|
33
|
+
? window.requestIdleCallback || _requestIdleCallbackPolyfill
|
|
34
|
+
: _requestIdleCallbackServerSide;
|
|
35
|
+
exports.cancelIdleCallback = typeof window !== 'undefined'
|
|
36
|
+
? window.cancelIdleCallback || _cancelIdleCallbackPolyfill
|
|
37
|
+
: _cancelIdleCallbackServerSide;
|
|
38
|
+
/**
|
|
39
|
+
* awaitPageLoadAndIdle - Invokes the callback after:
|
|
40
|
+
* 1. The page has finished loading
|
|
41
|
+
* 2. Idle time remaining is >= specified `remainingTime` (default 49ms)
|
|
42
|
+
* 3. Timeout of `timeout` duration (default 2000ms) if idle condition is not met
|
|
43
|
+
*/
|
|
44
|
+
function awaitPageLoadAndIdle({ remainingTime, timeout } = {
|
|
45
|
+
remainingTime: 49,
|
|
46
|
+
timeout: 2000
|
|
47
|
+
}) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
// Return early if function is called in a non-browser environment
|
|
50
|
+
if (typeof window === 'undefined') {
|
|
51
|
+
return reject(new Error('[awaitPageLoadAndIdle] server side is not supported'));
|
|
52
|
+
}
|
|
53
|
+
let idleCallbackId; // Tracks the idle callback
|
|
54
|
+
const tid = setTimeout(() => {
|
|
55
|
+
if (exports.cancelIdleCallback && idleCallbackId) {
|
|
56
|
+
(0, exports.cancelIdleCallback)(idleCallbackId);
|
|
57
|
+
}
|
|
58
|
+
resolve(); // Force resolve after timeout
|
|
59
|
+
}, timeout);
|
|
60
|
+
// Function to check if sufficient idle time is available
|
|
61
|
+
function onIdle(deadline) {
|
|
62
|
+
if (deadline.timeRemaining() >= remainingTime) {
|
|
63
|
+
clearTimeout(tid);
|
|
64
|
+
resolve();
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
idleCallbackId = (0, exports.requestIdleCallback)(onIdle); // Retry if idle time insufficient
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Event handler to trigger on page load
|
|
71
|
+
const onLoad = () => {
|
|
72
|
+
window.removeEventListener('load', onLoad); // Clean up listener
|
|
73
|
+
idleCallbackId = (0, exports.requestIdleCallback)(onIdle); // Start checking for idle time
|
|
74
|
+
};
|
|
75
|
+
// If page is already loaded, check idle time immediately
|
|
76
|
+
if (document.readyState === 'complete') {
|
|
77
|
+
idleCallbackId = (0, exports.requestIdleCallback)(onIdle);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
window.addEventListener('load', onLoad); // Wait for page load event
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|