@neovici/cosmoz-utils 6.22.2 → 6.24.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.
@@ -0,0 +1,3 @@
1
+ type Value = string | null | ((item: Element) => string | null);
2
+ export declare const forwardAttributes: (attrs: Record<string, Value>) => unknown;
3
+ export {};
@@ -0,0 +1,64 @@
1
+ import { noChange } from 'lit-html';
2
+ import { AsyncDirective, directive } from 'lit-html/async-directive.js';
3
+ import { invoke } from '../function';
4
+ const write = (item, name, value) => {
5
+ if (value == null) {
6
+ item.removeAttribute(name);
7
+ }
8
+ else {
9
+ item.setAttribute(name, value);
10
+ }
11
+ };
12
+ const forward = (attrs, item) => {
13
+ for (const [name, value] of Object.entries(attrs)) {
14
+ write(item, name, invoke(value, item));
15
+ }
16
+ };
17
+ /**
18
+ * Forwards attributes onto every flattened assigned element of the slot
19
+ * the directive is placed on — on each render (state changes re-render
20
+ * the slot) and on slot changes (different elements are assigned).
21
+ *
22
+ * Values are strings, nulls (removals), or functions of the assigned
23
+ * element. Placement is the configuration: the placed slot states which
24
+ * members receive the attributes. Slot semantics decide membership:
25
+ * slotting forwards through nested slots, wrappers do not — a
26
+ * non-forwarding wrapper receives the attributes itself without
27
+ * exposing state.
28
+ */
29
+ class ForwardAttributesDirective extends AsyncDirective {
30
+ _slot;
31
+ _attrs;
32
+ #forward = () => {
33
+ if (!this._slot || !this._attrs) {
34
+ return;
35
+ }
36
+ const items = this._slot.assignedElements({ flatten: true });
37
+ for (const item of items) {
38
+ forward(this._attrs, item);
39
+ }
40
+ };
41
+ #onSlotChange = () => this.#forward();
42
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
+ render(attrs) {
44
+ return noChange;
45
+ }
46
+ update(part, [attrs]) {
47
+ this._attrs = attrs;
48
+ const slot = part.element;
49
+ if (this._slot !== slot) {
50
+ this._slot?.removeEventListener('slotchange', this.#onSlotChange);
51
+ this._slot = slot;
52
+ slot.addEventListener('slotchange', this.#onSlotChange);
53
+ }
54
+ this.#forward();
55
+ return noChange;
56
+ }
57
+ disconnected() {
58
+ // the slot outlives renders; the listener is owned for its lifetime
59
+ // and removed when the part is cleared (template swap, disconnect)
60
+ this._slot?.removeEventListener('slotchange', this.#onSlotChange);
61
+ this._slot = undefined;
62
+ }
63
+ }
64
+ export const forwardAttributes = directive(ForwardAttributesDirective);
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.24.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",
@@ -60,10 +60,10 @@
60
60
  "./keybindings/*": "./dist/keybindings/*.js"
61
61
  },
62
62
  "dependencies": {
63
- "@pionjs/pion": "^2.7.1"
63
+ "@pionjs/pion": "^2.16.3"
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",