ctrl-fx 0.0.1 → 0.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.
Files changed (145) hide show
  1. package/dist/clipboard.d.ts +7 -0
  2. package/dist/clipboard.d.ts.map +1 -0
  3. package/dist/clipboard.js +3 -0
  4. package/dist/db/effects.d.ts +78 -0
  5. package/dist/db/effects.d.ts.map +1 -0
  6. package/dist/db/effects.js +98 -0
  7. package/dist/db/errors.d.ts +21 -0
  8. package/dist/db/errors.d.ts.map +1 -0
  9. package/dist/db/errors.js +13 -0
  10. package/dist/db/index.d.ts +32 -0
  11. package/dist/db/index.d.ts.map +1 -0
  12. package/dist/db/index.js +19 -0
  13. package/dist/db/keypath.d.ts +40 -0
  14. package/dist/db/keypath.d.ts.map +1 -0
  15. package/dist/db/keypath.js +20 -0
  16. package/dist/db/setup.d.ts +83 -0
  17. package/dist/db/setup.d.ts.map +1 -0
  18. package/dist/db/setup.js +100 -0
  19. package/dist/db/testdb.d.ts +19 -0
  20. package/dist/db/testdb.d.ts.map +1 -0
  21. package/dist/db/testdb.js +34 -0
  22. package/dist/dom/attrs.d.ts +67 -0
  23. package/dist/dom/attrs.d.ts.map +1 -0
  24. package/dist/dom/attrs.js +154 -0
  25. package/dist/dom/components.d.ts +43 -0
  26. package/dist/dom/components.d.ts.map +1 -0
  27. package/dist/dom/components.js +86 -0
  28. package/dist/dom/elems.d.ts +121 -0
  29. package/dist/dom/elems.d.ts.map +1 -0
  30. package/dist/dom/elems.js +52 -0
  31. package/dist/dom/events.d.ts +232 -0
  32. package/dist/dom/events.d.ts.map +1 -0
  33. package/dist/dom/events.js +289 -0
  34. package/dist/dom/index.d.ts +248 -0
  35. package/dist/dom/index.d.ts.map +1 -0
  36. package/dist/dom/index.js +266 -0
  37. package/dist/dom/nodeid.d.ts +9 -0
  38. package/dist/dom/nodeid.d.ts.map +1 -0
  39. package/dist/dom/nodeid.js +27 -0
  40. package/dist/dom/svg.d.ts +23 -0
  41. package/dist/dom/svg.d.ts.map +1 -0
  42. package/dist/dom/svg.js +1 -0
  43. package/dist/dom/views.d.ts +60 -0
  44. package/dist/dom/views.d.ts.map +1 -0
  45. package/dist/dom/views.js +154 -0
  46. package/dist/effects.d.ts +580 -0
  47. package/dist/effects.d.ts.map +1 -0
  48. package/dist/effects.js +1556 -0
  49. package/dist/index.d.ts +41 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +78 -0
  52. package/dist/internal/clipboard.d.ts +5 -0
  53. package/dist/internal/clipboard.d.ts.map +1 -0
  54. package/dist/internal/clipboard.js +33 -0
  55. package/dist/internal/cmpmgr.d.ts +46 -0
  56. package/dist/internal/cmpmgr.d.ts.map +1 -0
  57. package/dist/internal/cmpmgr.js +561 -0
  58. package/dist/internal/dbmgr.d.ts +11 -0
  59. package/dist/internal/dbmgr.d.ts.map +1 -0
  60. package/dist/internal/dbmgr.js +200 -0
  61. package/dist/internal/dominterpreter.d.ts +5 -0
  62. package/dist/internal/dominterpreter.d.ts.map +1 -0
  63. package/dist/internal/dominterpreter.js +337 -0
  64. package/dist/internal/eventmgr.d.ts +41 -0
  65. package/dist/internal/eventmgr.d.ts.map +1 -0
  66. package/dist/internal/eventmgr.js +696 -0
  67. package/dist/internal/index.d.ts +19 -0
  68. package/dist/internal/index.d.ts.map +1 -0
  69. package/dist/internal/index.js +38 -0
  70. package/dist/internal/interpreter.d.ts +10 -0
  71. package/dist/internal/interpreter.d.ts.map +1 -0
  72. package/dist/internal/interpreter.js +1 -0
  73. package/dist/internal/locutils.d.ts +4 -0
  74. package/dist/internal/locutils.d.ts.map +1 -0
  75. package/dist/internal/locutils.js +22 -0
  76. package/dist/internal/net.d.ts +4 -0
  77. package/dist/internal/net.d.ts.map +1 -0
  78. package/dist/internal/net.js +64 -0
  79. package/dist/internal/realdom.d.ts +249 -0
  80. package/dist/internal/realdom.d.ts.map +1 -0
  81. package/dist/internal/realdom.js +1 -0
  82. package/dist/internal/styleregistry.d.ts +6 -0
  83. package/dist/internal/styleregistry.d.ts.map +1 -0
  84. package/dist/internal/styleregistry.js +18 -0
  85. package/dist/internal/taskreg.d.ts +9 -0
  86. package/dist/internal/taskreg.d.ts.map +1 -0
  87. package/dist/internal/taskreg.js +28 -0
  88. package/dist/internal/testing.d.ts +12 -0
  89. package/dist/internal/testing.d.ts.map +1 -0
  90. package/dist/internal/testing.js +974 -0
  91. package/dist/internal/testinginterpreter.d.ts +10 -0
  92. package/dist/internal/testinginterpreter.d.ts.map +1 -0
  93. package/dist/internal/testinginterpreter.js +646 -0
  94. package/dist/internal/vdom.d.ts +93 -0
  95. package/dist/internal/vdom.d.ts.map +1 -0
  96. package/dist/internal/vdom.js +470 -0
  97. package/dist/json/index.d.ts +5 -0
  98. package/dist/json/index.d.ts.map +1 -0
  99. package/dist/json/index.js +2 -0
  100. package/dist/json/validation.d.ts +28 -0
  101. package/dist/json/validation.d.ts.map +1 -0
  102. package/dist/json/validation.js +8 -0
  103. package/dist/net/fragment.d.ts +10 -0
  104. package/dist/net/fragment.d.ts.map +1 -0
  105. package/dist/net/fragment.js +27 -0
  106. package/dist/net/index.d.ts +42 -0
  107. package/dist/net/index.d.ts.map +1 -0
  108. package/dist/net/index.js +60 -0
  109. package/dist/net/location.d.ts +21 -0
  110. package/dist/net/location.d.ts.map +1 -0
  111. package/dist/net/location.js +41 -0
  112. package/dist/net/path.d.ts +12 -0
  113. package/dist/net/path.d.ts.map +1 -0
  114. package/dist/net/path.js +29 -0
  115. package/dist/net/queryparam.d.ts +10 -0
  116. package/dist/net/queryparam.d.ts.map +1 -0
  117. package/dist/net/queryparam.js +34 -0
  118. package/dist/router.d.ts +40 -0
  119. package/dist/router.d.ts.map +1 -0
  120. package/dist/router.js +77 -0
  121. package/dist/scroll.d.ts +7 -0
  122. package/dist/scroll.d.ts.map +1 -0
  123. package/dist/scroll.js +3 -0
  124. package/dist/testing.d.ts +248 -0
  125. package/dist/testing.d.ts.map +1 -0
  126. package/dist/testing.js +159 -0
  127. package/dist/utils/apply.d.ts +9 -0
  128. package/dist/utils/apply.d.ts.map +1 -0
  129. package/dist/utils/apply.js +10 -0
  130. package/dist/utils/index.d.ts +22 -0
  131. package/dist/utils/index.d.ts.map +1 -0
  132. package/dist/utils/index.js +138 -0
  133. package/dist/utils/lens.d.ts +18 -0
  134. package/dist/utils/lens.d.ts.map +1 -0
  135. package/dist/utils/lens.js +27 -0
  136. package/dist/utils/match.d.ts +17 -0
  137. package/dist/utils/match.d.ts.map +1 -0
  138. package/dist/utils/match.js +28 -0
  139. package/dist/utils/result.d.ts +47 -0
  140. package/dist/utils/result.d.ts.map +1 -0
  141. package/dist/utils/result.js +97 -0
  142. package/dist/webcomponent.d.ts +15 -0
  143. package/dist/webcomponent.d.ts.map +1 -0
  144. package/dist/webcomponent.js +43 -0
  145. package/package.json +1 -1
