veles 0.0.7 → 0.0.8

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,928 @@
1
+ import {
2
+ Fragment,
3
+ createElement,
4
+ createTextElement,
5
+ executeComponent,
6
+ onMount,
7
+ onUnmount
8
+ } from "./chunk-4QXZSBVZ.js";
9
+
10
+ // src/context/index.ts
11
+ var publicContextStack = [];
12
+ var contextIdCounter = 1;
13
+ function createContext() {
14
+ const contextId = contextIdCounter++;
15
+ function addContext(value) {
16
+ const currentContextObject = publicContextStack[publicContextStack.length - 1];
17
+ if (!currentContextObject) {
18
+ console.error("cannot add Context due to missing stack value");
19
+ } else {
20
+ publicContextStack[publicContextStack.length - 1] = {
21
+ ...currentContextObject,
22
+ [contextId]: value
23
+ };
24
+ }
25
+ }
26
+ return {
27
+ Provider: ({ value, children }) => {
28
+ addContext(value);
29
+ return createElement(Fragment, { children });
30
+ },
31
+ addContext,
32
+ readContext: () => {
33
+ const currentContext = publicContextStack[publicContextStack.length - 1];
34
+ if (!currentContext) {
35
+ console.error("no Context currently available");
36
+ } else {
37
+ return currentContext[contextId];
38
+ }
39
+ }
40
+ };
41
+ }
42
+ function addPublicContext(specificContext) {
43
+ if (specificContext) {
44
+ publicContextStack.push(specificContext);
45
+ } else {
46
+ if (publicContextStack.length === 0) {
47
+ publicContextStack.push({});
48
+ } else {
49
+ const currentContext = publicContextStack[publicContextStack.length - 1];
50
+ publicContextStack.push(currentContext);
51
+ }
52
+ }
53
+ }
54
+ function popPublicContext() {
55
+ publicContextStack.pop();
56
+ }
57
+ function getCurrentContext() {
58
+ return publicContextStack[publicContextStack.length - 1];
59
+ }
60
+
61
+ // src/_utils.ts
62
+ function getExecutedComponentVelesNode(component) {
63
+ if ("executedVelesStringElement" in component) {
64
+ return component;
65
+ }
66
+ let childNode = component;
67
+ while ("executedVelesComponent" in childNode) {
68
+ if ("executedVelesStringElement" in childNode.tree) {
69
+ return childNode.tree;
70
+ } else {
71
+ childNode = childNode.tree;
72
+ }
73
+ }
74
+ return childNode;
75
+ }
76
+ function renderTree(component, { parentVelesElement } = {}) {
77
+ if ("velesStringElement" in component) {
78
+ const executedString = {
79
+ executedVelesStringElement: true,
80
+ _privateMethods: component._privateMethods,
81
+ html: component.html,
82
+ parentVelesElement
83
+ };
84
+ if (component.needExecutedVersion) {
85
+ component.executedVersion = executedString;
86
+ }
87
+ return executedString;
88
+ } else if ("velesComponentObject" in component) {
89
+ addPublicContext();
90
+ const componentTree = executeComponent(component);
91
+ const executedComponent = {};
92
+ executedComponent.executedVelesComponent = true;
93
+ executedComponent.tree = renderTree(componentTree.tree);
94
+ popPublicContext();
95
+ executedComponent._privateMethods = {
96
+ ...componentTree._privateMethods,
97
+ _callMountHandlers: () => {
98
+ component._privateMethods._callMountHandlers();
99
+ componentTree._privateMethods._callMountHandlers();
100
+ },
101
+ _callUnmountHandlers: () => {
102
+ component._privateMethods._callUnmountHandlers();
103
+ componentTree._privateMethods._callUnmountHandlers();
104
+ }
105
+ };
106
+ const newNode = getExecutedComponentVelesNode(executedComponent);
107
+ if (parentVelesElement) {
108
+ if (component.insertAfter) {
109
+ if ("velesComponentObject" in component.insertAfter) {
110
+ const lastNode = insertNode({
111
+ velesElement: newNode,
112
+ adjacentNode: component.insertAfter.html,
113
+ parentVelesElement
114
+ });
115
+ component.html = lastNode;
116
+ } else {
117
+ const lastNode = insertNode({
118
+ velesElement: newNode,
119
+ adjacentNode: component.insertAfter,
120
+ parentVelesElement
121
+ });
122
+ component.html = lastNode;
123
+ }
124
+ } else {
125
+ const lastNode = insertNode({
126
+ velesElement: newNode,
127
+ // it means we are inserting the first element
128
+ adjacentNode: null,
129
+ parentVelesElement
130
+ });
131
+ component.html = lastNode;
132
+ }
133
+ newNode.parentVelesElement = parentVelesElement;
134
+ }
135
+ if (component.needExecutedVersion) {
136
+ component.executedVersion = executedComponent;
137
+ }
138
+ return executedComponent;
139
+ } else if ("velesNode" in component) {
140
+ const executedNode = {};
141
+ executedNode.executedVelesNode = true;
142
+ executedNode._privateMethods = component._privateMethods;
143
+ executedNode.html = component.html;
144
+ if (parentVelesElement) {
145
+ executedNode.parentVelesElement = parentVelesElement;
146
+ }
147
+ if (component.phantom) {
148
+ executedNode.phantom = component.phantom;
149
+ }
150
+ executedNode.childComponents = component.childComponents.map(
151
+ (childComponent) => renderTree(childComponent, { parentVelesElement: executedNode })
152
+ );
153
+ if (component.needExecutedVersion) {
154
+ component.executedVersion = executedNode;
155
+ }
156
+ return executedNode;
157
+ }
158
+ }
159
+ function insertNode({
160
+ velesElement,
161
+ adjacentNode,
162
+ parentVelesElement
163
+ }) {
164
+ if (velesElement.phantom) {
165
+ let lastInsertedNode = null;
166
+ velesElement.childComponents.forEach(
167
+ (childComponentofPhantom) => {
168
+ if ("executedVelesNode" in childComponentofPhantom) {
169
+ if (lastInsertedNode) {
170
+ lastInsertedNode.after(childComponentofPhantom.html);
171
+ } else {
172
+ if (adjacentNode) {
173
+ adjacentNode.after(childComponentofPhantom.html);
174
+ } else {
175
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
176
+ }
177
+ }
178
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
179
+ lastInsertedNode = childComponentofPhantom.html;
180
+ } else if ("executedVelesStringElement" in childComponentofPhantom) {
181
+ if (lastInsertedNode) {
182
+ lastInsertedNode.after(childComponentofPhantom.html);
183
+ } else {
184
+ if (adjacentNode) {
185
+ adjacentNode.after(childComponentofPhantom.html);
186
+ } else {
187
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
188
+ }
189
+ }
190
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
191
+ lastInsertedNode = childComponentofPhantom.html;
192
+ } else {
193
+ const executedNode = getExecutedComponentVelesNode(
194
+ childComponentofPhantom
195
+ );
196
+ if (lastInsertedNode) {
197
+ lastInsertedNode.after(executedNode.html);
198
+ } else {
199
+ if (adjacentNode) {
200
+ adjacentNode.after(executedNode.html);
201
+ } else {
202
+ parentVelesElement.html.prepend(executedNode.html);
203
+ }
204
+ }
205
+ executedNode.parentVelesElement = parentVelesElement;
206
+ lastInsertedNode = executedNode.html;
207
+ }
208
+ }
209
+ );
210
+ velesElement.parentVelesElement = parentVelesElement;
211
+ return lastInsertedNode;
212
+ } else {
213
+ if (adjacentNode) {
214
+ adjacentNode.after(velesElement.html);
215
+ } else {
216
+ parentVelesElement.html.prepend(velesElement.html);
217
+ }
218
+ velesElement.parentVelesElement = parentVelesElement;
219
+ return velesElement.html;
220
+ }
221
+ }
222
+ function callMountHandlers(component) {
223
+ component._privateMethods._callMountHandlers();
224
+ if ("executedVelesStringElement" in component) {
225
+ return;
226
+ }
227
+ if ("executedVelesComponent" in component) {
228
+ callMountHandlers(component.tree);
229
+ }
230
+ if ("executedVelesNode" in component) {
231
+ component.childComponents.forEach(
232
+ (childComponent) => callMountHandlers(childComponent)
233
+ );
234
+ }
235
+ }
236
+ function callUnmountHandlers(component) {
237
+ if ("executedVelesStringElement" in component) {
238
+ } else if ("executedVelesComponent" in component) {
239
+ callUnmountHandlers(component.tree);
240
+ } else if ("executedVelesNode" in component) {
241
+ component.childComponents.forEach(
242
+ (childComponent) => callUnmountHandlers(childComponent)
243
+ );
244
+ }
245
+ component._privateMethods._callUnmountHandlers();
246
+ }
247
+ function identity(value1, value2) {
248
+ return value1 === value2;
249
+ }
250
+ function unique(arr) {
251
+ const map = /* @__PURE__ */ new Map();
252
+ const resultArr = [];
253
+ arr.forEach((element) => {
254
+ if (map.has(element))
255
+ return;
256
+ map.set(element, true);
257
+ resultArr.push(element);
258
+ });
259
+ return resultArr;
260
+ }
261
+
262
+ // src/create-state/update-usevalue-selector-value.ts
263
+ function updateUseValueSelector({
264
+ value,
265
+ selectorTrackingElement,
266
+ newTrackingSelectorElements,
267
+ trackers,
268
+ getValue
269
+ }) {
270
+ const { selectedValue, selector, cb, node, comparator, savedContext } = selectorTrackingElement;
271
+ const newSelectedValue = selector ? selector(value) : value;
272
+ if (comparator(selectedValue, newSelectedValue)) {
273
+ newTrackingSelectorElements.push(selectorTrackingElement);
274
+ return;
275
+ }
276
+ addPublicContext(savedContext);
277
+ const returnednewNode = cb ? cb(newSelectedValue) : newSelectedValue == void 0 ? "" : String(newSelectedValue);
278
+ const newNode = !returnednewNode || typeof returnednewNode === "string" ? createTextElement(returnednewNode) : returnednewNode;
279
+ const newRenderedNode = renderTree(newNode);
280
+ popPublicContext();
281
+ newNode.executedVersion = newRenderedNode;
282
+ if (!node.executedVersion) {
283
+ console.error("the node was not mounted");
284
+ return;
285
+ }
286
+ const oldVelesElementNode = getExecutedComponentVelesNode(
287
+ node.executedVersion
288
+ );
289
+ const newVelesElementNode = getExecutedComponentVelesNode(newRenderedNode);
290
+ const parentVelesElement = node.parentVelesElement;
291
+ const parentVelesElementRendered = oldVelesElementNode.parentVelesElement;
292
+ const newTrackingSelectorElement = {
293
+ selector,
294
+ selectedValue: newSelectedValue,
295
+ cb,
296
+ node: newNode,
297
+ comparator,
298
+ savedContext
299
+ };
300
+ if (parentVelesElementRendered) {
301
+ newNode.parentVelesElement = parentVelesElement;
302
+ newVelesElementNode.parentVelesElement = parentVelesElementRendered;
303
+ if ("executedVelesNode" in newVelesElementNode && newVelesElementNode.phantom) {
304
+ const insertAllPhantomChildren = (adjacentNode) => {
305
+ newVelesElementNode.childComponents.forEach(
306
+ (childComponentofPhantom) => {
307
+ if ("executedVelesNode" in childComponentofPhantom) {
308
+ adjacentNode.html.before(childComponentofPhantom.html);
309
+ childComponentofPhantom.parentVelesElement = adjacentNode.parentVelesElement;
310
+ } else {
311
+ const velesElementNode = getExecutedComponentVelesNode(
312
+ childComponentofPhantom
313
+ );
314
+ if (!velesElementNode) {
315
+ console.error("can't find HTML tree in a component chain");
316
+ } else {
317
+ adjacentNode.html.before(velesElementNode.html);
318
+ velesElementNode.parentVelesElement = adjacentNode.parentVelesElement;
319
+ }
320
+ }
321
+ }
322
+ );
323
+ };
324
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
325
+ let isInserted = false;
326
+ oldVelesElementNode.childComponents.forEach(
327
+ (childComponentofPhantom) => {
328
+ if ("executedVelesNode" in childComponentofPhantom) {
329
+ if (!isInserted) {
330
+ insertAllPhantomChildren(childComponentofPhantom);
331
+ isInserted = true;
332
+ }
333
+ childComponentofPhantom.html.remove();
334
+ } else {
335
+ const velesElementNode = getExecutedComponentVelesNode(
336
+ childComponentofPhantom
337
+ );
338
+ if (!velesElementNode) {
339
+ console.error("can't find HTML tree in a component chain");
340
+ } else {
341
+ if (!isInserted) {
342
+ insertAllPhantomChildren(velesElementNode);
343
+ isInserted = true;
344
+ }
345
+ velesElementNode.html.remove();
346
+ }
347
+ }
348
+ }
349
+ );
350
+ } else {
351
+ insertAllPhantomChildren(oldVelesElementNode);
352
+ oldVelesElementNode.html.remove();
353
+ }
354
+ } else {
355
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
356
+ let isInserted = false;
357
+ oldVelesElementNode.childComponents.forEach(
358
+ (childComponentofPhantom) => {
359
+ if ("executedVelesNode" in childComponentofPhantom) {
360
+ if (!isInserted) {
361
+ childComponentofPhantom.html.before(newVelesElementNode.html);
362
+ isInserted = true;
363
+ }
364
+ childComponentofPhantom.html.remove();
365
+ } else {
366
+ const velesElementNode = getExecutedComponentVelesNode(
367
+ childComponentofPhantom
368
+ );
369
+ if (!velesElementNode) {
370
+ console.error("can't find HTML tree in a component chain");
371
+ } else {
372
+ if (!isInserted) {
373
+ velesElementNode.html.before(newVelesElementNode.html);
374
+ isInserted = true;
375
+ }
376
+ velesElementNode.html.remove();
377
+ }
378
+ }
379
+ }
380
+ );
381
+ } else {
382
+ try {
383
+ parentVelesElementRendered.html.replaceChild(
384
+ newVelesElementNode.html,
385
+ oldVelesElementNode.html
386
+ );
387
+ } catch (e) {
388
+ console.error("failed to update...");
389
+ console.log(document.body.innerHTML);
390
+ console.log(oldVelesElementNode.parentVelesElement.html.innerHTML);
391
+ console.log(
392
+ //@ts-expect-error
393
+ oldVelesElementNode.parentVelesElement.childComponents[0].html.textContent
394
+ );
395
+ }
396
+ }
397
+ }
398
+ parentVelesElementRendered.childComponents = parentVelesElementRendered.childComponents.map(
399
+ (childComponent) => childComponent === node.executedVersion ? newRenderedNode : childComponent
400
+ );
401
+ if (parentVelesElement == null ? void 0 : parentVelesElement.childComponents) {
402
+ parentVelesElement.childComponents = parentVelesElement.childComponents.map(
403
+ (childComponent) => childComponent === node ? newNode : childComponent
404
+ );
405
+ }
406
+ callUnmountHandlers(node.executedVersion);
407
+ addUseValueMountHandler({
408
+ usedValue: value,
409
+ trackers,
410
+ getValue,
411
+ trackingSelectorElement: newTrackingSelectorElement
412
+ });
413
+ callMountHandlers(newRenderedNode);
414
+ newNode._privateMethods._addUnmountHandler(() => {
415
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
416
+ (el) => el !== newTrackingSelectorElement
417
+ );
418
+ });
419
+ } else {
420
+ console.log("parent node was not found");
421
+ }
422
+ newTrackingSelectorElements.push(newTrackingSelectorElement);
423
+ }
424
+ function addUseValueMountHandler({
425
+ usedValue,
426
+ getValue,
427
+ trackers,
428
+ trackingSelectorElement
429
+ }) {
430
+ trackingSelectorElement.node._privateMethods._addMountHandler(() => {
431
+ const currentValue = getValue();
432
+ if (usedValue === currentValue) {
433
+ trackers.trackingSelectorElements.push(trackingSelectorElement);
434
+ trackingSelectorElement.node._privateMethods._addUnmountHandler(() => {
435
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
436
+ (el) => trackingSelectorElement !== el
437
+ );
438
+ });
439
+ } else {
440
+ const newTrackingSelectorElements = [];
441
+ updateUseValueSelector({
442
+ value: getValue(),
443
+ trackers,
444
+ selectorTrackingElement: trackingSelectorElement,
445
+ newTrackingSelectorElements,
446
+ getValue
447
+ });
448
+ if (newTrackingSelectorElements[0]) {
449
+ const newTrackingSelectorElement = newTrackingSelectorElements[0];
450
+ if (newTrackingSelectorElement.node === trackingSelectorElement.node) {
451
+ trackers.trackingSelectorElements.push(newTrackingSelectorElement);
452
+ newTrackingSelectorElement.node._privateMethods._addUnmountHandler(
453
+ () => {
454
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
455
+ (el) => trackingSelectorElement !== el
456
+ );
457
+ }
458
+ );
459
+ } else {
460
+ }
461
+ } else {
462
+ }
463
+ }
464
+ });
465
+ }
466
+
467
+ // src/create-state/update-useattribute-value.ts
468
+ function updateUseAttributeValue({
469
+ element,
470
+ value
471
+ }) {
472
+ const { cb, htmlElement, attributeName, attributeValue } = element;
473
+ const newAttributeValue = cb ? cb(value) : value;
474
+ if (typeof newAttributeValue === "boolean") {
475
+ if (newAttributeValue) {
476
+ htmlElement.setAttribute(attributeName, "");
477
+ } else {
478
+ htmlElement.removeAttribute(attributeName);
479
+ }
480
+ } else if (attributeName.startsWith("on")) {
481
+ if (attributeValue === newAttributeValue) {
482
+ return;
483
+ }
484
+ const eventName = attributeName[2].toLocaleLowerCase() + attributeName.slice(3);
485
+ if (attributeValue) {
486
+ htmlElement.removeEventListener(eventName, attributeValue);
487
+ }
488
+ if (newAttributeValue && typeof newAttributeValue === "function") {
489
+ htmlElement.addEventListener(eventName, newAttributeValue);
490
+ }
491
+ element.attributeValue = newAttributeValue;
492
+ } else {
493
+ htmlElement.setAttribute(attributeName, newAttributeValue);
494
+ }
495
+ }
496
+
497
+ // src/create-state/update-usevalueiterator-value.ts
498
+ function updateUseValueIteratorValue({
499
+ value,
500
+ trackingIterator,
501
+ createState: createState2
502
+ }) {
503
+ const {
504
+ cb,
505
+ key,
506
+ renderedElements,
507
+ elementsByKey,
508
+ wrapperComponent,
509
+ selector,
510
+ savedContext
511
+ } = trackingIterator;
512
+ if (!wrapperComponent) {
513
+ console.error("there is no wrapper component for the iterator");
514
+ return;
515
+ }
516
+ if (!wrapperComponent.executedVersion) {
517
+ console.error("it seems the wrapper component was not mounted");
518
+ return;
519
+ }
520
+ const wrapperVelesElementNode = getExecutedComponentVelesNode(
521
+ wrapperComponent.executedVersion
522
+ );
523
+ const parentVelesElement = wrapperVelesElementNode.parentVelesElement;
524
+ if (!parentVelesElement) {
525
+ console.error("there is no parent Veles node for the iterator wrapper");
526
+ return;
527
+ }
528
+ const elements = selector ? selector(value) : value;
529
+ if (Array.isArray(elements)) {
530
+ const newRenderedElements = [];
531
+ const newElementsByKey = {};
532
+ const renderedExistingElements = {};
533
+ elements.forEach((element, index) => {
534
+ let calculatedKey = "";
535
+ if (typeof key === "string" && typeof element === "object" && element !== null && key in element) {
536
+ calculatedKey = element[key];
537
+ } else if (typeof key === "function") {
538
+ calculatedKey = key({ element, index });
539
+ } else {
540
+ }
541
+ if (!calculatedKey) {
542
+ return;
543
+ }
544
+ const existingElement = elementsByKey[calculatedKey];
545
+ if (existingElement) {
546
+ renderedExistingElements[calculatedKey] = true;
547
+ const currentValue = existingElement.elementState.getValue();
548
+ if (currentValue !== element) {
549
+ existingElement.elementState.setValue(element);
550
+ }
551
+ const currentIndex = existingElement.indexState.getValue();
552
+ if (currentIndex !== index) {
553
+ existingElement.indexState.setValue(index);
554
+ }
555
+ newRenderedElements.push([
556
+ existingElement.node,
557
+ calculatedKey,
558
+ existingElement.elementState
559
+ ]);
560
+ newElementsByKey[calculatedKey] = {
561
+ elementState: existingElement.elementState,
562
+ indexState: existingElement.indexState,
563
+ indexValue: index,
564
+ node: existingElement.node
565
+ };
566
+ } else {
567
+ const elementState = createState2(element);
568
+ const indexState = createState2(index);
569
+ addPublicContext(savedContext);
570
+ const node = cb({ elementState, indexState });
571
+ const renderedNode = renderTree(node);
572
+ node.executedVersion = renderedNode;
573
+ popPublicContext();
574
+ newRenderedElements.push([node, calculatedKey, elementState]);
575
+ newElementsByKey[calculatedKey] = {
576
+ elementState,
577
+ indexState,
578
+ indexValue: index,
579
+ node
580
+ };
581
+ }
582
+ });
583
+ const newChildRenderedComponents = [];
584
+ const newChildComponents = [];
585
+ const positioningOffset = {};
586
+ let newElementsCount = 0;
587
+ let offset = 0;
588
+ let currentElement = null;
589
+ newRenderedElements.forEach((newRenderedElement, index) => {
590
+ var _a, _b, _c;
591
+ newChildRenderedComponents.push(newRenderedElement[0].executedVersion);
592
+ newChildComponents.push(newRenderedElement[0]);
593
+ if (positioningOffset[index]) {
594
+ offset = offset + positioningOffset[index];
595
+ }
596
+ const [newNode, calculatedKey, newState] = newRenderedElement;
597
+ const existingElement = elementsByKey[calculatedKey];
598
+ if (existingElement) {
599
+ const existingElementNode = getExecutedComponentVelesNode(
600
+ existingElement.node.executedVersion
601
+ );
602
+ if (existingElement.indexValue + offset === index) {
603
+ currentElement = existingElementNode.html;
604
+ return;
605
+ }
606
+ if (existingElement.indexValue + offset > index) {
607
+ if (currentElement) {
608
+ currentElement.after(existingElementNode.html);
609
+ positioningOffset[existingElement.indexValue + 1] = -1;
610
+ } else {
611
+ const firstRenderedElement = (_a = renderedElements[0]) == null ? void 0 : _a[0];
612
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
613
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
614
+ firstRenderedElement.executedVersion
615
+ );
616
+ firstRenderedVelesNode.html.before(existingElementNode.html);
617
+ } else {
618
+ }
619
+ }
620
+ currentElement = existingElementNode.html;
621
+ offset = offset + 1;
622
+ } else {
623
+ if (currentElement) {
624
+ currentElement.after(existingElementNode.html);
625
+ positioningOffset[existingElement.indexValue + 1] = 1;
626
+ } else {
627
+ const firstRenderedElement = (_b = renderedElements[0]) == null ? void 0 : _b[0];
628
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
629
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
630
+ firstRenderedElement.executedVersion
631
+ );
632
+ firstRenderedVelesNode.html.before(existingElementNode.html);
633
+ } else {
634
+ }
635
+ }
636
+ currentElement = existingElementNode.html;
637
+ offset = offset - 1;
638
+ }
639
+ } else {
640
+ const newNodeVelesElement = getExecutedComponentVelesNode(
641
+ newNode.executedVersion
642
+ );
643
+ newNodeVelesElement.parentVelesElement = parentVelesElement;
644
+ if (currentElement) {
645
+ currentElement.after(newNodeVelesElement.html);
646
+ } else {
647
+ const firstRenderedElement = (_c = renderedElements[0]) == null ? void 0 : _c[0];
648
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
649
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
650
+ firstRenderedElement.executedVersion
651
+ );
652
+ firstRenderedVelesNode.html.before(newNodeVelesElement.html);
653
+ } else {
654
+ parentVelesElement.html.prepend(newNodeVelesElement.html);
655
+ }
656
+ }
657
+ offset = offset + 1;
658
+ currentElement = newNodeVelesElement.html;
659
+ newElementsCount = newElementsCount + 1;
660
+ callMountHandlers(newNode.executedVersion);
661
+ }
662
+ });
663
+ if (renderedElements.length === newRenderedElements.length + newElementsCount) {
664
+ } else {
665
+ renderedElements.forEach(([oldNode, calculatedKey]) => {
666
+ if (renderedExistingElements[calculatedKey] === true) {
667
+ return;
668
+ } else {
669
+ const oldRenderedVelesNode = getExecutedComponentVelesNode(
670
+ oldNode.executedVersion
671
+ );
672
+ oldRenderedVelesNode.html.remove();
673
+ callUnmountHandlers(oldNode.executedVersion);
674
+ if ("executedVelesNode" in wrapperVelesElementNode) {
675
+ wrapperVelesElementNode.childComponents = wrapperVelesElementNode.childComponents.filter(
676
+ (childComponent) => childComponent !== oldNode.executedVersion
677
+ );
678
+ } else {
679
+ throw new Error("Wrapper iterator element is a string");
680
+ }
681
+ if ("velesNode" in wrapperComponent) {
682
+ wrapperComponent.childComponents = wrapperComponent.childComponents.filter(
683
+ (childComponent) => childComponent !== oldNode
684
+ );
685
+ }
686
+ }
687
+ });
688
+ }
689
+ if ("executedVelesNode" in wrapperVelesElementNode) {
690
+ wrapperVelesElementNode.childComponents = newChildRenderedComponents;
691
+ }
692
+ if ("velesNode" in wrapperComponent) {
693
+ wrapperComponent.childComponents = newChildComponents;
694
+ }
695
+ trackingIterator.renderedElements = newRenderedElements;
696
+ trackingIterator.elementsByKey = newElementsByKey;
697
+ }
698
+ }
699
+
700
+ // src/create-state/trigger-updates.ts
701
+ function triggerUpdates({
702
+ value,
703
+ createState: createState2,
704
+ trackers,
705
+ getValue
706
+ }) {
707
+ const newTrackingSelectorElements = [];
708
+ trackers.trackingSelectorElements.forEach(
709
+ (selectorTrackingElement) => updateUseValueSelector({
710
+ value,
711
+ selectorTrackingElement,
712
+ newTrackingSelectorElements,
713
+ trackers,
714
+ getValue
715
+ })
716
+ );
717
+ trackers.trackingSelectorElements = unique(
718
+ trackers.trackingSelectorElements.concat(newTrackingSelectorElements)
719
+ );
720
+ trackers.trackingAttributes.forEach((element) => {
721
+ updateUseAttributeValue({ element, value });
722
+ });
723
+ trackers.trackingEffects.forEach((trackingEffect) => {
724
+ const { cb, selectedValue, selector, comparator } = trackingEffect;
725
+ const newSelectedValue = selector ? selector(value) : value;
726
+ if (comparator ? comparator(selectedValue, newSelectedValue) : selectedValue === newSelectedValue) {
727
+ return;
728
+ }
729
+ cb(newSelectedValue);
730
+ trackingEffect.selectedValue = newSelectedValue;
731
+ });
732
+ trackers.trackingIterators.forEach((trackingIterator) => {
733
+ updateUseValueIteratorValue({ value, trackingIterator, createState: createState2 });
734
+ });
735
+ }
736
+
737
+ // src/create-state/index.ts
738
+ function createState(initialValue, subscribeCallback) {
739
+ let value = initialValue;
740
+ let previousValue = void 0;
741
+ const trackers = {
742
+ trackingEffects: [],
743
+ trackingSelectorElements: [],
744
+ trackingAttributes: [],
745
+ trackingIterators: []
746
+ };
747
+ const result = {
748
+ // supposed to be used at the component level
749
+ trackValue: (cb, options = {}) => {
750
+ result.trackValueSelector(void 0, cb, options);
751
+ },
752
+ trackValueSelector(selector, cb, options = {}) {
753
+ const trackedValue = selector ? selector(value) : value;
754
+ trackers.trackingEffects.push({
755
+ cb,
756
+ selector,
757
+ comparator: options.comparator,
758
+ selectedValue: trackedValue
759
+ });
760
+ if (!options.skipFirstCall) {
761
+ if (options.callOnMount) {
762
+ onMount(() => {
763
+ cb(trackedValue);
764
+ });
765
+ } else {
766
+ cb(trackedValue);
767
+ }
768
+ }
769
+ onUnmount(() => {
770
+ trackers.trackingEffects = trackers.trackingEffects.filter(
771
+ (trackingCallback) => trackingCallback.cb !== cb
772
+ );
773
+ });
774
+ },
775
+ useValue: (cb, comparator) => {
776
+ return result.useValueSelector(void 0, cb, comparator);
777
+ },
778
+ useValueSelector(selector, cb, comparator = identity) {
779
+ const selectedValue = selector ? selector(value) : value;
780
+ const returnedNode = cb ? cb(selectedValue) : selectedValue == void 0 ? "" : String(selectedValue);
781
+ const node = !returnedNode || typeof returnedNode === "string" ? createTextElement(returnedNode) : returnedNode;
782
+ const currentContext = getCurrentContext();
783
+ node.needExecutedVersion = true;
784
+ const trackingSelectorElement = {
785
+ selector,
786
+ selectedValue,
787
+ cb,
788
+ node,
789
+ comparator,
790
+ savedContext: currentContext
791
+ };
792
+ addUseValueMountHandler({
793
+ usedValue: value,
794
+ getValue: () => value,
795
+ trackers,
796
+ trackingSelectorElement
797
+ });
798
+ return node;
799
+ },
800
+ useValueIterator(options, cb) {
801
+ const currentContext = getCurrentContext();
802
+ const trackingParams = {};
803
+ trackingParams.savedContext = currentContext;
804
+ const wrapperComponent = createElement((_props, componentAPI) => {
805
+ const children = [];
806
+ const elementsByKey = {};
807
+ const elements = options.selector ? options.selector(value) : value;
808
+ if (!Array.isArray(elements)) {
809
+ console.error("useValueIterator received non-array value");
810
+ return null;
811
+ }
812
+ elements.forEach((element, index) => {
813
+ let calculatedKey = "";
814
+ if (typeof options.key === "string" && typeof element === "object" && element !== null && options.key in element) {
815
+ calculatedKey = element[options.key];
816
+ } else if (typeof options.key === "function") {
817
+ calculatedKey = options.key({ element, index });
818
+ } else {
819
+ }
820
+ const elementState = createState(element);
821
+ const indexState = createState(index);
822
+ if (!calculatedKey) {
823
+ return;
824
+ }
825
+ let node = cb({ elementState, indexState });
826
+ node.needExecutedVersion = true;
827
+ elementsByKey[calculatedKey] = {
828
+ node,
829
+ indexState,
830
+ indexValue: index,
831
+ elementState
832
+ };
833
+ children.push([node, calculatedKey, elementState]);
834
+ });
835
+ trackingParams.elementsByKey = elementsByKey;
836
+ trackingParams.renderedElements = children;
837
+ trackers.trackingIterators.push(trackingParams);
838
+ onMount(() => {
839
+ componentAPI.onUnmount(() => {
840
+ trackers.trackingIterators = trackers.trackingIterators.filter(
841
+ (currentTrackingParams) => currentTrackingParams !== trackingParams
842
+ );
843
+ });
844
+ });
845
+ return createElement("div", {
846
+ phantom: true,
847
+ children: children.map((child) => child[0])
848
+ });
849
+ });
850
+ wrapperComponent.needExecutedVersion = true;
851
+ trackingParams.cb = cb;
852
+ trackingParams.key = options.key;
853
+ trackingParams.wrapperComponent = wrapperComponent;
854
+ if (options.selector) {
855
+ trackingParams.selector = options.selector;
856
+ }
857
+ return wrapperComponent;
858
+ },
859
+ useAttribute: (cb) => {
860
+ const originalValue = value;
861
+ let wasMounted = false;
862
+ const attributeValue = cb ? cb(value) : value;
863
+ const attributeHelper = (htmlElement, attributeName, node) => {
864
+ const trackingElement = {
865
+ cb,
866
+ htmlElement,
867
+ attributeName,
868
+ attributeValue
869
+ };
870
+ node._privateMethods._addMountHandler(() => {
871
+ trackers.trackingAttributes.push(trackingElement);
872
+ if (!wasMounted && value === originalValue) {
873
+ } else {
874
+ updateUseAttributeValue({ element: trackingElement, value });
875
+ }
876
+ if (!wasMounted) {
877
+ wasMounted = true;
878
+ }
879
+ node._privateMethods._addUnmountHandler(() => {
880
+ trackers.trackingAttributes = trackers.trackingAttributes.filter(
881
+ (trackingAttribute) => trackingAttribute !== trackingElement
882
+ );
883
+ });
884
+ });
885
+ return attributeValue;
886
+ };
887
+ attributeHelper.velesAttribute = true;
888
+ return attributeHelper;
889
+ },
890
+ // useful for stuff like callbacks
891
+ getValue: () => {
892
+ return value;
893
+ },
894
+ getPreviousValue: () => {
895
+ return previousValue;
896
+ },
897
+ // set up new value only through the callback which
898
+ // gives the latest value to ensure no outdated data
899
+ // can be used for the state
900
+ setValue: (newValueCB) => {
901
+ const newValue = (
902
+ // @ts-expect-error
903
+ typeof newValueCB === "function" ? newValueCB(value) : newValueCB
904
+ );
905
+ if (newValue !== value) {
906
+ previousValue = value;
907
+ value = newValue;
908
+ triggerUpdates({ value, createState, trackers, getValue: () => value });
909
+ }
910
+ }
911
+ };
912
+ if (subscribeCallback) {
913
+ const unsubscribe = subscribeCallback(result.setValue);
914
+ if (unsubscribe) {
915
+ onUnmount(unsubscribe);
916
+ }
917
+ }
918
+ return result;
919
+ }
920
+
921
+ export {
922
+ createContext,
923
+ getExecutedComponentVelesNode,
924
+ renderTree,
925
+ callMountHandlers,
926
+ callUnmountHandlers,
927
+ createState
928
+ };