@lexriver/dome 2.0.1 → 2.0.3

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.
Files changed (43) hide show
  1. package/out/index.cjs +802 -0
  2. package/out/index.mjs +1987 -0
  3. package/out/src/AnimatedArray.d.ts +29 -0
  4. package/out/src/AnimatedTable.d.mts +2 -1
  5. package/out/src/AnimatedTable.d.ts +39 -0
  6. package/out/src/AnimatedTable.mjs +2 -1
  7. package/out/src/AnimatedText.d.mts +1 -1
  8. package/out/src/AnimatedText.d.ts +13 -0
  9. package/out/src/AnimatedText.mjs +1 -1
  10. package/out/src/Animation.d.ts +4 -0
  11. package/out/src/Dome.d.ts +9 -0
  12. package/out/src/Dome.mjs +1 -1
  13. package/out/src/DomeComponent.d.mts +4 -1
  14. package/out/src/DomeComponent.d.ts +25 -0
  15. package/out/src/DomeComponent.mjs +24 -3
  16. package/out/src/DomeManipulator.d.mts +4 -0
  17. package/out/src/DomeManipulator.d.ts +52 -0
  18. package/out/src/DomeManipulator.mjs +24 -13
  19. package/out/src/DomeRouter.console-test.d.ts +1 -0
  20. package/out/src/DomeRouter.d.ts +32 -0
  21. package/out/src/LongestCommonSubsequence.d.ts +15 -0
  22. package/out/src/concurrent-updates.test.d.mts +1 -0
  23. package/out/src/concurrent-updates.test.d.ts +1 -0
  24. package/out/src/concurrent-updates.test.mjs +203 -0
  25. package/out/src/index.d.ts +11 -0
  26. package/out/src/temp-test.d.ts +1 -0
  27. package/package.json +20 -3
  28. package/out/vitest.config.d.ts +0 -2
  29. package/out/vitest.config.js +0 -9
  30. package/src/AnimatedArray.mts +0 -74
  31. package/src/AnimatedTable.mts +0 -120
  32. package/src/AnimatedText.mts +0 -30
  33. package/src/Animation.mts +0 -4
  34. package/src/Dome.mts +0 -346
  35. package/src/DomeComponent.mts +0 -63
  36. package/src/DomeManipulator.mts +0 -380
  37. package/src/DomeRouter.console-test.mts +0 -52
  38. package/src/DomeRouter.mts +0 -287
  39. package/src/LongestCommonSubsequence.mts +0 -201
  40. package/src/index.mts +0 -12
  41. package/src/temp-test.mts +0 -23
  42. package/tsconfig.json +0 -59
  43. package/vitest.config.ts +0 -10
@@ -0,0 +1,29 @@
1
+ import { Animation } from './Animation';
2
+ interface KeyToElementPair {
3
+ key: string;
4
+ element: HTMLElement;
5
+ }
6
+ export declare class AnimatedArray<T> {
7
+ protected params: {
8
+ parentElement?: HTMLElement;
9
+ array?: T[];
10
+ getKey: (o: T) => string;
11
+ getHtmlElement: (o: T) => HTMLElement;
12
+ animationShow?: Animation;
13
+ animationHide?: Animation;
14
+ emptyList?: HTMLElement;
15
+ };
16
+ arrayOfKeyToElement: KeyToElementPair[];
17
+ constructor(params: {
18
+ parentElement?: HTMLElement;
19
+ array?: T[];
20
+ getKey: (o: T) => string;
21
+ getHtmlElement: (o: T) => HTMLElement;
22
+ animationShow?: Animation;
23
+ animationHide?: Animation;
24
+ emptyList?: HTMLElement;
25
+ });
26
+ get size(): number;
27
+ update(array: T[], parentElement?: HTMLElement | Element): void;
28
+ }
29
+ export {};
@@ -1,6 +1,7 @@
1
1
  import { ObservableVariable } from '@lexriver/observable';
2
2
  import { Animation } from './Animation.mjs';
