@ls-stack/utils 3.17.1 → 3.18.0
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/timers.cjs +93 -0
- package/lib/timers.d.cts +110 -0
- package/lib/timers.d.ts +110 -0
- package/lib/timers.js +65 -0
- package/package.json +5 -1
package/lib/timers.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/timers.ts
|
|
21
|
+
var timers_exports = {};
|
|
22
|
+
__export(timers_exports, {
|
|
23
|
+
createDebouncedTimeout: () => createDebouncedTimeout,
|
|
24
|
+
createInterval: () => createInterval,
|
|
25
|
+
createTimeout: () => createTimeout,
|
|
26
|
+
createWaitUntil: () => createWaitUntil
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(timers_exports);
|
|
29
|
+
function createTimeout(ms, callback) {
|
|
30
|
+
const timeoutId = setTimeout(callback, ms);
|
|
31
|
+
let isCleaned = false;
|
|
32
|
+
return () => {
|
|
33
|
+
if (isCleaned) return;
|
|
34
|
+
clearTimeout(timeoutId);
|
|
35
|
+
isCleaned = true;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createInterval(ms, callback) {
|
|
39
|
+
const intervalId = setInterval(callback, ms);
|
|
40
|
+
let isCleaned = false;
|
|
41
|
+
return () => {
|
|
42
|
+
if (isCleaned) return;
|
|
43
|
+
clearInterval(intervalId);
|
|
44
|
+
isCleaned = true;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function createDebouncedTimeout(ms, callback) {
|
|
48
|
+
let cleanupTimer = null;
|
|
49
|
+
return {
|
|
50
|
+
clean: () => {
|
|
51
|
+
cleanupTimer?.();
|
|
52
|
+
},
|
|
53
|
+
call: () => {
|
|
54
|
+
cleanupTimer?.();
|
|
55
|
+
cleanupTimer = createTimeout(ms, () => {
|
|
56
|
+
callback();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function createWaitUntil({
|
|
62
|
+
condition,
|
|
63
|
+
maxWaitMs,
|
|
64
|
+
callback,
|
|
65
|
+
checkIntervalMs = 20
|
|
66
|
+
}) {
|
|
67
|
+
let cleanCheckTimeout = null;
|
|
68
|
+
let cleanMaxWaitTimeout = null;
|
|
69
|
+
cleanMaxWaitTimeout = createTimeout(maxWaitMs, () => {
|
|
70
|
+
cleanCheckTimeout?.();
|
|
71
|
+
});
|
|
72
|
+
function check() {
|
|
73
|
+
const result = condition();
|
|
74
|
+
if (result) {
|
|
75
|
+
cleanMaxWaitTimeout?.();
|
|
76
|
+
callback(result);
|
|
77
|
+
} else {
|
|
78
|
+
cleanCheckTimeout = createTimeout(checkIntervalMs, check);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
check();
|
|
82
|
+
return () => {
|
|
83
|
+
cleanMaxWaitTimeout();
|
|
84
|
+
cleanCheckTimeout?.();
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
createDebouncedTimeout,
|
|
90
|
+
createInterval,
|
|
91
|
+
createTimeout,
|
|
92
|
+
createWaitUntil
|
|
93
|
+
});
|
package/lib/timers.d.cts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
type CleanupTimer = () => void;
|
|
2
|
+
/**
|
|
3
|
+
* Creates a timeout with automatic cleanup capability.
|
|
4
|
+
*
|
|
5
|
+
* Returns a cleanup function that can be called to cancel the timeout.
|
|
6
|
+
* The cleanup function is idempotent - calling it multiple times is safe.
|
|
7
|
+
*
|
|
8
|
+
* @param ms - The timeout duration in milliseconds
|
|
9
|
+
* @param callback - The function to execute when the timeout completes
|
|
10
|
+
* @returns A cleanup function that cancels the timeout when called
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const cleanup = createTimeout(1000, () => {
|
|
15
|
+
* console.log('Timeout completed');
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Cancel the timeout before it completes
|
|
19
|
+
* cleanup();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function createTimeout(ms: number, callback: () => void): CleanupTimer;
|
|
23
|
+
/**
|
|
24
|
+
* Creates an interval with automatic cleanup capability.
|
|
25
|
+
*
|
|
26
|
+
* Returns a cleanup function that can be called to cancel the interval.
|
|
27
|
+
* The cleanup function is idempotent - calling it multiple times is safe.
|
|
28
|
+
*
|
|
29
|
+
* @param ms - The interval duration in milliseconds
|
|
30
|
+
* @param callback - The function to execute on each interval tick
|
|
31
|
+
* @returns A cleanup function that cancels the interval when called
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const cleanup = createInterval(1000, () => {
|
|
36
|
+
* console.log('Interval tick');
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Stop the interval
|
|
40
|
+
* cleanup();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function createInterval(ms: number, callback: () => void): CleanupTimer;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a timeout that prevents concurrent executions.
|
|
46
|
+
*
|
|
47
|
+
* Each call to the `call` function will cancel any previous pending timeout
|
|
48
|
+
* and start a new one. This is useful for debouncing or ensuring only the
|
|
49
|
+
* last call executes after a delay.
|
|
50
|
+
*
|
|
51
|
+
* @param ms - The timeout duration in milliseconds
|
|
52
|
+
* @param callback - The function to execute when the timeout completes
|
|
53
|
+
* @returns An object with `call` to trigger the timeout and `clean` to cancel it
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const { call, clean } = createDebouncedTimeout(1000, () => {
|
|
58
|
+
* console.log('Only the last call executes');
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* call(); // This will be cancelled
|
|
62
|
+
* call(); // This will be cancelled
|
|
63
|
+
* call(); // Only this one will execute after 1000ms
|
|
64
|
+
*
|
|
65
|
+
* // Or cancel all pending timeouts
|
|
66
|
+
* clean();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function createDebouncedTimeout(ms: number, callback: () => void): {
|
|
70
|
+
call: () => void;
|
|
71
|
+
clean: CleanupTimer;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Creates a timeout that waits for a condition to become true.
|
|
75
|
+
*
|
|
76
|
+
* Polls the condition function at regular intervals until it returns a truthy value,
|
|
77
|
+
* then calls the callback with that value. If the condition doesn't become true
|
|
78
|
+
* within the maximum wait time, the timeout expires without calling the callback.
|
|
79
|
+
*
|
|
80
|
+
* @template T - The type of value returned by the condition function when true
|
|
81
|
+
* @param options - Configuration options
|
|
82
|
+
* @param options.condition - Function that returns false or a truthy value when the condition is met
|
|
83
|
+
* @param options.maxWaitMs - Maximum time to wait for the condition in milliseconds
|
|
84
|
+
* @param options.callback - Function to call when the condition becomes true
|
|
85
|
+
* @param options.checkIntervalMs - How often to check the condition in milliseconds (default: 20)
|
|
86
|
+
* @returns A cleanup function that cancels the condition timeout
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const cleanup = createWaitUntil({
|
|
91
|
+
* condition: () => document.getElementById('myElement'),
|
|
92
|
+
* maxWaitMs: 5000,
|
|
93
|
+
* callback: (element) => {
|
|
94
|
+
* console.log('Element found:', element);
|
|
95
|
+
* },
|
|
96
|
+
* checkIntervalMs: 50
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // Cancel the condition check
|
|
100
|
+
* cleanup();
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, maxWaitMs, callback, checkIntervalMs, }: {
|
|
104
|
+
condition: () => false | T;
|
|
105
|
+
maxWaitMs: number;
|
|
106
|
+
callback: (value: T) => void;
|
|
107
|
+
checkIntervalMs?: number;
|
|
108
|
+
}): CleanupTimer;
|
|
109
|
+
|
|
110
|
+
export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil };
|
package/lib/timers.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
type CleanupTimer = () => void;
|
|
2
|
+
/**
|
|
3
|
+
* Creates a timeout with automatic cleanup capability.
|
|
4
|
+
*
|
|
5
|
+
* Returns a cleanup function that can be called to cancel the timeout.
|
|
6
|
+
* The cleanup function is idempotent - calling it multiple times is safe.
|
|
7
|
+
*
|
|
8
|
+
* @param ms - The timeout duration in milliseconds
|
|
9
|
+
* @param callback - The function to execute when the timeout completes
|
|
10
|
+
* @returns A cleanup function that cancels the timeout when called
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const cleanup = createTimeout(1000, () => {
|
|
15
|
+
* console.log('Timeout completed');
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Cancel the timeout before it completes
|
|
19
|
+
* cleanup();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare function createTimeout(ms: number, callback: () => void): CleanupTimer;
|
|
23
|
+
/**
|
|
24
|
+
* Creates an interval with automatic cleanup capability.
|
|
25
|
+
*
|
|
26
|
+
* Returns a cleanup function that can be called to cancel the interval.
|
|
27
|
+
* The cleanup function is idempotent - calling it multiple times is safe.
|
|
28
|
+
*
|
|
29
|
+
* @param ms - The interval duration in milliseconds
|
|
30
|
+
* @param callback - The function to execute on each interval tick
|
|
31
|
+
* @returns A cleanup function that cancels the interval when called
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const cleanup = createInterval(1000, () => {
|
|
36
|
+
* console.log('Interval tick');
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Stop the interval
|
|
40
|
+
* cleanup();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function createInterval(ms: number, callback: () => void): CleanupTimer;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a timeout that prevents concurrent executions.
|
|
46
|
+
*
|
|
47
|
+
* Each call to the `call` function will cancel any previous pending timeout
|
|
48
|
+
* and start a new one. This is useful for debouncing or ensuring only the
|
|
49
|
+
* last call executes after a delay.
|
|
50
|
+
*
|
|
51
|
+
* @param ms - The timeout duration in milliseconds
|
|
52
|
+
* @param callback - The function to execute when the timeout completes
|
|
53
|
+
* @returns An object with `call` to trigger the timeout and `clean` to cancel it
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const { call, clean } = createDebouncedTimeout(1000, () => {
|
|
58
|
+
* console.log('Only the last call executes');
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* call(); // This will be cancelled
|
|
62
|
+
* call(); // This will be cancelled
|
|
63
|
+
* call(); // Only this one will execute after 1000ms
|
|
64
|
+
*
|
|
65
|
+
* // Or cancel all pending timeouts
|
|
66
|
+
* clean();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function createDebouncedTimeout(ms: number, callback: () => void): {
|
|
70
|
+
call: () => void;
|
|
71
|
+
clean: CleanupTimer;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Creates a timeout that waits for a condition to become true.
|
|
75
|
+
*
|
|
76
|
+
* Polls the condition function at regular intervals until it returns a truthy value,
|
|
77
|
+
* then calls the callback with that value. If the condition doesn't become true
|
|
78
|
+
* within the maximum wait time, the timeout expires without calling the callback.
|
|
79
|
+
*
|
|
80
|
+
* @template T - The type of value returned by the condition function when true
|
|
81
|
+
* @param options - Configuration options
|
|
82
|
+
* @param options.condition - Function that returns false or a truthy value when the condition is met
|
|
83
|
+
* @param options.maxWaitMs - Maximum time to wait for the condition in milliseconds
|
|
84
|
+
* @param options.callback - Function to call when the condition becomes true
|
|
85
|
+
* @param options.checkIntervalMs - How often to check the condition in milliseconds (default: 20)
|
|
86
|
+
* @returns A cleanup function that cancels the condition timeout
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const cleanup = createWaitUntil({
|
|
91
|
+
* condition: () => document.getElementById('myElement'),
|
|
92
|
+
* maxWaitMs: 5000,
|
|
93
|
+
* callback: (element) => {
|
|
94
|
+
* console.log('Element found:', element);
|
|
95
|
+
* },
|
|
96
|
+
* checkIntervalMs: 50
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // Cancel the condition check
|
|
100
|
+
* cleanup();
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, maxWaitMs, callback, checkIntervalMs, }: {
|
|
104
|
+
condition: () => false | T;
|
|
105
|
+
maxWaitMs: number;
|
|
106
|
+
callback: (value: T) => void;
|
|
107
|
+
checkIntervalMs?: number;
|
|
108
|
+
}): CleanupTimer;
|
|
109
|
+
|
|
110
|
+
export { type CleanupTimer, createDebouncedTimeout, createInterval, createTimeout, createWaitUntil };
|
package/lib/timers.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/timers.ts
|
|
2
|
+
function createTimeout(ms, callback) {
|
|
3
|
+
const timeoutId = setTimeout(callback, ms);
|
|
4
|
+
let isCleaned = false;
|
|
5
|
+
return () => {
|
|
6
|
+
if (isCleaned) return;
|
|
7
|
+
clearTimeout(timeoutId);
|
|
8
|
+
isCleaned = true;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function createInterval(ms, callback) {
|
|
12
|
+
const intervalId = setInterval(callback, ms);
|
|
13
|
+
let isCleaned = false;
|
|
14
|
+
return () => {
|
|
15
|
+
if (isCleaned) return;
|
|
16
|
+
clearInterval(intervalId);
|
|
17
|
+
isCleaned = true;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function createDebouncedTimeout(ms, callback) {
|
|
21
|
+
let cleanupTimer = null;
|
|
22
|
+
return {
|
|
23
|
+
clean: () => {
|
|
24
|
+
cleanupTimer?.();
|
|
25
|
+
},
|
|
26
|
+
call: () => {
|
|
27
|
+
cleanupTimer?.();
|
|
28
|
+
cleanupTimer = createTimeout(ms, () => {
|
|
29
|
+
callback();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function createWaitUntil({
|
|
35
|
+
condition,
|
|
36
|
+
maxWaitMs,
|
|
37
|
+
callback,
|
|
38
|
+
checkIntervalMs = 20
|
|
39
|
+
}) {
|
|
40
|
+
let cleanCheckTimeout = null;
|
|
41
|
+
let cleanMaxWaitTimeout = null;
|
|
42
|
+
cleanMaxWaitTimeout = createTimeout(maxWaitMs, () => {
|
|
43
|
+
cleanCheckTimeout?.();
|
|
44
|
+
});
|
|
45
|
+
function check() {
|
|
46
|
+
const result = condition();
|
|
47
|
+
if (result) {
|
|
48
|
+
cleanMaxWaitTimeout?.();
|
|
49
|
+
callback(result);
|
|
50
|
+
} else {
|
|
51
|
+
cleanCheckTimeout = createTimeout(checkIntervalMs, check);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
check();
|
|
55
|
+
return () => {
|
|
56
|
+
cleanMaxWaitTimeout();
|
|
57
|
+
cleanCheckTimeout?.();
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
createDebouncedTimeout,
|
|
62
|
+
createInterval,
|
|
63
|
+
createTimeout,
|
|
64
|
+
createWaitUntil
|
|
65
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.18.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib"
|
|
@@ -154,6 +154,10 @@
|
|
|
154
154
|
"import": "./lib/time.js",
|
|
155
155
|
"require": "./lib/time.cjs"
|
|
156
156
|
},
|
|
157
|
+
"./timers": {
|
|
158
|
+
"import": "./lib/timers.js",
|
|
159
|
+
"require": "./lib/timers.cjs"
|
|
160
|
+
},
|
|
157
161
|
"./tsResult": {
|
|
158
162
|
"import": "./lib/tsResult.js",
|
|
159
163
|
"require": "./lib/tsResult.cjs"
|