@@ -0,0 +1,561 @@
1
+ import { componentPath } from ".";
2
+ import { foldNode } from "../dom";
3
+ import { getId } from "../dom/attrs";
4
+ import {} from "../dom/components";
5
+ import { nodeId } from "../dom/nodeid";
6
+ import { eq, exhaustivenessCheck } from "../utils";
7
+ import { EventManager } from "./eventmgr";
8
+ import { TaskRegistry } from "./taskreg";
9
+ import { closestComponentPath, componentNode, detach, internalElementNode, internalTextNode, rootNode, updateAttrs, updateProps, updateText, viewNode, } from "./vdom";
10
+ let mountCount = 0;
11
+ export function manageComponent(component, mountId, window, interpreter, styleRegistry) {
12
+ const mountNode = window.document.getElementById(mountId);
13
+ if (!mountNode) {
14
+ throw new Error(`Cannot attach component tree. Root node with id ${mountId} not found`);
15
+ }
16
+ const mgr = new ComponentManager(component, mountNode, window, interpreter, styleRegistry);
17
+ return {
18
+ dispatchEffect: (effect) => mgr.dispatchEffect(effect),
19
+ };
20
+ }
21
+ export class ComponentManager {
22
+ constructor(component, mountNode, window, interpreter, styleRegistry, onRootEvent) {
23
+ Object.defineProperty(this, "rootCompPath", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "components", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: {}
34
+ });
35
+ Object.defineProperty(this, "removedComponents", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: {}
40
+ });
41
+ Object.defineProperty(this, "addedComponents", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: []
46
+ });
47
+ Object.defineProperty(this, "interpreter", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "window", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "document", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "eventManager", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "taskRegistry", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: new TaskRegistry()
76
+ });
77
+ Object.defineProperty(this, "styleRegistry", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "onRootEvent", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ this.window = window;
90
+ this.document = this.window.document;
91
+ this.interpreter = interpreter;
92
+ this.styleRegistry = styleRegistry;
93
+ this.onRootEvent = onRootEvent;
94
+ this.eventManager = new EventManager(this.window, (effect, node, onResult) => {
95
+ const componentPath = closestComponentPath(node);
96
+ if (componentPath) {
97
+ this.runEffect(effect, componentPath, onResult);
98
+ }
99
+ });
100
+ const rootCompPath = componentPath(nodeId(`__root_component:${++mountCount}`));
101
+ this.rootCompPath = rootCompPath;
102
+ const compNode = componentNode(component, rootCompPath.nodeId);
103
+ const root = rootNode(mountNode);
104
+ root.append(compNode);
105
+ this.components[componentPath(compNode.nodeId, undefined).format()] =
106
+ compNode;
107
+ this.injectComponentCss(component);
108
+ const initStateEffect = component.initialState(component.params);
109
+ this.runEffect(initStateEffect, rootCompPath, (state) => {
110
+ this.renderComponent(rootCompPath, state);
111
+ });
112
+ }
113
+ injectComponentCss(component) {
114
+ if (!component.css)
115
+ return;
116
+ const css = typeof component.css === "function"
117
+ ? component.css(component.params)
118
+ : component.css;
119
+ this.styleRegistry.injectStyle(css);
120
+ }
121
+ getRootState() {
122
+ return (this.components[this.rootCompPath.format()]?.state ?? {
123
+ _type: "Uninitialized",
124
+ });
125
+ }
126
+ dispatchEffect(effect) {
127
+ this.runEffect(effect, this.rootCompPath, () => { });
128
+ }
129
+ destroy() {
130
+ this.taskRegistry.cancelAll();
131
+ }
132
+ runEffect(effect, componentPath, f) {
133
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
134
+ const that = this;
135
+ this.interpreter(effect, {
136
+ onComplete: f,
137
+ onFireEvent: (event) => {
138
+ const component = this.components[componentPath.format()];
139
+ if (component) {
140
+ const parentCompPath = componentPath.parentComponentPath;
141
+ if (parentCompPath) {
142
+ component.componentEventListeners.forEach((listener) => {
143
+ const effect = listener(event);
144
+ this.runEffect(effect, parentCompPath, () => { });
145
+ });
146
+ }
147
+ else {
148
+ this.onRootEvent?.(event);
149
+ }
150
+ }
151
+ },
152
+ getState: () => {
153
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
+ return that.components[componentPath.format()].state.value;
155
+ },
156
+ setState: (state) => {
157
+ that.renderComponent(componentPath, state);
158
+ },
159
+ }, this.taskRegistry);
160
+ }
161
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
162
+ renderComponent(componentPath, state) {
163
+ const component = this.components[componentPath.format()];
164
+ if (!component) {
165
+ return;
166
+ }
167
+ // if state hasn't changed, nothing to do
168
+ if (component.state._type === "Ready" && eq(component.state.value, state)) {
169
+ return;
170
+ }
171
+ component.state = { _type: "Ready", value: state };
172
+ const newNodeGroup = component.component.view(state, component.component.params);
173
+ component.containerListeners = newNodeGroup.containerListeners;
174
+ this.eventManager.setContainerListeners(component);
175
+ const oldNodes = component.firstChild;
176
+ this.compareNodeSequences(oldNodes, newNodeGroup.nodes, component, componentPath);
177
+ const added = this.addedComponents.splice(0);
178
+ added.forEach((childPath) => {
179
+ const cNode = this.components[childPath.format()];
180
+ if (cNode) {
181
+ const initEffect = cNode.component.initialState(cNode.component.params);
182
+ this.runEffect(initEffect, childPath, (state) => {
183
+ this.renderComponent(childPath, state);
184
+ });
185
+ }
186
+ });
187
+ }
188
+ compareNodeSequences(oldNodes, newNodes, parent, ancestorComponentPath) {
189
+ const mutNewNodes = [...newNodes];
190
+ while (mutNewNodes.length > 0) {
191
+ const newNode = mutNewNodes.shift();
192
+ const correspondingOldNode = this.findCorrespondingOldNode(newNode, oldNodes, ancestorComponentPath);
193
+ if (correspondingOldNode) {
194
+ if (correspondingOldNode !== oldNodes) {
195
+ oldNodes?.before(correspondingOldNode);
196
+ }
197
+ this.compareNodes(correspondingOldNode, newNode, ancestorComponentPath);
198
+ oldNodes = correspondingOldNode.nextSibling;
199
+ }
200
+ else {
201
+ const newInternalNode = this.convertNodeToInternalNode(newNode, ancestorComponentPath);
202
+ if (oldNodes) {
203
+ oldNodes.before(newInternalNode);
204
+ }
205
+ else {
206
+ parent.append(newInternalNode);
207
+ }
208
+ }
209
+ }
210
+ let oldNode = oldNodes;
211
+ while (oldNode) {
212
+ const next = oldNode.nextSibling;
213
+ this.removeNode(oldNode, ancestorComponentPath);
214
+ oldNode = next;
215
+ }
216
+ }
217
+ removeNode(oldNode, ancestorComponentPath) {
218
+ detach(oldNode);
219
+ // TODO: event listeners
220
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
221
+ const that = this;
222
+ function removeNodeRecords(node, ancestorComponentPath) {
223
+ switch (node._type) {
224
+ case "InternalTextNode":
225
+ return;
226
+ case "InternalElementNode": {
227
+ that.eventManager.setElementListeners(node);
228
+ return;
229
+ }
230
+ case "ViewNode": {
231
+ that.eventManager.setContainerListeners(node);
232
+ return;
233
+ }
234
+ case "ComponentNode": {
235
+ that.eventManager.setContainerListeners(node);
236
+ const compPath = componentPath(node.nodeId, ancestorComponentPath).format();
237
+ delete that.components[compPath];
238
+ that.removedComponents[compPath] = node;
239
+ return;
240
+ }
241
+ }
242
+ }
243
+ foldTree(oldNode, ancestorComponentPath, undefined, (node, ancestorComponentId) => {
244
+ removeNodeRecords(node, ancestorComponentId);
245
+ });
246
+ }
247
+ compareNodes(oldNode, newNode, ancestorComponentPath) {
248
+ const parent = oldNode.parent;
249
+ const removeOldAndAddNew = () => {
250
+ const nextSibling = oldNode.nextSibling;
251
+ this.removeNode(oldNode, ancestorComponentPath);
252
+ const newInternalNode = this.convertNodeToInternalNode(newNode, ancestorComponentPath);
253
+ if (nextSibling) {
254
+ nextSibling.after(newInternalNode);
255
+ }
256
+ else if (parent) {
257
+ parent.append(newInternalNode);
258
+ }
259
+ };
260
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
261
+ const that = this;
262
+ foldNode(newNode, {
263
+ onText(text) {
264
+ if (oldNode._type !== "InternalTextNode") {
265
+ removeOldAndAddNew();
266
+ return;
267
+ }
268
+ updateText(oldNode, text);
269
+ },
270
+ onVoidElement(elem) {
271
+ if (oldNode._type !== "InternalElementNode" ||
272
+ oldNode.tag !== elem.tag) {
273
+ removeOldAndAddNew();
274
+ return;
275
+ }
276
+ updateAttrs(oldNode, elem.attrs);
277
+ updateProps(oldNode, elem.props);
278
+ if (oldNode.eventListeners.length > 0 ||
279
+ elem.eventListeners.length > 0) {
280
+ oldNode.eventListeners = elem.eventListeners;
281
+ that.eventManager.setElementListeners(oldNode);
282
+ }
283
+ },
284
+ onNonVoidElement(elem) {
285
+ if (oldNode._type !== "InternalElementNode" ||
286
+ oldNode.tag !== elem.tag) {
287
+ removeOldAndAddNew();
288
+ return;
289
+ }
290
+ updateAttrs(oldNode, elem.attrs);
291
+ updateProps(oldNode, elem.props);
292
+ if (oldNode.eventListeners.length > 0) {
293
+ oldNode.eventListeners = elem.eventListeners;
294
+ that.eventManager.setElementListeners(oldNode);
295
+ }
296
+ that.compareNodeSequences(oldNode.firstChild, elem.children, oldNode, ancestorComponentPath);
297
+ },
298
+ onView(view) {
299
+ if (oldNode._type !== "ViewNode" || !oldNode.nodeId.eq(view.nodeId)) {
300
+ removeOldAndAddNew();
301
+ return;
302
+ }
303
+ if (eq(oldNode.params, view.params)) {
304
+ return;
305
+ }
306
+ const newNodeGroup = view.nodes(view.params);
307
+ oldNode.params = view.params;
308
+ oldNode.containerListeners = newNodeGroup.containerListeners;
309
+ that.eventManager.setContainerListeners(oldNode);
310
+ that.compareNodeSequences(oldNode.firstChild, newNodeGroup.nodes, oldNode, ancestorComponentPath);
311
+ },
312
+ onComponent(comp) {
313
+ if (oldNode._type !== "ComponentNode" ||
314
+ !oldNode.nodeId.eq(comp.nodeId)) {
315
+ removeOldAndAddNew();
316
+ return;
317
+ }
318
+ if (oldNode.state._type === "Ready") {
319
+ if (oldNode.componentEventListeners.length > 0 ||
320
+ comp.componentEventListeners.length > 0) {
321
+ oldNode.componentEventListeners = comp.componentEventListeners;
322
+ that.eventManager.setContainerListeners(oldNode);
323
+ }
324
+ if (eq(oldNode.component.params, comp.component.params)) {
325
+ oldNode.component = comp.component;
326
+ }
327
+ else {
328
+ that.injectComponentCss(comp.component);
329
+ const newNodeGroup = comp.component.view(oldNode.state.value, comp.component.params);
330
+ oldNode.containerListeners = newNodeGroup.containerListeners;
331
+ that.eventManager.setContainerListeners(oldNode);
332
+ oldNode.component = comp.component;
333
+ that.compareNodeSequences(oldNode.firstChild, newNodeGroup.nodes, oldNode, componentPath(comp.nodeId, ancestorComponentPath));
334
+ }
335
+ }
336
+ else {
337
+ oldNode.component = comp.component;
338
+ }
339
+ },
340
+ });
341
+ }
342
+ convertNodeToInternalNode(node, ancestorComponentId) {
343
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
344
+ const that = this;
345
+ return foldNode(node, {
346
+ onText(text) {
347
+ return internalTextNode(that.document, text);
348
+ },
349
+ onVoidElement(elem) {
350
+ const internalElem = internalElementNode(that.document, elem.tag, elem.nodeId, elem.attrs, elem.eventListeners, elem.props);
351
+ if (elem.eventListeners.length > 0) {
352
+ that.eventManager.setElementListeners(internalElem);
353
+ }
354
+ return internalElem;
355
+ },
356
+ onNonVoidElement(elem) {
357
+ const internalElement = internalElementNode(that.document, elem.tag, elem.nodeId, elem.attrs, elem.eventListeners, elem.props);
358
+ if (elem.eventListeners.length > 0) {
359
+ that.eventManager.setElementListeners(internalElement);
360
+ }
361
+ elem.children.forEach((childNode) => {
362
+ internalElement.append(that.convertNodeToInternalNode(childNode, ancestorComponentId));
363
+ });
364
+ return internalElement;
365
+ },
366
+ onView(view) {
367
+ const vNode = viewNode(view);
368
+ const nodeGroup = view.nodes(view.params);
369
+ vNode.params = view.params;
370
+ vNode.containerListeners = nodeGroup.containerListeners;
371
+ nodeGroup.nodes.forEach((childNode) => {
372
+ vNode.append(that.convertNodeToInternalNode(childNode, ancestorComponentId));
373
+ });
374
+ that.eventManager.setContainerListeners(vNode);
375
+ return vNode;
376
+ },
377
+ onComponent(comp) {
378
+ const compId = componentPath(comp.nodeId, ancestorComponentId).format();
379
+ const existingComp = that.components[compId];
380
+ if (existingComp) {
381
+ detach(existingComp);
382
+ that.compareNodes(existingComp, comp, ancestorComponentId);
383
+ return existingComp;
384
+ }
385
+ const cNode = componentNode(comp.component, comp.nodeId);
386
+ if (comp.componentEventListeners.length > 0) {
387
+ cNode.componentEventListeners = comp.componentEventListeners;
388
+ that.eventManager.setContainerListeners(cNode);
389
+ }
390
+ that.injectComponentCss(comp.component);
391
+ that.components[compId] = cNode;
392
+ that.addedComponents.push(componentPath(comp.nodeId, ancestorComponentId));
393
+ return cNode;
394
+ },
395
+ });
396
+ }
397
+ // private compareNodes(
398
+ // oldNode: InternalNode,
399
+ // newNode: Node<unknown, unknown>,
400
+ // parent: RealElement,
401
+ // prevSibling: RealNode | null,
402
+ // ancestorComponentId: ComponentId
403
+ // ): RealNode | null {
404
+ // if (typeof (newNode) === 'string') {
405
+ // if (oldNode._type === 'InternalTextNode') {
406
+ // if (oldNode.text !== newNode) {
407
+ // oldNode.realNode.nodeValue = newNode
408
+ // }
409
+ // return oldNode.realNode
410
+ // }
411
+ // this.removeNode(oldNode)
412
+ // return this.addNode1(newNode, parent, prevSibling, ancestorComponentId)
413
+ // }
414
+ // switch (newNode._type) {
415
+ // case 'Element': {
416
+ // if (
417
+ // oldNode._type === 'InternalElementNode' &&
418
+ // newNode.tag === oldNode.tag
419
+ // ) {
420
+ // this.compareElements(oldNode, newNode)
421
+ // return oldNode.realNode
422
+ // } else {
423
+ // this.removeNode(oldNode)
424
+ // return this.addNode1(newNode, parent, prevSibling, ancestorComponentId)
425
+ // }
426
+ // }
427
+ // case 'Component': {
428
+ // return todo()
429
+ // }
430
+ // case 'View': {
431
+ // return todo()
432
+ // }
433
+ // }
434
+ // }
435
+ /**
436
+ * Return an old node that can be compared to `newNode`.
437
+ *
438
+ * If the old node represent the same node as `newNode`, return it.
439
+ *
440
+ * If the same node isn't found, return the first node if it is `comparable`,
441
+ * otherwise return undefined. (We don't move nodes if they are comparable but
442
+ * not the same).
443
+ */
444
+ findCorrespondingOldNode(newNode, oldNodes, ancestorComponentPath) {
445
+ if (typeof newNode !== "string" && newNode._type === "Component") {
446
+ const registeredComponent = this.components[componentPath(newNode.nodeId, ancestorComponentPath).format()];
447
+ if (registeredComponent) {
448
+ return registeredComponent;
449
+ }
450
+ }
451
+ let next = oldNodes;
452
+ while (next) {
453
+ if (getNodesRelationship(next, newNode) === "same") {
454
+ return next;
455
+ }
456
+ next = next.nextSibling;
457
+ }
458
+ if (oldNodes) {
459
+ if (getNodesRelationship(oldNodes, newNode) === "comparable") {
460
+ return oldNodes;
461
+ }
462
+ }
463
+ return undefined;
464
+ }
465
+ }
466
+ // private removeNode(_node: InternalNode): void {
467
+ // }
468
+ // private compareElements(
469
+ // _oldNode: InternalElementNode,
470
+ // _newNode: Element<unknown, unknown>
471
+ // ): void { }
472
+ // }
473
+ function getNodesRelationship(oldNode, newNode) {
474
+ if (typeof newNode === "string") {
475
+ return oldNode._type === "InternalTextNode" ? "comparable" : "unrelated";
476
+ }
477
+ if (oldNode._type === "InternalTextNode") {
478
+ return "unrelated";
479
+ }
480
+ switch (newNode._type) {
481
+ case "Element": {
482
+ if (oldNode._type === "InternalElementNode") {
483
+ if (newNode.nodeId) {
484
+ if (oldNode.nodeId) {
485
+ return newNode.nodeId.eq(oldNode.nodeId) ? "same" : "unrelated";
486
+ }
487
+ }
488
+ else if (oldNode.nodeId) {
489
+ return "unrelated";
490
+ }
491
+ const oldId = getId(oldNode.attrs);
492
+ const newId = getId(newNode.attrs);
493
+ if (newId) {
494
+ if (oldId) {
495
+ return oldId === newId ? "same" : "unrelated";
496
+ }
497
+ else {
498
+ return "unrelated";
499
+ }
500
+ }
501
+ else if (oldId) {
502
+ return "unrelated";
503
+ }
504
+ return oldNode.tag === newNode.tag ? "comparable" : "unrelated";
505
+ }
506
+ else {
507
+ return "unrelated";
508
+ }
509
+ }
510
+ case "View":
511
+ if (oldNode._type === "ViewNode") {
512
+ return newNode.nodeId.eq(oldNode.nodeId) ? "same" : "unrelated";
513
+ }
514
+ else {
515
+ return "unrelated";
516
+ }
517
+ case "Component":
518
+ if (oldNode._type === "ComponentNode") {
519
+ return newNode.nodeId.eq(oldNode.nodeId) ? "same" : "unrelated";
520
+ }
521
+ else {
522
+ return "unrelated";
523
+ }
524
+ }
525
+ }
526
+ export function foldTree(root, ancestorComponentPath, init, f) {
527
+ let acc = init;
528
+ function visit(n, ancestorComponentPath, moveThroushSiblings) {
529
+ acc = f(n, ancestorComponentPath, acc);
530
+ switch (n._type) {
531
+ case "InternalTextNode": {
532
+ break;
533
+ }
534
+ case "InternalElementNode":
535
+ case "ViewNode": {
536
+ if (n.firstChild) {
537
+ visit(n.firstChild, ancestorComponentPath, true);
538
+ }
539
+ break;
540
+ }
541
+ case "ComponentNode": {
542
+ if (n.firstChild) {
543
+ visit(n.firstChild, componentPath(n.nodeId, ancestorComponentPath), true);
544
+ }
545
+ break;
546
+ }
547
+ default: {
548
+ exhaustivenessCheck(n);
549
+ }
550
+ }
551
+ if (moveThroushSiblings) {
552
+ let next = n.nextSibling;
553
+ while (next) {
554
+ visit(next, ancestorComponentPath, false);
555
+ next = next.nextSibling;
556
+ }
557
+ }
558
+ }
559
+ visit(root, ancestorComponentPath, false);
560
+ return acc;
561
+ }
@@ -0,0 +1,11 @@
1
+ import { type DbName, type DbVersion, type ObjectStore } from '../db';
2
+ import type { DbEffect } from '../db/effects';
3
+ import { type DbError } from '../db/errors';
4
+ import type { DbSetupEffect } from '../db/setup';
5
+ import { type Result } from '../utils/result';
6
+ export declare class DbManager {
7
+ private openDatabases;
8
+ open(db: DbName, version: DbVersion, setup: (oldVersion: DbVersion) => DbSetupEffect<void>, callback: (result: Result<void, DbError>) => void): void;
9
+ runTransaction(db: DbName, objectStores: readonly ObjectStore[], mode: 'readonly' | 'readwrite', effect: DbEffect<void>, callback: (result: Result<void, DbError>) => void): void;
10
+ }
11
+ //# sourceMappingURL=dbmgr.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dbmgr.d.ts","sourceRoot":"","sources":["../../src/internal/dbmgr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,MAAM,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,OAAO,CAAA;AAChF,OAAO,KAAK,EAAE,QAAQ,EAAe,MAAM,eAAe,CAAA;AAC1D,OAAO,EAIL,KAAK,OAAO,EACb,MAAM,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,aAAa,EAAoB,MAAM,aAAa,CAAA;AAGlE,OAAO,EAAoB,KAAK,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE/D,qBAAa,SAAS;IACpB,OAAO,CAAC,aAAa,CAAiC;IAEtD,IAAI,CACF,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,SAAS,EAClB,KAAK,EAAE,CAAC,UAAU,EAAE,SAAS,KAAK,aAAa,CAAC,IAAI,CAAC,EACrD,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,IAAI,GAChD,IAAI;IA4DP,cAAc,CACZ,EAAE,EAAE,MAAM,EACV,YAAY,EAAE,SAAS,WAAW,EAAE,EACpC,IAAI,EAAE,UAAU,GAAG,WAAW,EAC9B,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,EACtB,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,IAAI,GAChD,IAAI;CAoCR"}