@neovici/cosmoz-utils 6.22.2 → 6.23.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/promise.d.ts CHANGED
@@ -8,6 +8,25 @@ type Predicate<T extends Event> = (e: T) => boolean;
8
8
  export declare const event$: <E extends Event, P extends Predicate<E> = Predicate<E>>(target: EventTarget, type: string, predicate?: P, timeout?: number) => Promise<E>;
9
9
  export declare const limit$: <T extends unknown[], P>(fn: (...args: T) => PromiseLike<P>, limit: number) => (...args: T) => Promise<P>;
10
10
  export declare const debounce$: <T extends unknown[], P>(fn: (...args: T) => P | PromiseLike<P>, ms?: number) => (...args: T) => Promise<P>;
11
+ /**
12
+ * Broadcasts the result of the wrapped function to every caller that is
13
+ * waiting for a result. Compose with `debounce$` to turn N concurrent calls
14
+ * into a single invocation that resolves all callers:
15
+ *
16
+ * ```js
17
+ * import { debounce$, share$ } from '@neovici/cosmoz-utils/promise';
18
+ *
19
+ * const statistics$ = share$(debounce$(fetchStatistics, 320));
20
+ * ```
21
+ *
22
+ * Since `debounce$` only resolves the last pending call, `share$` keeps
23
+ * track of pending callers and resolves them all when the inner promise
24
+ * resolves. In case of an error, only the last pending caller is rejected,
25
+ * to avoid amplifying one error into N rejections; earlier pending callers
26
+ * never settle. Only use with identical arguments: the first inner promise
27
+ * that settles broadcasts its result to all pending callers.
28
+ */
29
+ export declare const share$: <T extends unknown[], P>(fn: (...args: T) => P | PromiseLike<P>) => (...args: T) => Promise<P>;
11
30
  export declare const log$: <T extends unknown[], P>(fn: (...args: T) => PromiseLike<P>) => (...args: T) => PromiseLike<P>;
12
31
  export declare const retry$: <T extends unknown[], P>(fn: (...args: T) => P | PromiseLike<P>, n: number) => (...args: T) => Promise<P>;
13
32
  export declare const invoke$: <T, A extends unknown[]>(fn: T | ((...args: A) => T | PromiseLike<T>), ...args: A) => Promise<Awaited<T> | Awaited<T>>;
package/dist/promise.js CHANGED
@@ -63,6 +63,38 @@ export const debounce$ = (fn, ms) => {
63
63
  }, ms);
64
64
  });
65
65
  };
66
+ /**
67
+ * Broadcasts the result of the wrapped function to every caller that is
68
+ * waiting for a result. Compose with `debounce$` to turn N concurrent calls
69
+ * into a single invocation that resolves all callers:
70
+ *
71
+ * ```js
72
+ * import { debounce$, share$ } from '@neovici/cosmoz-utils/promise';
73
+ *
74
+ * const statistics$ = share$(debounce$(fetchStatistics, 320));
75
+ * ```
76
+ *
77
+ * Since `debounce$` only resolves the last pending call, `share$` keeps
78
+ * track of pending callers and resolves them all when the inner promise
79
+ * resolves. In case of an error, only the last pending caller is rejected,
80
+ * to avoid amplifying one error into N rejections; earlier pending callers
81
+ * never settle. Only use with identical arguments: the first inner promise
82
+ * that settles broadcasts its result to all pending callers.
83
+ */
84
+ export const share$ = (fn) => {
85
+ let pending = [];
86
+ return (...args) => new Promise((resolve, reject) => {
87
+ pending.push({ resolve, reject });
88
+ Promise.resolve(fn(...args)).then((result) => {
89
+ pending.forEach(({ resolve }) => resolve(result));
90
+ pending = [];
91
+ }, (error) => {
92
+ const last = pending.pop();
93
+ last?.reject(error);
94
+ pending = [];
95
+ });
96
+ });
97
+ };
66
98
  export const log$ = (fn) => (...args) => fn(...args).then((response) => {
67
99
  // eslint-disable-next-line no-console
68
100
  console.log(response);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "6.22.2",
3
+ "version": "6.23.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",
@@ -63,7 +63,7 @@
63
63
  "@pionjs/pion": "^2.7.1"
64
64
  },
65
65
  "devDependencies": {
66
- "@changesets/cli": "^2.27.0",
66
+ "@changesets/cli": "^3.0.2",
67
67
  "@commitlint/cli": "^19.0.0",
68
68
  "@commitlint/config-conventional": "^19.0.0",
69
69
  "@neovici/cfg": "^2.13.0",