@sohanemon/utils 5.1.6 → 5.1.7

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.
@@ -1,5 +1,6 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
- export * from './shield';
4
3
  export * from './poll';
4
+ export * from './schedule';
5
+ export * from './shield';
5
6
  export * from './utils';
@@ -1,5 +1,6 @@
1
1
  export * from './cookie';
2
2
  export * from './object';
3
- export * from './shield';
4
3
  export * from './poll';
4
+ export * from './schedule';
5
+ export * from './shield';
5
6
  export * from './utils';
@@ -0,0 +1,11 @@
1
+ type Task = () => Promise<void> | void;
2
+ interface ScheduleOpts {
3
+ retry?: number;
4
+ delay?: number;
5
+ }
6
+ /**
7
+ * Runs a function asynchronously in the background.
8
+ * Returns immediately, retries on failure if configured.
9
+ */
10
+ export declare function schedule(task: Task, options?: ScheduleOpts): void;
11
+ export {};
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Runs a function asynchronously in the background.
3
+ * Returns immediately, retries on failure if configured.
4
+ */
5
+ export function schedule(task, options = {}) {
6
+ const { retry = 0, delay = 0 } = options;
7
+ const attempt = async (triesLeft) => {
8
+ try {
9
+ await task();
10
+ }
11
+ catch (err) {
12
+ console.log('⚡[schedule.ts] err:', err);
13
+ if (triesLeft > 0) {
14
+ console.log(`⚡[schedule.ts] Retrying in ${delay}ms...`);
15
+ setTimeout(() => attempt(triesLeft - 1), delay);
16
+ }
17
+ }
18
+ };
19
+ // Schedule immediately
20
+ setTimeout(() => attempt(retry), 0);
21
+ }
@@ -4,7 +4,7 @@ export type IMPLIES<T, U> = T extends U ? true : false;
4
4
  export type XOR_Binary<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
5
5
  export type XNOR_Binary<T, U> = (T & U) | (Without<T, U> & Without<U, T>);
6
6
  /**
7
- * Computes a type-level AND for a tuple of types.
7
+ * Computes a type-level AND (all must true) for a tuple of types.
8
8
  *
9
9
  * Truth table for 3 arguments:
10
10
  *
@@ -22,7 +22,7 @@ export type XNOR_Binary<T, U> = (T & U) | (Without<T, U> & Without<U, T>);
22
22
  */
23
23
  export type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
24
24
  /**
25
- * Computes a type-level OR for a tuple of types.
25
+ * Computes a type-level OR (At least one) for a tuple of types.
26
26
  *
27
27
  * Truth table for 3 arguments:
28
28
  *
@@ -40,7 +40,7 @@ export type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends a
40
40
  */
41
41
  export type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
42
42
  /**
43
- * Computes a type-level XOR for a tuple of types.
43
+ * Computes a type-level XOR (only one/odd) for a tuple of types.
44
44
  *
45
45
  * Truth table for 3 arguments:
46
46
  *
@@ -58,7 +58,7 @@ export type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends an
58
58
  */
59
59
  export type XOR<T extends any[]> = T extends [infer F, ...infer R] ? R extends [infer S, ...infer Rest] ? XOR<[XOR_Binary<F, S>, ...Rest]> : F : never;
60
60
  /**
61
- * Computes a type-level XNOR for a tuple of types.
61
+ * Computes a type-level XNOR (All or None true) for a tuple of types.
62
62
  *
63
63
  * Truth table for 3 arguments:
64
64
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "5.1.6",
3
+ "version": "5.1.7",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",