@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.
@@ -9,6 +9,7 @@ export function shield(operation) {
9
9
  return [null, data];
10
10
  }
11
11
  catch (error) {
12
+ console.log(`\x1b[31m🛡 [shield]\x1b[0m ${operation.name} failed →`, error);
12
13
  return [error, null];
13
14
  }
14
15
  }
@@ -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
- * @returns - A Promise that resolves after the specified time
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<unknown>;
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;
@@ -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
- * @returns - A Promise that resolves after the specified time
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) => setTimeout(resolve, time));
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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.0.8",
3
+ "version": "5.1.0",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",