@sohanemon/utils 5.0.8 → 5.1.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/dist/functions/shield.js
CHANGED
|
@@ -86,10 +86,13 @@ export declare const svgToBase64: (str: string) => string;
|
|
|
86
86
|
/**
|
|
87
87
|
* Pauses execution for the specified time.
|
|
88
88
|
*
|
|
89
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
90
|
+
*
|
|
89
91
|
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
90
|
-
* @
|
|
92
|
+
* @param signal - Optional AbortSignal to cancel the sleep early
|
|
93
|
+
* @returns - A Promise that resolves after the specified time or when aborted
|
|
91
94
|
*/
|
|
92
|
-
export declare const sleep: (time?: number) => Promise<
|
|
95
|
+
export declare const sleep: (time?: number, signal?: AbortSignal) => Promise<void>;
|
|
93
96
|
type DebouncedFunction<F extends (...args: any[]) => any> = {
|
|
94
97
|
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
95
98
|
readonly isPending: boolean;
|
package/dist/functions/utils.js
CHANGED
|
@@ -158,10 +158,32 @@ export const svgToBase64 = (str) => isSSR ? Buffer.from(str).toString('base64')
|
|
|
158
158
|
/**
|
|
159
159
|
* Pauses execution for the specified time.
|
|
160
160
|
*
|
|
161
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
162
|
+
*
|
|
161
163
|
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
162
|
-
* @
|
|
164
|
+
* @param signal - Optional AbortSignal to cancel the sleep early
|
|
165
|
+
* @returns - A Promise that resolves after the specified time or when aborted
|
|
163
166
|
*/
|
|
164
|
-
export const sleep = (time = 1000) => new Promise((resolve) =>
|
|
167
|
+
export const sleep = (time = 1000, signal) => new Promise((resolve) => {
|
|
168
|
+
if (signal?.aborted)
|
|
169
|
+
return resolve();
|
|
170
|
+
const id = setTimeout(() => {
|
|
171
|
+
cleanup();
|
|
172
|
+
resolve();
|
|
173
|
+
}, time);
|
|
174
|
+
function onAbort() {
|
|
175
|
+
clearTimeout(id);
|
|
176
|
+
cleanup();
|
|
177
|
+
resolve();
|
|
178
|
+
}
|
|
179
|
+
function cleanup() {
|
|
180
|
+
signal?.removeEventListener('abort', onAbort);
|
|
181
|
+
}
|
|
182
|
+
// only add listener if a signal was supplied
|
|
183
|
+
if (signal) {
|
|
184
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
185
|
+
}
|
|
186
|
+
});
|
|
165
187
|
/**
|
|
166
188
|
* Creates a debounced function that delays invoking the provided function until
|
|
167
189
|
* after the specified `wait` time has elapsed since the last invocation.
|