@ls-stack/utils 3.17.0 → 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/dedent.cjs +42 -23
- package/lib/dedent.d.cts +12 -4
- package/lib/dedent.d.ts +12 -4
- package/lib/dedent.js +42 -23
- 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 +9 -1
package/lib/dedent.cjs
CHANGED
|
@@ -23,33 +23,52 @@ __export(dedent_exports, {
|
|
|
23
23
|
dedent: () => dedent
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(dedent_exports);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
var dedent = createDedent({});
|
|
27
|
+
function createDedent(options) {
|
|
28
|
+
d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
|
|
29
|
+
return d;
|
|
30
|
+
function d(strings, ...values) {
|
|
31
|
+
const raw = typeof strings === "string" ? [strings] : strings.raw;
|
|
32
|
+
const {
|
|
33
|
+
escapeSpecialCharacters = Array.isArray(strings),
|
|
34
|
+
trimWhitespace = true
|
|
35
|
+
} = options;
|
|
36
|
+
let result = "";
|
|
37
|
+
for (let i = 0; i < raw.length; i++) {
|
|
38
|
+
let next = raw[i];
|
|
39
|
+
if (escapeSpecialCharacters) {
|
|
40
|
+
next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\\{/g, "{");
|
|
41
|
+
}
|
|
42
|
+
result += next;
|
|
43
|
+
if (i < values.length) {
|
|
44
|
+
result += values[i];
|
|
45
|
+
}
|
|
33
46
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
const lines = result.split("\n");
|
|
48
|
+
let mindent = null;
|
|
49
|
+
for (const l of lines) {
|
|
50
|
+
const m = l.match(/^(\s+)\S+/);
|
|
51
|
+
if (m) {
|
|
52
|
+
const indent = m[1].length;
|
|
53
|
+
if (!mindent) {
|
|
54
|
+
mindent = indent;
|
|
55
|
+
} else {
|
|
56
|
+
mindent = Math.min(mindent, indent);
|
|
57
|
+
}
|
|
45
58
|
}
|
|
46
59
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
60
|
+
if (mindent !== null) {
|
|
61
|
+
const m = mindent;
|
|
62
|
+
result = lines.map((l) => l[0] === " " || l[0] === " " ? l.slice(m) : l).join("\n");
|
|
63
|
+
}
|
|
64
|
+
if (trimWhitespace) {
|
|
65
|
+
result = result.trim();
|
|
66
|
+
}
|
|
67
|
+
if (escapeSpecialCharacters) {
|
|
68
|
+
result = result.replace(/\\n/g, "\n");
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
51
71
|
}
|
|
52
|
-
return result.trim().replace(/\\n/g, "\n");
|
|
53
72
|
}
|
|
54
73
|
// Annotate the CommonJS export names for ESM import in node:
|
|
55
74
|
0 && (module.exports = {
|
package/lib/dedent.d.cts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
interface DedentOptions {
|
|
2
|
+
escapeSpecialCharacters?: boolean;
|
|
3
|
+
trimWhitespace?: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface Dedent {
|
|
6
|
+
(literals: string): string;
|
|
7
|
+
(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
8
|
+
withOptions: CreateDedent;
|
|
9
|
+
}
|
|
10
|
+
type CreateDedent = (options: DedentOptions) => Dedent;
|
|
11
|
+
declare const dedent: Dedent;
|
|
4
12
|
|
|
5
|
-
export { dedent };
|
|
13
|
+
export { type CreateDedent, type Dedent, type DedentOptions, dedent };
|
package/lib/dedent.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
interface DedentOptions {
|
|
2
|
+
escapeSpecialCharacters?: boolean;
|
|
3
|
+
trimWhitespace?: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface Dedent {
|
|
6
|
+
(literals: string): string;
|
|
7
|
+
(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
8
|
+
withOptions: CreateDedent;
|
|
9
|
+
}
|
|
10
|
+
type CreateDedent = (options: DedentOptions) => Dedent;
|
|
11
|
+
declare const dedent: Dedent;
|
|
4
12
|
|
|
5
|
-
export { dedent };
|
|
13
|
+
export { type CreateDedent, type Dedent, type DedentOptions, dedent };
|
package/lib/dedent.js
CHANGED
|
@@ -1,31 +1,50 @@
|
|
|
1
1
|
// src/dedent.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
var dedent = createDedent({});
|
|
3
|
+
function createDedent(options) {
|
|
4
|
+
d.withOptions = (newOptions) => createDedent({ ...options, ...newOptions });
|
|
5
|
+
return d;
|
|
6
|
+
function d(strings, ...values) {
|
|
7
|
+
const raw = typeof strings === "string" ? [strings] : strings.raw;
|
|
8
|
+
const {
|
|
9
|
+
escapeSpecialCharacters = Array.isArray(strings),
|
|
10
|
+
trimWhitespace = true
|
|
11
|
+
} = options;
|
|
12
|
+
let result = "";
|
|
13
|
+
for (let i = 0; i < raw.length; i++) {
|
|
14
|
+
let next = raw[i];
|
|
15
|
+
if (escapeSpecialCharacters) {
|
|
16
|
+
next = next.replace(/\\\n[ \t]*/g, "").replace(/\\`/g, "`").replace(/\\\$/g, "$").replace(/\\\{/g, "{");
|
|
17
|
+
}
|
|
18
|
+
result += next;
|
|
19
|
+
if (i < values.length) {
|
|
20
|
+
result += values[i];
|
|
21
|
+
}
|
|
9
22
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
const lines = result.split("\n");
|
|
24
|
+
let mindent = null;
|
|
25
|
+
for (const l of lines) {
|
|
26
|
+
const m = l.match(/^(\s+)\S+/);
|
|
27
|
+
if (m) {
|
|
28
|
+
const indent = m[1].length;
|
|
29
|
+
if (!mindent) {
|
|
30
|
+
mindent = indent;
|
|
31
|
+
} else {
|
|
32
|
+
mindent = Math.min(mindent, indent);
|
|
33
|
+
}
|
|
21
34
|
}
|
|
22
35
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
36
|
+
if (mindent !== null) {
|
|
37
|
+
const m = mindent;
|
|
38
|
+
result = lines.map((l) => l[0] === " " || l[0] === " " ? l.slice(m) : l).join("\n");
|
|
39
|
+
}
|
|
40
|
+
if (trimWhitespace) {
|
|
41
|
+
result = result.trim();
|
|
42
|
+
}
|
|
43
|
+
if (escapeSpecialCharacters) {
|
|
44
|
+
result = result.replace(/\\n/g, "\n");
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
27
47
|
}
|
|
28
|
-
return result.trim().replace(/\\n/g, "\n");
|
|
29
48
|
}
|
|
30
49
|
export {
|
|
31
50
|
dedent
|
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"
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"import": "./lib/main.js",
|
|
19
19
|
"require": "./lib/main.cjs"
|
|
20
20
|
},
|
|
21
|
+
"./__snapshots__": {
|
|
22
|
+
"import": "./lib/__snapshots__.js",
|
|
23
|
+
"require": "./lib/__snapshots__.cjs"
|
|
24
|
+
},
|
|
21
25
|
"./arrayUtils": {
|
|
22
26
|
"import": "./lib/arrayUtils.js",
|
|
23
27
|
"require": "./lib/arrayUtils.cjs"
|
|
@@ -150,6 +154,10 @@
|
|
|
150
154
|
"import": "./lib/time.js",
|
|
151
155
|
"require": "./lib/time.cjs"
|
|
152
156
|
},
|
|
157
|
+
"./timers": {
|
|
158
|
+
"import": "./lib/timers.js",
|
|
159
|
+
"require": "./lib/timers.cjs"
|
|
160
|
+
},
|
|
153
161
|
"./tsResult": {
|
|
154
162
|
"import": "./lib/tsResult.js",
|
|
155
163
|
"require": "./lib/tsResult.cjs"
|