@hkdigital/lib-sveltekit 0.0.50 → 0.0.51
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.
@@ -18,3 +18,17 @@ export function once(callback: Function): Function;
|
|
18
18
|
*/
|
19
19
|
export function debounce(fn: Function, intervalMs?: number): Function;
|
20
20
|
export function noop(): void;
|
21
|
+
/**
|
22
|
+
* Defer the execution of a function
|
23
|
+
* - Uses the best 'setImmediate' implementation supported by the current
|
24
|
+
* runtime environment
|
25
|
+
*
|
26
|
+
* @param {function} fn - Function to execute
|
27
|
+
*
|
28
|
+
* --
|
29
|
+
*
|
30
|
+
* @note setImmediate is preferred over nextTick
|
31
|
+
*
|
32
|
+
* @see https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
|
33
|
+
*/
|
34
|
+
export const defer: any;
|
@@ -12,12 +12,65 @@
|
|
12
12
|
*
|
13
13
|
* sayHelloOnce();
|
14
14
|
* sayHelloOnce();
|
15
|
+
*
|
16
|
+
* @example
|
17
|
+
*
|
18
|
+
* import { defer } from './process.js';
|
19
|
+
*
|
20
|
+
* defer( () => {
|
21
|
+
* console.log("The execution of the function has been defered");
|
22
|
+
* } );
|
15
23
|
*/
|
16
24
|
|
17
25
|
/* ------------------------------------------------------------------ Imports */
|
18
26
|
|
19
27
|
import * as expect from '../expect/index.js';
|
20
28
|
|
29
|
+
/* ---------------------------------------------------------------- Internals */
|
30
|
+
|
31
|
+
const NEXT_TICK_MESSAGE = 'hk-next-tick';
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Detect and return the most suitable setImmediate implementation available
|
35
|
+
* on the current platform
|
36
|
+
*/
|
37
|
+
function set_immediate_implementation() {
|
38
|
+
if (typeof global !== 'undefined') {
|
39
|
+
if (undefined !== global.setImmediate) {
|
40
|
+
return global.setImmediate;
|
41
|
+
}
|
42
|
+
} else if (typeof window !== 'undefined') {
|
43
|
+
if (window.postMessage && window.addEventListener) {
|
44
|
+
const queue = [];
|
45
|
+
|
46
|
+
window.addEventListener(
|
47
|
+
'message',
|
48
|
+
(event) => {
|
49
|
+
const source = event.source;
|
50
|
+
|
51
|
+
if ((source === window || source === null) && event.data === NEXT_TICK_MESSAGE) {
|
52
|
+
event.stopPropagation();
|
53
|
+
if (queue.length > 0) {
|
54
|
+
const fn = queue.shift();
|
55
|
+
fn();
|
56
|
+
}
|
57
|
+
}
|
58
|
+
},
|
59
|
+
true
|
60
|
+
);
|
61
|
+
|
62
|
+
return function nextTickUsingPostMessage(fn) {
|
63
|
+
expect.function(fn);
|
64
|
+
|
65
|
+
queue.push(fn);
|
66
|
+
window.postMessage(NEXT_TICK_MESSAGE, '*');
|
67
|
+
};
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
throw new Error('No suitable [setImmediate] implementation available');
|
72
|
+
}
|
73
|
+
|
21
74
|
/* ------------------------------------------------------------------ Exports */
|
22
75
|
|
23
76
|
/**
|
@@ -99,6 +152,23 @@ export function debounce(fn, intervalMs = 200) {
|
|
99
152
|
|
100
153
|
// -----------------------------------------------------------------------------
|
101
154
|
|
155
|
+
/**
|
156
|
+
* Defer the execution of a function
|
157
|
+
* - Uses the best 'setImmediate' implementation supported by the current
|
158
|
+
* runtime environment
|
159
|
+
*
|
160
|
+
* @param {function} fn - Function to execute
|
161
|
+
*
|
162
|
+
* --
|
163
|
+
*
|
164
|
+
* @note setImmediate is preferred over nextTick
|
165
|
+
*
|
166
|
+
* @see https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
|
167
|
+
*/
|
168
|
+
export const defer = set_immediate_implementation();
|
169
|
+
|
170
|
+
// -----------------------------------------------------------------------------
|
171
|
+
|
102
172
|
/**
|
103
173
|
* Adds a wrapper around a function that only calls the supplied function
|
104
174
|
* if the (first) supplied argument to the returned function is not `null`
|