3
- import { AnimatedArray, DomeComponent } from "./index.mjs";
3
+ import { AnimatedArray } from "./AnimatedArray.mjs";
4
+ import { DomeComponent } from "./DomeComponent.mjs";
4
5
  interface Attrs<T> {
5
6
  isLoadingO?: ObservableVariable<boolean>;
6
7
  itemsO: ObservableVariable<T[]>;
@@ -0,0 +1,39 @@
1
+ import { ObservableVariable } from '@lexriver/observable';
2
+ import { Animation } from './Animation';
3
+ import { AnimatedArray } from "./AnimatedArray";
4
+ import { DomeComponent } from "./DomeComponent";
5
+ interface Attrs<T> {
6
+ isLoadingO?: ObservableVariable<boolean>;
7
+ itemsO: ObservableVariable<T[]>;
8
+ animationShowRow: Animation;
9
+ animationHideRow: Animation;
10
+ animationHideTable?: Animation;
11
+ animationShowTable?: Animation;
12
+ animationHideEmptyList?: Animation;
13
+ animationShowEmptyList?: Animation;
14
+ animationShowLoading?: Animation;
15
+ animationHideLoading?: Animation;
16
+ getKey: (item: T) => string;
17
+ rootElement?: HTMLElement;
18
+ tableElement?: HTMLElement;
19
+ tableBody?: HTMLElement;
20
+ renderTableHead?: (items: T[]) => HTMLElement;
21
+ renderTableRow: (item: T) => HTMLElement;
22
+ renderTableFooter?: (items: T[]) => HTMLElement;
23
+ renderEmptyList: () => HTMLElement;
24
+ renderLoading?: () => HTMLElement;
25
+ }
26
+ export declare class AnimatedTable<T> extends DomeComponent<Attrs<T>> {
27
+ refRoot: HTMLElement;
28
+ refTable: HTMLElement;
29
+ refTableHead: Element;
30
+ refTableBody: HTMLElement;
31
+ refTableFooter: Element;
32
+ refEmptyList: Element;
33
+ refLoading: Element;
34
+ animatedArray: AnimatedArray<T>;
35
+ render(): HTMLElement;
36
+ afterRender(): void;
37
+ updateAsync(): Promise<void>;
38
+ }
39
+ export {};
@@ -1,5 +1,6 @@
1
1
  import { DomeManipulator } from "./DomeManipulator.mjs";
2
- import { AnimatedArray, DomeComponent } from "./index.mjs";
2
+ import { AnimatedArray } from "./AnimatedArray.mjs";
3
+ import { DomeComponent } from "./DomeComponent.mjs";
3
4
  export class AnimatedTable extends DomeComponent {
4
5
  // rootElement
5
6
  // table
@@ -1,6 +1,6 @@
1
1
  import { ObservableVariable } from "@lexriver/observable";
2
2
  import { CssClass } from "./DomeManipulator.mjs";
3
- import { DomeComponent } from "./index.mjs";
3
+ import { DomeComponent } from "./DomeComponent.mjs";
4
4
  interface Attrs {
5
5
  textO: ObservableVariable<string>;
6
6
  tag?: string;
@@ -0,0 +1,13 @@
1
+ import { ObservableVariable } from "@lexriver/observable";
2
+ import { CssClass } from "./DomeManipulator";
3
+ import { DomeComponent } from "./DomeComponent";
4
+ interface Attrs {
5
+ textO: ObservableVariable<string>;
6
+ tag?: string;
7
+ class?: CssClass;
8
+ }
9
+ export declare class AnimatedText extends DomeComponent<Attrs> {
10
+ render(): HTMLElement;
11
+ updateAsync(): Promise<void>;
12
+ }
13
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { DomeManipulator } from "./DomeManipulator.mjs";
2
- import { DomeComponent } from "./index.mjs";
2
+ import { DomeComponent } from "./DomeComponent.mjs";
3
3
  export class AnimatedText extends DomeComponent {
4
4
  render() {
5
5
  //const result = this.attrs.tag ? document.createElement(this.attrs.tag) : document.createElement('span')
@@ -0,0 +1,4 @@
1
+ export interface Animation {
2
+ cssClassName: string;
3
+ timeMs: number;
4
+ }
@@ -0,0 +1,9 @@
1
+ export declare function h(tagName: any, attrs: any, ...childrenArgs: any[]): any;
2
+ export declare const React: {
3
+ createElement: typeof h;
4
+ Fragment: {
5
+ new (): DocumentFragment;
6
+ prototype: DocumentFragment;
7
+ } | (() => void);
8
+ };
9
+ export default React;
package/out/src/Dome.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // inspiration: https://github.com/vadimdemedes/dom-chef/blob/master/index.js
2
- const svgTagNames = require('svg-tag-names');
2
+ import { svgTagNames } from 'svg-tag-names';
3
3
  import { DataTypes } from '@lexriver/data-types';
4
4
  import { checkIfObservable } from '@lexriver/observable';
5
5
  import { DomeManipulator } from './DomeManipulator.mjs';
@@ -8,13 +8,16 @@ export declare abstract class DomeComponent<Attrs> {
8
8
  attrs: Attrs & InternalAttrs;
9
9
  children: any;
10
10
  rootElement: Element | HTMLElement;
11
+ private updateInProgress;
12
+ private updateRequested;
11
13
  constructor(attrs: Attrs & InternalAttrs, children: any);
12
14
  protected init(): void;
13
15
  protected abstract render(): HTMLElement;
14
16
  protected afterRender(): void;
15
17
  updateAsync(): Promise<void>;
18
+ private runScheduledUpdateAsync;
16
19
  scheduleUpdate: {
17
- (this: unknown, ...args: [] & any[]): Promise<void>;
20
+ (this: unknown, ...args: [] & any[]): Promise<Promise<void>>;
18
21
  cancel: (reason?: any) => void;
19
22
  };
20
23
  protected afterUpdate(): void;
@@ -0,0 +1,25 @@
1
+ import { Animation } from './Animation';
2
+ interface InternalAttrs {
3
+ ref?: (ref: any) => void;
4
+ onShowAnimation?: Animation;
5
+ onHideAnimation?: Animation;
6
+ }
7
+ export declare abstract class DomeComponent<Attrs> {
8
+ attrs: Attrs & InternalAttrs;
9
+ children: any;
10
+ rootElement: Element | HTMLElement;
11
+ private updateInProgress;
12
+ private updateRequested;
13
+ constructor(attrs: Attrs & InternalAttrs, children: any);
14
+ protected init(): void;
15
+ protected abstract render(): HTMLElement;
16
+ protected afterRender(): void;
17
+ updateAsync(): Promise<void>;
18
+ private runScheduledUpdateAsync;
19
+ scheduleUpdate: {
20
+ (this: unknown, ...args: [] & any[]): Promise<Promise<void>>;
21
+ cancel: (reason?: any) => void;
22
+ };
23
+ protected afterUpdate(): void;
24
+ }
25
+ export {};
@@ -4,6 +4,8 @@ export class DomeComponent {
4
4
  attrs;
5
5
  children;
6
6
  rootElement;
7
+ updateInProgress = false;
8
+ updateRequested = false;
7
9
  constructor(attrs, children) {
8
10
  //this.init()
9
11
  this.attrs = attrs;
@@ -35,9 +37,28 @@ export class DomeComponent {
35
37
  //console.timeEnd('browser')
36
38
  this.afterUpdate();
37
39
  }
38
- scheduleUpdate = debounce(() => {
39
- this.updateAsync();
40
- }, 5);
40
+ async runScheduledUpdateAsync() {
41
+ if (this.updateInProgress) {
42
+ this.updateRequested = true;
43
+ return;
44
+ }
45
+ this.updateInProgress = true;
46
+ try {
47
+ do {
48
+ this.updateRequested = false;
49
+ try {
50
+ await this.updateAsync();
51
+ }
52
+ catch (error) {
53
+ console.error('DomeComponent: scheduled update failed', error);
54
+ }
55
+ } while (this.updateRequested);
56
+ }
57
+ finally {
58
+ this.updateInProgress = false;
59
+ }
60
+ }
61
+ scheduleUpdate = debounce(() => this.runScheduledUpdateAsync(), 5);
41
62
  afterUpdate() {
42
63
  }
43
64
  }
@@ -16,6 +16,10 @@ export declare namespace DomeManipulator {
16
16
  function removeAllChildrenAsync(element: Element, animation?: Animation): Promise<void>;
17
17
  function appendChildAsync(containerElement: Element, child: Element, animation?: Animation): Promise<void>;
18
18
  function appendChildrenAsync(containerElement: Element, children: Element | Element[] | DocumentFragment | Text | string | null | undefined, animation?: Animation): Promise<void>;
19
+ /**
20
+ * Replacements for the same container run in call order. A rejected replacement does
21
+ * not block the queue, and replacements for different containers remain independent.
22
+ */
19
23
  function replaceAllChildrenAsync(containerElement: Element, childrenToInsert: Element | Element[] | DocumentFragment | Text | string | null | undefined, animationForHide?: Animation, animationForShow?: Animation): Promise<void>;
20
24
  function isInDom(el: Element | undefined): boolean;
21
25
  function isOnScreen(el: Element | undefined): boolean;
@@ -0,0 +1,52 @@
1
+ import { ObservableVariable } from "@lexriver/observable";
2
+ import { Animation } from './Animation';
3
+ export type CssClass = {
4
+ [key: string]: boolean | ObservableVariable<boolean>;
5
+ } | string[] | string;
6
+ export declare namespace DomeManipulator {
7
+ function hideElementAsync(element: Element, animation?: Animation): Promise<void>;
8
+ function unhideElementAsync(element: Element, animation?: Animation): Promise<void>;
9
+ function insertAsFirstChildAsync(elementToInsert: Element, parentElement: Element | DocumentFragment, animation?: Animation): Promise<void>;
10
+ function insertBeforeAsync(elementToInsert: Element, refElement: Element | null, parentElement: Element | DocumentFragment, animation?: Animation): Promise<void>;
11
+ function insertAfterAsync(elementToInsert: Element, refElement: Element | null | undefined, parentElement: Element | DocumentFragment, animation?: Animation): Promise<void>;
12
+ function insertByIndexAsync(elementToInsert: Element, index: number, parentElement: Element | DocumentFragment, animation?: Animation): Promise<void>;
13
+ function replaceAsync(oldElement: Element, newElement: Element, animationHide?: Animation, animationShow?: Animation): Promise<Element>;
14
+ function removeElementAsync(element: Element, animation?: Animation): Promise<void>;
15
+ function forEachChildrenOf(element: Element, action: (child: ChildNode) => void): void;
16
+ function removeAllChildrenAsync(element: Element, animation?: Animation): Promise<void>;
17
+ function appendChildAsync(containerElement: Element, child: Element, animation?: Animation): Promise<void>;
18
+ function appendChildrenAsync(containerElement: Element, children: Element | Element[] | DocumentFragment | Text | string | null | undefined, animation?: Animation): Promise<void>;
19
+ /**
20
+ * Replacements for the same container run in call order. A rejected replacement does
21
+ * not block the queue, and replacements for different containers remain independent.
22
+ */
23
+ function replaceAllChildrenAsync(containerElement: Element, childrenToInsert: Element | Element[] | DocumentFragment | Text | string | null | undefined, animationForHide?: Animation, animationForShow?: Animation): Promise<void>;
24
+ function isInDom(el: Element | undefined): boolean;
25
+ function isOnScreen(el: Element | undefined): boolean;
26
+ function addCssClassAsync(element: Element, cssClassName: string, removeAfterMs?: number): Promise<void>;
27
+ function addCssClassesAsync(element: Element, cssClassNames: string[], removeAfterMs?: number): Promise<void>;
28
+ function removeCssClass(element: Element, cssClassName: string): void;
29
+ function removeCssClasses(element: Element, cssClassNames: string[]): void;
30
+ /**
31
+ * set css classes by array ['class1', 'class2'] or
32
+ * by object {class1:true, class2:false} or
33
+ * by Observable<boolean> {class1:true, class2:myVarO} or
34
+ * by string
35
+ * this function will replace class attribute so all classes that are not in params will be removed
36
+ * @param value
37
+ * @param element
38
+ */
39
+ function setCssClasses(element: Element, value: CssClass): void;
40
+ function setAttribute(element: Element, name: string, value: any): void;
41
+ function hasFocus(el: Element): boolean;
42
+ function scrollIntoView(element: Element, paddingFromTop?: number): void;
43
+ const scrollToTop: () => void;
44
+ function getCurrentScrollPosition(): number;
45
+ function scrollToAsync(p: {
46
+ pxFromTop?: number;
47
+ pxFromLeft?: number;
48
+ smooth?: boolean;
49
+ msStep?: number;
50
+ maxMsToWait?: number;
51
+ }): Promise<void>;
52
+ }
@@ -1,6 +1,7 @@
1
1
  import { Async } from "@lexriver/async";
2
2
  import { DataTypes } from "@lexriver/data-types";
3
3
  import { checkIfObservable } from "@lexriver/observable";
4
+ const replacementPromiseByElement = new WeakMap();
4
5
  export var DomeManipulator;
5
6
  (function (DomeManipulator) {
6
7
  async function hideElementAsync(element, animation) {
@@ -121,8 +122,8 @@ export var DomeManipulator;
121
122
  forEachChildrenOf(element, (child) => child.nodeType == Node.ELEMENT_NODE && child.classList.add(animation.cssClassName));
122
123
  //console.log('##', 'wait ms', animation.timeMs)
123
124
  await Async.waitMsAsync(animation.timeMs);
124
- //console.log('##', 'removing children', element.childNodes)
125
- forEachChildrenOf(element, (child) => child.remove());
125
+ // Take a snapshot because childNodes is live and shrinks as nodes are removed.
126
+ Array.from(element.childNodes).forEach(child => child.remove());
126
127
  //console.log('##', 'done')
127
128
  }
128
129
  // if(element.children.length>0) {
@@ -152,19 +153,29 @@ export var DomeManipulator;
152
153
  }
153
154
  }
154
155
  DomeManipulator.appendChildrenAsync = appendChildrenAsync;
156
+ /**
157
+ * Replacements for the same container run in call order. A rejected replacement does
158
+ * not block the queue, and replacements for different containers remain independent.
159
+ */
155
160
  async function replaceAllChildrenAsync(containerElement, childrenToInsert, animationForHide, animationForShow) {
156
- //await removeAllChildrenAsync(containerElement, animationForHide)
157
- if (containerElement.childNodes.length > 0) {
158
- await removeAllChildrenAsync(containerElement, animationForHide);
161
+ const previousReplacement = replacementPromiseByElement.get(containerElement);
162
+ const replacement = (previousReplacement ?? Promise.resolve())
163
+ .catch(() => undefined)
164
+ .then(async () => {
165
+ if (containerElement.childNodes.length > 0) {
166
+ await removeAllChildrenAsync(containerElement, animationForHide);
167
+ }
168
+ await appendChildrenAsync(containerElement, childrenToInsert, animationForShow);
169
+ });
170
+ replacementPromiseByElement.set(containerElement, replacement);
171
+ try {
172
+ await replacement;
173
+ }
174
+ finally {
175
+ if (replacementPromiseByElement.get(containerElement) === replacement) {
176
+ replacementPromiseByElement.delete(containerElement);
177
+ }
159
178
  }
160
- // while(containerElement.childNodes.length>0){
161
- // await removeAllChildrenAsync(containerElement, animationForHide) // can be executed more than once! TODO: why?
162
- // }
163
- // if(containerElement.children.length>0) {
164
- // console.error('DomeManipulator: replaceAllChildenrAsync() failed:', 'containerElement.children.length=', containerElement.children.length, 'children=', containerElement.children, 'containerElement=', containerElement, 'animationForHide=', animationForHide)
165
- // throw new Error()
166
- // }
167
- await appendChildrenAsync(containerElement, childrenToInsert, animationForShow);
168
179
  }
169
180
  DomeManipulator.replaceAllChildrenAsync = replaceAllChildrenAsync;
170
181
  function isInDom(el) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ export type RouteAction = (params: {
2
+ [key: string]: string | number;
3
+ }, url: string, scrollToPreviousPositionAsync: () => Promise<void>, query: {
4
+ [key: string]: string;
5
+ }) => void | Promise<void>;
6
+ export declare namespace DomeRouter {
7
+ let maxHistoryUrlsCount: number;
8
+ function navigate(url: string): void;
9
+ function goBack(): void;
10
+ function goForward(): void;
11
+ function changeUrl(url: string): void;
12
+ function getCurrentUrl(): string;
13
+ /**
14
+ * get previous page url navigated by router
15
+ * @param previousPageIndex 0=previousPage, 1=previousPage-1, etc
16
+ */
17
+ function getPreviousPageUrl(previousPageIndex?: number): string | undefined;
18
+ function resolveUrl(url?: string, addToHistory?: boolean): void;
19
+ function onRoute(route: string, exactMatch: boolean, action: RouteAction): void;
20
+ function onNotFound(action: () => void): void;
21
+ /**
22
+ * Check if url matched route.
23
+ * example: /get-product/445345, /get-product/:productId, true ==> Map([productId:445345])
24
+ * example: /get-product/123, /another-route, true ==> undefined
25
+ * example: /articles, /articles, true ==> Map()
26
+ * example: /articles/some-article, /articles, false ==> Map()
27
+ * @param url
28
+ * @param route
29
+ * @param exactMatch
30
+ */
31
+ function checkUrlMatchRouteAndGetParameters(url: string, route: string, exactMatch?: boolean): Object | undefined;
32
+ }
@@ -0,0 +1,15 @@
1
+ export declare namespace LongestCommonSubsequence {
2
+ function getLongestCommonSubsequence(set1: string[], set2: string[]): string[];
3
+ function getPatch({ oldArray, newArray, onRemove, onAdd }: {
4
+ oldArray: string[];
5
+ newArray: string[];
6
+ onRemove: (index: number, item: string) => void;
7
+ onAdd: (index: number, item: string) => void;
8
+ }): number;
9
+ function getPatchOrdered({ oldArray, newArray, onRemove, onAdd }: {
10
+ oldArray: string[];
11
+ newArray: string[];
12
+ onRemove: (index: number, item: string) => void;
13
+ onAdd: (index: number, item: string) => void;
14
+ }): number;
15
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,203 @@
1
+ import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
2
+ import { DomeComponent } from './DomeComponent.mjs';
3
+ import { DomeManipulator } from './DomeManipulator.mjs';
4
+ class FakeElement {
5
+ nodeType = 1;
6
+ childNodes = [];
7
+ classList = {
8
+ values: new Set(),
9
+ add: (name) => this.classList.values.add(name),
10
+ remove: (name) => this.classList.values.delete(name)
11
+ };
12
+ parentNode = null;
13
+ throwOnAppend = false;
14
+ get firstChild() {
15
+ return this.childNodes[0] ?? null;
16
+ }
17
+ appendChild(child) {
18
+ if (child.throwOnAppend) {
19
+ throw new Error('append failed');
20
+ }
21
+ child.parentNode = this;
22
+ this.childNodes.push(child);
23
+ return child;
24
+ }
25
+ remove() {
26
+ if (!this.parentNode)
27
+ return;
28
+ const index = this.parentNode.childNodes.indexOf(this);
29
+ if (index >= 0) {
30
+ this.parentNode.childNodes.splice(index, 1);
31
+ }
32
+ this.parentNode = null;
33
+ }
34
+ }
35
+ const asElement = (element) => element;
36
+ const animation = (name) => ({ cssClassName: name, timeMs: 5 });
37
+ const waitAsync = (timeMs) => new Promise(resolve => setTimeout(resolve, timeMs));
38
+ async function waitForAsync(condition) {
39
+ const timeoutAt = Date.now() + 500;
40
+ while (!condition()) {
41
+ if (Date.now() >= timeoutAt) {
42
+ throw new Error('Timed out waiting for condition');
43
+ }
44
+ await waitAsync(1);
45
+ }
46
+ }
47
+ function deferred() {
48
+ let resolve;
49
+ const promise = new Promise(resolvePromise => {
50
+ resolve = resolvePromise;
51
+ });
52
+ return { promise, resolve };
53
+ }
54
+ const originalNode = Object.getOwnPropertyDescriptor(globalThis, 'Node');
55
+ beforeAll(() => {
56
+ Object.defineProperty(globalThis, 'Node', {
57
+ configurable: true,
58
+ value: { ELEMENT_NODE: 1 }
59
+ });
60
+ });
61
+ afterAll(() => {
62
+ if (originalNode) {
63
+ Object.defineProperty(globalThis, 'Node', originalNode);
64
+ }
65
+ else {
66
+ delete globalThis.Node;
67
+ }
68
+ });
69
+ describe('DomeManipulator.replaceAllChildrenAsync', () => {
70
+ async function expectLastQueuedReplacement(animationForHide, animationForShow) {
71
+ const container = new FakeElement();
72
+ const existing = new FakeElement();
73
+ const first = new FakeElement();
74
+ const second = new FakeElement();
75
+ container.appendChild(existing);
76
+ await Promise.all([
77
+ DomeManipulator.replaceAllChildrenAsync(asElement(container), asElement(first), animationForHide, animationForShow),
78
+ DomeManipulator.replaceAllChildrenAsync(asElement(container), asElement(second), animationForHide, animationForShow)
79
+ ]);
80
+ expect(container.childNodes).toEqual([second]);
81
+ }
82
+ it('serializes two concurrent replacements without animations', async () => {
83
+ await expectLastQueuedReplacement();
84
+ });
85
+ it('serializes concurrent replacements with a hide animation', async () => {
86
+ await expectLastQueuedReplacement(animation('hide'));
87
+ });
88
+ it('serializes concurrent replacements with a show animation', async () => {
89
+ await expectLastQueuedReplacement(undefined, animation('show'));
90
+ });
91
+ it('serializes concurrent replacements with hide and show animations', async () => {
92
+ await expectLastQueuedReplacement(animation('hide'), animation('show'));
93
+ });
94
+ it('runs replacements on different containers independently', async () => {
95
+ const slowContainer = new FakeElement();
96
+ const fastContainer = new FakeElement();
97
+ const oldSlowChild = new FakeElement();
98
+ const slowChild = new FakeElement();
99
+ const fastChild = new FakeElement();
100
+ slowContainer.appendChild(oldSlowChild);
101
+ let slowReplacementFinished = false;
102
+ const slowReplacement = DomeManipulator.replaceAllChildrenAsync(asElement(slowContainer), asElement(slowChild), { cssClassName: 'hide', timeMs: 30 }).then(() => {
103
+ slowReplacementFinished = true;
104
+ });
105
+ await waitAsync(1);
106
+ await DomeManipulator.replaceAllChildrenAsync(asElement(fastContainer), asElement(fastChild));
107
+ expect(fastContainer.childNodes).toEqual([fastChild]);
108
+ expect(slowReplacementFinished).toBe(false);
109
+ expect(slowContainer.childNodes).toEqual([oldSlowChild]);
110
+ await slowReplacement;
111
+ });
112
+ it('does not let a failed replacement block a later replacement', async () => {
113
+ const container = new FakeElement();
114
+ const failingChild = new FakeElement();
115
+ const successfulChild = new FakeElement();
116
+ failingChild.throwOnAppend = true;
117
+ const failedReplacement = DomeManipulator.replaceAllChildrenAsync(asElement(container), asElement(failingChild));
118
+ const queuedReplacement = DomeManipulator.replaceAllChildrenAsync(asElement(container), asElement(successfulChild));
119
+ await expect(failedReplacement).rejects.toThrow('append failed');
120
+ await queuedReplacement;
121
+ expect(container.childNodes).toEqual([successfulChild]);
122
+ });
123
+ });
124
+ class ScheduledTestComponent extends DomeComponent {
125
+ updateCount = 0;
126
+ activeUpdates = 0;
127
+ maximumActiveUpdates = 0;
128
+ updateBehavior = async () => undefined;
129
+ render() {
130
+ return {};
131
+ }
132
+ async updateAsync() {
133
+ this.updateCount++;
134
+ this.activeUpdates++;
135
+ this.maximumActiveUpdates = Math.max(this.maximumActiveUpdates, this.activeUpdates);
136
+ try {
137
+ await this.updateBehavior();
138
+ }
139
+ finally {
140
+ this.activeUpdates--;
141
+ }
142
+ }
143
+ }
144
+ describe('DomeComponent.scheduleUpdate', () => {
145
+ it('debounces repeated requests made before an update starts', async () => {
146
+ const component = new ScheduledTestComponent({}, null);
147
+ await Promise.all([
148
+ component.scheduleUpdate(),
149
+ component.scheduleUpdate(),
150
+ component.scheduleUpdate()
151
+ ]);
152
+ expect(component.updateCount).toBe(1);
153
+ });
154
+ it('coalesces requests during an update into exactly one follow-up update', async () => {
155
+ const component = new ScheduledTestComponent({}, null);
156
+ const firstUpdate = deferred();
157
+ component.updateBehavior = () => component.updateCount === 1
158
+ ? firstUpdate.promise
159
+ : Promise.resolve();
160
+ const initialSchedule = component.scheduleUpdate();
161
+ await waitForAsync(() => component.updateCount === 1);
162
+ component.scheduleUpdate();
163
+ component.scheduleUpdate();
164
+ component.scheduleUpdate();
165
+ await waitAsync(10);
166
+ firstUpdate.resolve();
167
+ await initialSchedule;
168
+ expect(component.updateCount).toBe(2);
169
+ });
170
+ it('never runs scheduled updates concurrently for one component', async () => {
171
+ const component = new ScheduledTestComponent({}, null);
172
+ const firstUpdate = deferred();
173
+ component.updateBehavior = () => component.updateCount === 1
174
+ ? firstUpdate.promise
175
+ : Promise.resolve();
176
+ const initialSchedule = component.scheduleUpdate();
177
+ await waitForAsync(() => component.updateCount === 1);
178
+ component.scheduleUpdate();
179
+ await waitAsync(10);
180
+ expect(component.maximumActiveUpdates).toBe(1);
181
+ firstUpdate.resolve();
182
+ await initialSchedule;
183
+ expect(component.maximumActiveUpdates).toBe(1);
184
+ });
185
+ it('allows subsequent scheduled updates after updateAsync rejects', async () => {
186
+ const component = new ScheduledTestComponent({}, null);
187
+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
188
+ component.updateBehavior = async () => {
189
+ if (component.updateCount === 1) {
190
+ throw new Error('update failed');
191
+ }
192
+ };
193
+ try {
194
+ await component.scheduleUpdate();
195
+ await component.scheduleUpdate();
196
+ }
197
+ finally {
198
+ consoleError.mockRestore();
199
+ }
200
+ expect(component.updateCount).toBe(2);
201
+ expect(component.maximumActiveUpdates).toBe(1);
202
+ });
203
+ });
@@ -0,0 +1,11 @@
1
+ export * from '@lexriver/async';
2
+ export * from '@lexriver/data-types';
3
+ export * from '@lexriver/observable';
4
+ export * from '@lexriver/type-event';
5
+ export * from './AnimatedArray';
6
+ export * from './AnimatedTable';
7
+ export * from './AnimatedText';
8
+ export * from './Dome';
9
+ export * from './DomeComponent';
10
+ export * from './DomeManipulator';
11
+ export * from './DomeRouter.console-test';
@@ -0,0 +1 @@
1
+ export {};