veles 0.0.7 → 0.0.9

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.
@@ -26,58 +26,6 @@ __export(utils_exports, {
26
26
  });
27
27
  module.exports = __toCommonJS(utils_exports);
28
28
 
29
- // src/_utils.ts
30
- function getComponentVelesNode(component) {
31
- const componentsTree = [];
32
- if ("velesStringElement" in component) {
33
- return {
34
- velesElementNode: component,
35
- componentsTree: []
36
- };
37
- }
38
- let childNode = component;
39
- while ("velesComponent" in childNode) {
40
- componentsTree.push(childNode);
41
- if ("velesStringElement" in childNode.tree) {
42
- return {
43
- velesElementNode: childNode.tree,
44
- componentsTree
45
- };
46
- } else {
47
- childNode = childNode.tree;
48
- }
49
- }
50
- return { velesElementNode: childNode, componentsTree };
51
- }
52
- function callMountHandlers(component) {
53
- if ("velesStringElement" in component) {
54
- return;
55
- }
56
- if ("velesComponent" in component) {
57
- component._privateMethods._callMountHandlers();
58
- callMountHandlers(component.tree);
59
- }
60
- if ("velesNode" in component) {
61
- component.childComponents.forEach(
62
- (childComponent) => callMountHandlers(childComponent)
63
- );
64
- }
65
- }
66
- function identity(value1, value2) {
67
- return value1 === value2;
68
- }
69
- function unique(arr) {
70
- const map = /* @__PURE__ */ new Map();
71
- const resultArr = [];
72
- arr.forEach((element) => {
73
- if (map.has(element))
74
- return;
75
- map.set(element, true);
76
- resultArr.push(element);
77
- });
78
- return resultArr;
79
- }
80
-
81
29
  // src/hooks/lifecycle.ts
82
30
  var contextStack = [];
83
31
  var currentContext = null;
@@ -104,6 +52,103 @@ function onUnmount(cb) {
104
52
  }
105
53
  }
106
54
 
55
+ // src/create-element/create-text-element.ts
56
+ function createTextElement(text) {
57
+ const mountHandlers = [];
58
+ const unmountHandlers = [];
59
+ return {
60
+ velesStringElement: true,
61
+ // in case there is no text, we create an empty Text node, so we still can
62
+ // have a reference to it, replace it, call lifecycle methods, etc
63
+ html: document.createTextNode(text || ""),
64
+ _privateMethods: {
65
+ _addMountHandler(cb) {
66
+ mountHandlers.push(cb);
67
+ },
68
+ _callMountHandlers() {
69
+ mountHandlers.forEach((cb) => cb());
70
+ },
71
+ _addUnmountHandler: (cb) => {
72
+ unmountHandlers.push(cb);
73
+ },
74
+ _callUnmountHandlers: () => {
75
+ unmountHandlers.forEach((cb) => cb());
76
+ }
77
+ }
78
+ };
79
+ }
80
+
81
+ // src/create-element/parse-component.ts
82
+ function parseComponent({
83
+ element,
84
+ props
85
+ }) {
86
+ const mountCbs = [];
87
+ const unmountCbs = [];
88
+ return {
89
+ velesComponentObject: true,
90
+ element,
91
+ props,
92
+ _privateMethods: {
93
+ _addMountHandler(cb) {
94
+ mountCbs.push(cb);
95
+ },
96
+ _addUnmountHandler: (cb) => {
97
+ unmountCbs.push(cb);
98
+ },
99
+ _callMountHandlers: () => {
100
+ mountCbs.forEach((cb) => cb());
101
+ },
102
+ _callUnmountHandlers: () => {
103
+ unmountCbs.forEach((cb) => cb());
104
+ }
105
+ }
106
+ };
107
+ }
108
+ function executeComponent({
109
+ element,
110
+ props
111
+ }) {
112
+ let componentUnmountCbs = [];
113
+ let componentMountCbs = [];
114
+ const componentAPI = {
115
+ onMount: (cb) => {
116
+ componentMountCbs.push(cb);
117
+ },
118
+ onUnmount: (cb) => {
119
+ componentUnmountCbs.push(cb);
120
+ }
121
+ };
122
+ addContext(componentAPI);
123
+ const _componentTree = element(props, componentAPI);
124
+ const componentTree = typeof _componentTree === "string" || !_componentTree ? createTextElement(_componentTree) : _componentTree;
125
+ popContext();
126
+ const velesComponent = {
127
+ velesComponent: true,
128
+ tree: componentTree,
129
+ _privateMethods: {
130
+ _addMountHandler(cb) {
131
+ componentMountCbs.push(cb);
132
+ },
133
+ _addUnmountHandler: (cb) => {
134
+ componentAPI.onUnmount(cb);
135
+ },
136
+ _callMountHandlers: () => {
137
+ componentMountCbs.forEach((cb) => {
138
+ const mountCbResult = cb();
139
+ if (typeof mountCbResult === "function") {
140
+ componentAPI.onUnmount(mountCbResult);
141
+ }
142
+ });
143
+ },
144
+ _callUnmountHandlers: () => {
145
+ componentUnmountCbs.forEach((cb) => cb());
146
+ }
147
+ }
148
+ };
149
+ return velesComponent;
150
+ }
151
+
107
152
  // src/create-element/parse-children.ts
108
153
  function parseChildren({
109
154
  children,
@@ -114,30 +159,36 @@ function parseChildren({
114
159
  if (children === void 0 || children === null) {
115
160
  return childComponents;
116
161
  }
162
+ let lastInsertedNode = null;
117
163
  (Array.isArray(children) ? children : [children]).forEach(
118
164
  (childComponent) => {
119
165
  if (typeof childComponent === "string") {
120
- const text = document.createTextNode(childComponent);
121
- htmlElement.append(text);
166
+ const textNode = createTextElement(childComponent);
167
+ htmlElement.append(textNode.html);
168
+ lastInsertedNode = textNode.html;
169
+ childComponents.push(textNode);
122
170
  } else if (typeof childComponent === "number") {
123
- const text = document.createTextNode(String(childComponent));
124
- htmlElement.append(text);
171
+ const textNode = createTextElement(String(childComponent));
172
+ htmlElement.append(textNode.html);
173
+ lastInsertedNode = textNode.html;
174
+ childComponents.push(textNode);
125
175
  } else if (typeof childComponent === "object" && childComponent && "velesNode" in childComponent && (childComponent == null ? void 0 : childComponent.velesNode)) {
126
176
  if (childComponent.phantom) {
127
177
  childComponent.childComponents.forEach((childComponentofPhantom) => {
128
178
  if ("velesNode" in childComponentofPhantom) {
129
179
  htmlElement.append(childComponentofPhantom.html);
130
180
  childComponentofPhantom.parentVelesElement = velesNode;
131
- } else {
132
- const { velesElementNode } = getComponentVelesNode(
133
- childComponentofPhantom
134
- );
181
+ lastInsertedNode = childComponentofPhantom.html;
182
+ } else if ("velesStringElement" in childComponentofPhantom) {
183
+ const velesElementNode = childComponentofPhantom;
135
184
  if (!velesElementNode) {
136
185
  console.error("can't find HTML tree in a component chain");
137
186
  } else {
138
187
  htmlElement.append(velesElementNode.html);
188
+ lastInsertedNode = velesElementNode.html;
139
189
  velesElementNode.parentVelesElement = velesNode;
140
190
  }
191
+ } else {
141
192
  }
142
193
  });
143
194
  childComponent.parentVelesElement = velesNode;
@@ -146,39 +197,18 @@ function parseChildren({
146
197
  htmlElement.append(childComponent.html);
147
198
  childComponent.parentVelesElement = velesNode;
148
199
  childComponents.push(childComponent);
200
+ lastInsertedNode = childComponent.html;
149
201
  }
150
- } else if (typeof childComponent === "object" && childComponent && "velesComponent" in childComponent && (childComponent == null ? void 0 : childComponent.velesComponent)) {
151
- const { componentsTree, velesElementNode } = getComponentVelesNode(childComponent);
152
- if (!velesElementNode) {
153
- console.error("can't find HTML tree in a component chain");
154
- } else {
155
- if ("velesNode" in velesElementNode && velesElementNode.phantom) {
156
- velesElementNode.childComponents.forEach(
157
- (childComponentofPhantom) => {
158
- if ("velesNode" in childComponentofPhantom) {
159
- htmlElement.append(childComponentofPhantom.html);
160
- childComponentofPhantom.parentVelesElement = velesNode;
161
- } else {
162
- const { componentsTree: componentsTree2, velesElementNode: velesElementNode2 } = getComponentVelesNode(childComponentofPhantom);
163
- if (!velesElementNode2) {
164
- console.error("can't find HTML tree in a component chain");
165
- } else {
166
- htmlElement.append(velesElementNode2.html);
167
- velesElementNode2.parentVelesElement = velesNode;
168
- }
169
- }
170
- }
171
- );
172
- } else {
173
- htmlElement.append(velesElementNode.html);
174
- }
175
- velesElementNode.parentVelesElement = velesNode;
176
- childComponents.push(childComponent);
177
- }
202
+ } else if (typeof childComponent === "object" && childComponent && "velesComponentObject" in childComponent) {
203
+ childComponent.insertAfter = lastInsertedNode;
204
+ childComponent.parentVelesElement = velesNode;
205
+ childComponents.push(childComponent);
206
+ lastInsertedNode = childComponent;
178
207
  } else if (typeof childComponent === "object" && childComponent && "velesStringElement" in childComponent && (childComponent == null ? void 0 : childComponent.velesStringElement)) {
179
208
  htmlElement.append(childComponent.html);
180
209
  childComponent.parentVelesElement = velesNode;
181
210
  childComponents.push(childComponent);
211
+ lastInsertedNode = childComponent.html;
182
212
  }
183
213
  }
184
214
  );
@@ -195,69 +225,30 @@ function assignAttributes({
195
225
  const isFunction = typeof value === "function";
196
226
  if (isFunction && value.velesAttribute === true) {
197
227
  const attributeValue = value(htmlElement, key, velesNode);
198
- htmlElement.setAttribute(key, attributeValue);
199
- } else if (
200
- // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
201
- isFunction && key.length > 2 && key.startsWith("on")
202
- ) {
203
- htmlElement.addEventListener(
204
- key[2].toLocaleLowerCase() + key.slice(3),
205
- value
206
- );
228
+ assignAttribute({ key, value: attributeValue, htmlElement });
207
229
  } else {
208
- htmlElement.setAttribute(key, value);
230
+ assignAttribute({ key, value, htmlElement });
209
231
  }
210
232
  });
211
233
  }
212
-
213
- // src/create-element/parse-component.ts
214
- function parseComponent({
215
- element,
216
- props
234
+ function assignAttribute({
235
+ key,
236
+ value,
237
+ htmlElement
217
238
  }) {
218
- const componentUnmountCbs = [];
219
- const componentMountCbs = [];
220
- const componentAPI = {
221
- onMount: (cb) => {
222
- componentMountCbs.push(cb);
223
- },
224
- onUnmount: (cb) => {
225
- componentUnmountCbs.push(cb);
226
- }
227
- };
228
- addContext(componentAPI);
229
- const _componentTree = element(props, componentAPI);
230
- const componentTree = typeof _componentTree === "string" || !_componentTree ? {
231
- velesStringElement: true,
232
- html: document.createTextNode(
233
- typeof _componentTree === "string" ? _componentTree : ""
234
- )
235
- } : _componentTree;
236
- popContext();
237
- const velesComponent = {
238
- velesComponent: true,
239
- tree: componentTree,
240
- _privateMethods: {
241
- _addUnmountHandler: (cb) => {
242
- componentAPI.onUnmount(cb);
243
- },
244
- _callMountHandlers: () => {
245
- componentMountCbs.forEach((cb) => {
246
- const mountCbResult = cb();
247
- if (typeof mountCbResult === "function") {
248
- componentAPI.onUnmount(mountCbResult);
249
- }
250
- });
251
- },
252
- _callUnmountHandlers: () => {
253
- componentUnmountCbs.forEach((cb) => cb());
254
- if ("_privateMethods" in velesComponent.tree) {
255
- velesComponent.tree._privateMethods._callUnmountHandlers();
256
- }
257
- }
239
+ if (
240
+ // basically, any form of `on` handlers, like `onClick`, `onCopy`, etc
241
+ typeof value === "function" && key.startsWith("on")
242
+ ) {
243
+ htmlElement.addEventListener(key.slice(2).toLocaleLowerCase(), value);
244
+ } else {
245
+ if (typeof value === "boolean") {
246
+ if (value)
247
+ htmlElement.setAttribute(key, "");
248
+ } else {
249
+ htmlElement.setAttribute(key, value);
258
250
  }
259
- };
260
- return velesComponent;
251
+ }
261
252
  }
262
253
 
263
254
  // src/create-element/create-element.ts
@@ -274,23 +265,25 @@ function createElement(element, props = {}) {
274
265
  htmlElement: newElement,
275
266
  velesNode
276
267
  });
277
- let unmountHandlers = [];
278
- const callUnmountHandlers = () => {
279
- unmountHandlers.forEach((cb) => cb());
280
- unmountHandlers = [];
281
- childComponents.forEach((childComponent) => {
282
- childComponent._privateMethods._callUnmountHandlers();
283
- });
284
- };
268
+ const unmountHandlers = [];
285
269
  velesNode.html = newElement;
286
270
  velesNode.velesNode = true;
287
271
  velesNode.childComponents = childComponents;
288
272
  velesNode.phantom = phantom;
273
+ const mountHandlers = [];
289
274
  velesNode._privateMethods = {
290
- _addUnmountHandler: (cb) => {
275
+ _addMountHandler(cb) {
276
+ mountHandlers.push(cb);
277
+ },
278
+ _callMountHandlers() {
279
+ mountHandlers.forEach((cb) => cb());
280
+ },
281
+ _addUnmountHandler(cb) {
291
282
  unmountHandlers.push(cb);
292
283
  },
293
- _callUnmountHandlers: callUnmountHandlers
284
+ _callUnmountHandlers() {
285
+ unmountHandlers.forEach((cb) => cb());
286
+ }
294
287
  };
295
288
  assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });
296
289
  return velesNode;
@@ -302,333 +295,712 @@ function createElement(element, props = {}) {
302
295
  );
303
296
  }
304
297
 
305
- // src/create-element/create-text-element.ts
306
- function createTextElement(text) {
307
- const unmountHandlers = [];
308
- return {
309
- velesStringElement: true,
310
- // in case there is no text, we create an empty Text node, so we still can
311
- // have a reference to it, replace it, call lifecycle methods, etc
312
- html: document.createTextNode(text || ""),
313
- _privateMethods: {
314
- _addUnmountHandler: (cb) => {
315
- unmountHandlers.push(cb);
298
+ // src/context/index.ts
299
+ var publicContextStack = [];
300
+ function addPublicContext(specificContext) {
301
+ if (specificContext) {
302
+ publicContextStack.push(specificContext);
303
+ } else {
304
+ if (publicContextStack.length === 0) {
305
+ publicContextStack.push({});
306
+ } else {
307
+ const currentContext2 = publicContextStack[publicContextStack.length - 1];
308
+ publicContextStack.push(currentContext2);
309
+ }
310
+ }
311
+ }
312
+ function popPublicContext() {
313
+ publicContextStack.pop();
314
+ }
315
+ function getCurrentContext() {
316
+ return publicContextStack[publicContextStack.length - 1];
317
+ }
318
+
319
+ // src/_utils.ts
320
+ function getExecutedComponentVelesNode(component) {
321
+ if ("executedVelesStringElement" in component) {
322
+ return component;
323
+ }
324
+ let childNode = component;
325
+ while ("executedVelesComponent" in childNode) {
326
+ if ("executedVelesStringElement" in childNode.tree) {
327
+ return childNode.tree;
328
+ } else {
329
+ childNode = childNode.tree;
330
+ }
331
+ }
332
+ return childNode;
333
+ }
334
+ function renderTree(component, { parentVelesElement } = {}) {
335
+ if ("velesStringElement" in component) {
336
+ const executedString = {
337
+ executedVelesStringElement: true,
338
+ _privateMethods: component._privateMethods,
339
+ html: component.html,
340
+ parentVelesElement
341
+ };
342
+ if (component.needExecutedVersion) {
343
+ component.executedVersion = executedString;
344
+ }
345
+ return executedString;
346
+ } else if ("velesComponentObject" in component) {
347
+ addPublicContext();
348
+ const componentTree = executeComponent(component);
349
+ const executedComponent = {};
350
+ executedComponent.executedVelesComponent = true;
351
+ executedComponent.tree = renderTree(componentTree.tree);
352
+ popPublicContext();
353
+ executedComponent._privateMethods = {
354
+ ...componentTree._privateMethods,
355
+ _callMountHandlers: () => {
356
+ component._privateMethods._callMountHandlers();
357
+ componentTree._privateMethods._callMountHandlers();
316
358
  },
317
359
  _callUnmountHandlers: () => {
318
- unmountHandlers.forEach((cb) => cb());
360
+ component._privateMethods._callUnmountHandlers();
361
+ componentTree._privateMethods._callUnmountHandlers();
362
+ }
363
+ };
364
+ const newNode = getExecutedComponentVelesNode(executedComponent);
365
+ if (parentVelesElement) {
366
+ if (component.insertAfter) {
367
+ if ("velesComponentObject" in component.insertAfter) {
368
+ const lastNode = insertNode({
369
+ velesElement: newNode,
370
+ adjacentNode: component.insertAfter.html,
371
+ parentVelesElement
372
+ });
373
+ component.html = lastNode;
374
+ } else {
375
+ const lastNode = insertNode({
376
+ velesElement: newNode,
377
+ adjacentNode: component.insertAfter,
378
+ parentVelesElement
379
+ });
380
+ component.html = lastNode;
381
+ }
382
+ } else {
383
+ const lastNode = insertNode({
384
+ velesElement: newNode,
385
+ // it means we are inserting the first element
386
+ adjacentNode: null,
387
+ parentVelesElement
388
+ });
389
+ component.html = lastNode;
319
390
  }
391
+ newNode.parentVelesElement = parentVelesElement;
320
392
  }
321
- };
393
+ if (component.needExecutedVersion) {
394
+ component.executedVersion = executedComponent;
395
+ }
396
+ return executedComponent;
397
+ } else if ("velesNode" in component) {
398
+ const executedNode = {};
399
+ executedNode.executedVelesNode = true;
400
+ executedNode._privateMethods = component._privateMethods;
401
+ executedNode.html = component.html;
402
+ if (parentVelesElement) {
403
+ executedNode.parentVelesElement = parentVelesElement;
404
+ }
405
+ if (component.phantom) {
406
+ executedNode.phantom = component.phantom;
407
+ }
408
+ executedNode.childComponents = component.childComponents.map(
409
+ (childComponent) => renderTree(childComponent, { parentVelesElement: executedNode })
410
+ );
411
+ if (component.needExecutedVersion) {
412
+ component.executedVersion = executedNode;
413
+ }
414
+ return executedNode;
415
+ }
322
416
  }
323
-
324
- // src/hooks/create-state.ts
325
- function createState(initialValue, subscribeCallback) {
326
- let value = initialValue;
327
- let previousValue = void 0;
328
- let trackingEffects = [];
329
- let trackingSelectorElements = [];
330
- let trackingAttributes = [];
331
- let trackingIterators = [];
332
- const _triggerUpdates = () => {
333
- const newTrackingSelectorElements = [];
334
- trackingSelectorElements.forEach((selectorTrackingElement) => {
335
- const { selectedValue, selector, cb, node, comparator } = selectorTrackingElement;
336
- const newSelectedValue = selector ? selector(value) : value;
337
- if (comparator(selectedValue, newSelectedValue)) {
338
- newTrackingSelectorElements.push(selectorTrackingElement);
339
- return;
417
+ function insertNode({
418
+ velesElement,
419
+ adjacentNode,
420
+ parentVelesElement
421
+ }) {
422
+ if (velesElement.phantom) {
423
+ let lastInsertedNode = null;
424
+ velesElement.childComponents.forEach(
425
+ (childComponentofPhantom) => {
426
+ if ("executedVelesNode" in childComponentofPhantom) {
427
+ if (lastInsertedNode) {
428
+ lastInsertedNode.after(childComponentofPhantom.html);
429
+ } else {
430
+ if (adjacentNode) {
431
+ adjacentNode.after(childComponentofPhantom.html);
432
+ } else {
433
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
434
+ }
435
+ }
436
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
437
+ lastInsertedNode = childComponentofPhantom.html;
438
+ } else if ("executedVelesStringElement" in childComponentofPhantom) {
439
+ if (lastInsertedNode) {
440
+ lastInsertedNode.after(childComponentofPhantom.html);
441
+ } else {
442
+ if (adjacentNode) {
443
+ adjacentNode.after(childComponentofPhantom.html);
444
+ } else {
445
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
446
+ }
447
+ }
448
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
449
+ lastInsertedNode = childComponentofPhantom.html;
450
+ } else {
451
+ const executedNode = getExecutedComponentVelesNode(
452
+ childComponentofPhantom
453
+ );
454
+ if (lastInsertedNode) {
455
+ lastInsertedNode.after(executedNode.html);
456
+ } else {
457
+ if (adjacentNode) {
458
+ adjacentNode.after(executedNode.html);
459
+ } else {
460
+ parentVelesElement.html.prepend(executedNode.html);
461
+ }
462
+ }
463
+ executedNode.parentVelesElement = parentVelesElement;
464
+ lastInsertedNode = executedNode.html;
465
+ }
340
466
  }
341
- const returnednewNode = cb ? cb(newSelectedValue) : String(newSelectedValue);
342
- const newNode = !returnednewNode || typeof returnednewNode === "string" ? createTextElement(returnednewNode) : returnednewNode;
343
- const { velesElementNode: oldVelesElementNode } = getComponentVelesNode(node);
344
- const { velesElementNode: newVelesElementNode } = getComponentVelesNode(newNode);
345
- const parentVelesElement = oldVelesElementNode.parentVelesElement;
346
- const newTrackingSelectorElement = {
347
- selector,
348
- selectedValue: newSelectedValue,
349
- cb,
350
- node: newNode,
351
- comparator
467
+ );
468
+ velesElement.parentVelesElement = parentVelesElement;
469
+ return lastInsertedNode;
470
+ } else {
471
+ if (adjacentNode) {
472
+ adjacentNode.after(velesElement.html);
473
+ } else {
474
+ parentVelesElement.html.prepend(velesElement.html);
475
+ }
476
+ velesElement.parentVelesElement = parentVelesElement;
477
+ return velesElement.html;
478
+ }
479
+ }
480
+ function callMountHandlers(component) {
481
+ component._privateMethods._callMountHandlers();
482
+ if ("executedVelesStringElement" in component) {
483
+ return;
484
+ }
485
+ if ("executedVelesComponent" in component) {
486
+ callMountHandlers(component.tree);
487
+ }
488
+ if ("executedVelesNode" in component) {
489
+ component.childComponents.forEach(
490
+ (childComponent) => callMountHandlers(childComponent)
491
+ );
492
+ }
493
+ }
494
+ function callUnmountHandlers(component) {
495
+ if ("executedVelesStringElement" in component) {
496
+ } else if ("executedVelesComponent" in component) {
497
+ callUnmountHandlers(component.tree);
498
+ } else if ("executedVelesNode" in component) {
499
+ component.childComponents.forEach(
500
+ (childComponent) => callUnmountHandlers(childComponent)
501
+ );
502
+ }
503
+ component._privateMethods._callUnmountHandlers();
504
+ }
505
+ function identity(value1, value2) {
506
+ return value1 === value2;
507
+ }
508
+ function unique(arr) {
509
+ const map = /* @__PURE__ */ new Map();
510
+ const resultArr = [];
511
+ arr.forEach((element) => {
512
+ if (map.has(element))
513
+ return;
514
+ map.set(element, true);
515
+ resultArr.push(element);
516
+ });
517
+ return resultArr;
518
+ }
519
+
520
+ // src/create-state/update-usevalue-selector-value.ts
521
+ function updateUseValueSelector({
522
+ value,
523
+ selectorTrackingElement,
524
+ newTrackingSelectorElements,
525
+ trackers,
526
+ getValue
527
+ }) {
528
+ const { selectedValue, selector, cb, node, comparator, savedContext } = selectorTrackingElement;
529
+ const newSelectedValue = selector ? selector(value) : value;
530
+ if (comparator(selectedValue, newSelectedValue)) {
531
+ newTrackingSelectorElements.push(selectorTrackingElement);
532
+ return;
533
+ }
534
+ addPublicContext(savedContext);
535
+ const returnednewNode = cb ? cb(newSelectedValue) : newSelectedValue == void 0 ? "" : String(newSelectedValue);
536
+ const newNode = !returnednewNode || typeof returnednewNode === "string" ? createTextElement(returnednewNode) : returnednewNode;
537
+ const newRenderedNode = renderTree(newNode);
538
+ popPublicContext();
539
+ newNode.executedVersion = newRenderedNode;
540
+ if (!node.executedVersion) {
541
+ console.error("the node was not mounted");
542
+ return;
543
+ }
544
+ const oldVelesElementNode = getExecutedComponentVelesNode(
545
+ node.executedVersion
546
+ );
547
+ const newVelesElementNode = getExecutedComponentVelesNode(newRenderedNode);
548
+ const parentVelesElement = node.parentVelesElement;
549
+ const parentVelesElementRendered = oldVelesElementNode.parentVelesElement;
550
+ const newTrackingSelectorElement = {
551
+ selector,
552
+ selectedValue: newSelectedValue,
553
+ cb,
554
+ node: newNode,
555
+ comparator,
556
+ savedContext
557
+ };
558
+ if (parentVelesElementRendered) {
559
+ newNode.parentVelesElement = parentVelesElement;
560
+ newVelesElementNode.parentVelesElement = parentVelesElementRendered;
561
+ if ("executedVelesNode" in newVelesElementNode && newVelesElementNode.phantom) {
562
+ const insertAllPhantomChildren = (adjacentNode) => {
563
+ newVelesElementNode.childComponents.forEach(
564
+ (childComponentofPhantom) => {
565
+ if ("executedVelesNode" in childComponentofPhantom) {
566
+ adjacentNode.html.before(childComponentofPhantom.html);
567
+ childComponentofPhantom.parentVelesElement = adjacentNode.parentVelesElement;
568
+ } else {
569
+ const velesElementNode = getExecutedComponentVelesNode(
570
+ childComponentofPhantom
571
+ );
572
+ if (!velesElementNode) {
573
+ console.error("can't find HTML tree in a component chain");
574
+ } else {
575
+ adjacentNode.html.before(velesElementNode.html);
576
+ velesElementNode.parentVelesElement = adjacentNode.parentVelesElement;
577
+ }
578
+ }
579
+ }
580
+ );
352
581
  };
353
- if (parentVelesElement) {
354
- newVelesElementNode.parentVelesElement = parentVelesElement;
355
- if ("velesNode" in newVelesElementNode && newVelesElementNode.phantom) {
356
- const insertAllPhantomChildren = (adjacentNode) => {
357
- newVelesElementNode.childComponents.forEach(
358
- (childComponentofPhantom) => {
359
- if ("velesNode" in childComponentofPhantom) {
360
- adjacentNode.html.before(childComponentofPhantom.html);
361
- childComponentofPhantom.parentVelesElement = adjacentNode.parentVelesElement;
362
- } else {
363
- const { velesElementNode } = getComponentVelesNode(
364
- childComponentofPhantom
365
- );
366
- if (!velesElementNode) {
367
- console.error("can't find HTML tree in a component chain");
368
- } else {
369
- adjacentNode.html.before(velesElementNode.html);
370
- velesElementNode.parentVelesElement = adjacentNode.parentVelesElement;
371
- }
372
- }
582
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
583
+ let isInserted = false;
584
+ oldVelesElementNode.childComponents.forEach(
585
+ (childComponentofPhantom) => {
586
+ if ("executedVelesNode" in childComponentofPhantom) {
587
+ if (!isInserted) {
588
+ insertAllPhantomChildren(childComponentofPhantom);
589
+ isInserted = true;
373
590
  }
374
- );
375
- };
376
- if ("velesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
377
- let isInserted = false;
378
- oldVelesElementNode.childComponents.forEach(
379
- (childComponentofPhantom) => {
380
- if ("velesNode" in childComponentofPhantom) {
381
- if (!isInserted) {
382
- insertAllPhantomChildren(childComponentofPhantom);
383
- isInserted = true;
384
- }
385
- childComponentofPhantom.html.remove();
386
- } else {
387
- const { velesElementNode } = getComponentVelesNode(
388
- childComponentofPhantom
389
- );
390
- if (!velesElementNode) {
391
- console.error("can't find HTML tree in a component chain");
392
- } else {
393
- if (!isInserted) {
394
- insertAllPhantomChildren(velesElementNode);
395
- isInserted = true;
396
- }
397
- velesElementNode.html.remove();
398
- }
591
+ childComponentofPhantom.html.remove();
592
+ } else {
593
+ const velesElementNode = getExecutedComponentVelesNode(
594
+ childComponentofPhantom
595
+ );
596
+ if (!velesElementNode) {
597
+ console.error("can't find HTML tree in a component chain");
598
+ } else {
599
+ if (!isInserted) {
600
+ insertAllPhantomChildren(velesElementNode);
601
+ isInserted = true;
399
602
  }
603
+ velesElementNode.html.remove();
400
604
  }
401
- );
402
- } else {
403
- insertAllPhantomChildren(oldVelesElementNode);
404
- oldVelesElementNode.html.remove();
605
+ }
405
606
  }
406
- } else {
407
- if ("velesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
408
- let isInserted = false;
409
- oldVelesElementNode.childComponents.forEach(
410
- (childComponentofPhantom) => {
411
- if ("velesNode" in childComponentofPhantom) {
412
- if (!isInserted) {
413
- childComponentofPhantom.html.before(
414
- newVelesElementNode.html
415
- );
416
- isInserted = true;
417
- }
418
- childComponentofPhantom.html.remove();
419
- } else {
420
- const { velesElementNode } = getComponentVelesNode(
421
- childComponentofPhantom
422
- );
423
- if (!velesElementNode) {
424
- console.error("can't find HTML tree in a component chain");
425
- } else {
426
- if (!isInserted) {
427
- velesElementNode.html.before(newVelesElementNode.html);
428
- isInserted = true;
429
- }
430
- velesElementNode.html.remove();
431
- }
607
+ );
608
+ } else {
609
+ insertAllPhantomChildren(oldVelesElementNode);
610
+ oldVelesElementNode.html.remove();
611
+ }
612
+ } else {
613
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
614
+ let isInserted = false;
615
+ oldVelesElementNode.childComponents.forEach(
616
+ (childComponentofPhantom) => {
617
+ if ("executedVelesNode" in childComponentofPhantom) {
618
+ if (!isInserted) {
619
+ childComponentofPhantom.html.before(newVelesElementNode.html);
620
+ isInserted = true;
621
+ }
622
+ childComponentofPhantom.html.remove();
623
+ } else {
624
+ const velesElementNode = getExecutedComponentVelesNode(
625
+ childComponentofPhantom
626
+ );
627
+ if (!velesElementNode) {
628
+ console.error("can't find HTML tree in a component chain");
629
+ } else {
630
+ if (!isInserted) {
631
+ velesElementNode.html.before(newVelesElementNode.html);
632
+ isInserted = true;
432
633
  }
634
+ velesElementNode.html.remove();
433
635
  }
434
- );
435
- } else {
436
- parentVelesElement.html.replaceChild(
437
- newVelesElementNode.html,
438
- oldVelesElementNode.html
439
- );
636
+ }
440
637
  }
441
- }
442
- parentVelesElement.childComponents = parentVelesElement.childComponents.map(
443
- (childComponent) => childComponent === node ? newNode : node
444
638
  );
445
- node._privateMethods._callUnmountHandlers();
446
- callMountHandlers(newNode);
447
- newNode._privateMethods._addUnmountHandler(() => {
448
- trackingSelectorElements = trackingSelectorElements.filter(
449
- (el) => el !== newTrackingSelectorElement
450
- );
451
- });
452
639
  } else {
453
- console.log("parent node was not found");
640
+ try {
641
+ parentVelesElementRendered.html.replaceChild(
642
+ newVelesElementNode.html,
643
+ oldVelesElementNode.html
644
+ );
645
+ } catch (e) {
646
+ console.error("failed to update...");
647
+ console.log(document.body.innerHTML);
648
+ console.log(oldVelesElementNode.parentVelesElement.html.innerHTML);
649
+ console.log(
650
+ //@ts-expect-error
651
+ oldVelesElementNode.parentVelesElement.childComponents[0].html.textContent
652
+ );
653
+ }
454
654
  }
455
- newTrackingSelectorElements.push(newTrackingSelectorElement);
456
- });
457
- trackingSelectorElements = unique(
458
- trackingSelectorElements.concat(newTrackingSelectorElements)
655
+ }
656
+ parentVelesElementRendered.childComponents = parentVelesElementRendered.childComponents.map(
657
+ (childComponent) => childComponent === node.executedVersion ? newRenderedNode : childComponent
459
658
  );
460
- trackingAttributes.forEach(({ cb, htmlElement, attributeName }) => {
461
- const newAttributeValue = cb ? cb(value) : value;
462
- htmlElement.setAttribute(attributeName, newAttributeValue);
659
+ if (parentVelesElement == null ? void 0 : parentVelesElement.childComponents) {
660
+ parentVelesElement.childComponents = parentVelesElement.childComponents.map(
661
+ (childComponent) => childComponent === node ? newNode : childComponent
662
+ );
663
+ }
664
+ callUnmountHandlers(node.executedVersion);
665
+ addUseValueMountHandler({
666
+ usedValue: value,
667
+ trackers,
668
+ getValue,
669
+ trackingSelectorElement: newTrackingSelectorElement
463
670
  });
464
- trackingEffects.forEach((trackingEffect) => {
465
- const { cb, selectedValue, selector, comparator } = trackingEffect;
466
- const newSelectedValue = selector ? selector(value) : value;
467
- if (comparator ? comparator(selectedValue, newSelectedValue) : selectedValue === newSelectedValue) {
468
- return;
469
- }
470
- cb(newSelectedValue);
471
- trackingEffect.selectedValue = newSelectedValue;
671
+ callMountHandlers(newRenderedNode);
672
+ newNode._privateMethods._addUnmountHandler(() => {
673
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
674
+ (el) => el !== newTrackingSelectorElement
675
+ );
472
676
  });
473
- trackingIterators.forEach((trackingIterator) => {
474
- const {
475
- cb,
476
- key,
477
- renderedElements,
478
- elementsByKey,
479
- wrapperComponent,
480
- selector
481
- } = trackingIterator;
482
- if (!wrapperComponent) {
483
- console.error("there is no wrapper component for the iterator");
484
- return;
677
+ } else {
678
+ console.log("parent node was not found");
679
+ }
680
+ newTrackingSelectorElements.push(newTrackingSelectorElement);
681
+ }
682
+ function addUseValueMountHandler({
683
+ usedValue,
684
+ getValue,
685
+ trackers,
686
+ trackingSelectorElement
687
+ }) {
688
+ trackingSelectorElement.node._privateMethods._addMountHandler(() => {
689
+ const currentValue = getValue();
690
+ if (usedValue === currentValue) {
691
+ trackers.trackingSelectorElements.push(trackingSelectorElement);
692
+ trackingSelectorElement.node._privateMethods._addUnmountHandler(() => {
693
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
694
+ (el) => trackingSelectorElement !== el
695
+ );
696
+ });
697
+ } else {
698
+ const newTrackingSelectorElements = [];
699
+ updateUseValueSelector({
700
+ value: getValue(),
701
+ trackers,
702
+ selectorTrackingElement: trackingSelectorElement,
703
+ newTrackingSelectorElements,
704
+ getValue
705
+ });
706
+ if (newTrackingSelectorElements[0]) {
707
+ const newTrackingSelectorElement = newTrackingSelectorElements[0];
708
+ if (newTrackingSelectorElement.node === trackingSelectorElement.node) {
709
+ trackers.trackingSelectorElements.push(newTrackingSelectorElement);
710
+ newTrackingSelectorElement.node._privateMethods._addUnmountHandler(
711
+ () => {
712
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
713
+ (el) => trackingSelectorElement !== el
714
+ );
715
+ }
716
+ );
717
+ } else {
718
+ }
719
+ } else {
485
720
  }
486
- const { velesElementNode: wrapperVelesElementNode } = getComponentVelesNode(wrapperComponent);
487
- const parentVelesElement = wrapperVelesElementNode.parentVelesElement;
488
- if (!parentVelesElement) {
489
- console.error("there is no parent Veles node for the iterator wrapper");
721
+ }
722
+ });
723
+ }
724
+
725
+ // src/create-state/update-useattribute-value.ts
726
+ function updateUseAttributeValue({
727
+ element,
728
+ value
729
+ }) {
730
+ const { cb, htmlElement, attributeName, attributeValue } = element;
731
+ const newAttributeValue = cb ? cb(value) : value;
732
+ if (typeof newAttributeValue === "boolean") {
733
+ if (newAttributeValue) {
734
+ htmlElement.setAttribute(attributeName, "");
735
+ } else {
736
+ htmlElement.removeAttribute(attributeName);
737
+ }
738
+ } else if (attributeName.startsWith("on")) {
739
+ if (attributeValue === newAttributeValue) {
740
+ return;
741
+ }
742
+ const eventName = attributeName.slice(2).toLocaleLowerCase();
743
+ if (attributeValue) {
744
+ htmlElement.removeEventListener(eventName, attributeValue);
745
+ }
746
+ if (newAttributeValue && typeof newAttributeValue === "function") {
747
+ htmlElement.addEventListener(eventName, newAttributeValue);
748
+ }
749
+ element.attributeValue = newAttributeValue;
750
+ } else {
751
+ htmlElement.setAttribute(attributeName, newAttributeValue);
752
+ }
753
+ }
754
+
755
+ // src/create-state/update-usevalueiterator-value.ts
756
+ function updateUseValueIteratorValue({
757
+ value,
758
+ trackingIterator,
759
+ createState: createState2
760
+ }) {
761
+ const {
762
+ cb,
763
+ key,
764
+ renderedElements,
765
+ elementsByKey,
766
+ wrapperComponent,
767
+ selector,
768
+ savedContext
769
+ } = trackingIterator;
770
+ if (!wrapperComponent) {
771
+ console.error("there is no wrapper component for the iterator");
772
+ return;
773
+ }
774
+ if (!wrapperComponent.executedVersion) {
775
+ console.error("it seems the wrapper component was not mounted");
776
+ return;
777
+ }
778
+ const wrapperVelesElementNode = getExecutedComponentVelesNode(
779
+ wrapperComponent.executedVersion
780
+ );
781
+ const parentVelesElement = wrapperVelesElementNode.parentVelesElement;
782
+ if (!parentVelesElement) {
783
+ console.error("there is no parent Veles node for the iterator wrapper");
784
+ return;
785
+ }
786
+ const elements = selector ? selector(value) : value;
787
+ if (Array.isArray(elements)) {
788
+ const newRenderedElements = [];
789
+ const newElementsByKey = {};
790
+ const renderedExistingElements = {};
791
+ elements.forEach((element, index) => {
792
+ let calculatedKey = "";
793
+ if (typeof key === "string" && typeof element === "object" && element !== null && key in element) {
794
+ calculatedKey = element[key];
795
+ } else if (typeof key === "function") {
796
+ calculatedKey = key({ element, index });
797
+ } else {
798
+ }
799
+ if (!calculatedKey) {
490
800
  return;
491
801
  }
492
- const elements = selector ? selector(value) : value;
493
- if (Array.isArray(elements)) {
494
- const newRenderedElements = [];
495
- const newElementsByKey = {};
496
- const renderedExistingElements = {};
497
- elements.forEach((element, index) => {
498
- let calculatedKey = "";
499
- if (typeof key === "string" && typeof element === "object" && element !== null && key in element) {
500
- calculatedKey = element[key];
501
- } else if (typeof key === "function") {
502
- calculatedKey = key({ element, index });
503
- } else {
504
- }
505
- if (!calculatedKey) {
506
- return;
507
- }
508
- const existingElement = elementsByKey[calculatedKey];
509
- if (existingElement) {
510
- renderedExistingElements[calculatedKey] = true;
511
- const currentValue = existingElement.elementState.getValue();
512
- if (currentValue !== element) {
513
- existingElement.elementState.setValue(element);
514
- }
515
- const currentIndex = existingElement.indexState.getValue();
516
- if (currentIndex !== index) {
517
- existingElement.indexState.setValue(index);
518
- }
519
- newRenderedElements.push([
520
- existingElement.node,
521
- calculatedKey,
522
- existingElement.elementState
523
- ]);
524
- newElementsByKey[calculatedKey] = {
525
- elementState: existingElement.elementState,
526
- indexState: existingElement.indexState,
527
- indexValue: index,
528
- node: existingElement.node
529
- };
802
+ const existingElement = elementsByKey[calculatedKey];
803
+ if (existingElement) {
804
+ renderedExistingElements[calculatedKey] = true;
805
+ const currentValue = existingElement.elementState.getValue();
806
+ if (currentValue !== element) {
807
+ existingElement.elementState.setValue(element);
808
+ }
809
+ const currentIndex = existingElement.indexState.getValue();
810
+ if (currentIndex !== index) {
811
+ existingElement.indexState.setValue(index);
812
+ }
813
+ newRenderedElements.push([
814
+ existingElement.node,
815
+ calculatedKey,
816
+ existingElement.elementState
817
+ ]);
818
+ newElementsByKey[calculatedKey] = {
819
+ elementState: existingElement.elementState,
820
+ indexState: existingElement.indexState,
821
+ indexValue: index,
822
+ node: existingElement.node
823
+ };
824
+ } else {
825
+ const elementState = createState2(element);
826
+ const indexState = createState2(index);
827
+ addPublicContext(savedContext);
828
+ const node = cb({ elementState, indexState });
829
+ const renderedNode = renderTree(node);
830
+ node.executedVersion = renderedNode;
831
+ popPublicContext();
832
+ newRenderedElements.push([node, calculatedKey, elementState]);
833
+ newElementsByKey[calculatedKey] = {
834
+ elementState,
835
+ indexState,
836
+ indexValue: index,
837
+ node
838
+ };
839
+ }
840
+ });
841
+ const newChildRenderedComponents = [];
842
+ const newChildComponents = [];
843
+ const positioningOffset = {};
844
+ let newElementsCount = 0;
845
+ let offset = 0;
846
+ let currentElement = null;
847
+ newRenderedElements.forEach((newRenderedElement, index) => {
848
+ var _a, _b, _c;
849
+ newChildRenderedComponents.push(newRenderedElement[0].executedVersion);
850
+ newChildComponents.push(newRenderedElement[0]);
851
+ if (positioningOffset[index]) {
852
+ offset = offset + positioningOffset[index];
853
+ }
854
+ const [newNode, calculatedKey, newState] = newRenderedElement;
855
+ const existingElement = elementsByKey[calculatedKey];
856
+ if (existingElement) {
857
+ const existingElementNode = getExecutedComponentVelesNode(
858
+ existingElement.node.executedVersion
859
+ );
860
+ if (existingElement.indexValue + offset === index) {
861
+ currentElement = existingElementNode.html;
862
+ return;
863
+ }
864
+ if (existingElement.indexValue + offset > index) {
865
+ if (currentElement) {
866
+ currentElement.after(existingElementNode.html);
867
+ positioningOffset[existingElement.indexValue + 1] = -1;
530
868
  } else {
531
- const elementState = createState(element);
532
- const indexState = createState(index);
533
- const node = cb({ elementState, indexState });
534
- newRenderedElements.push([node, calculatedKey, elementState]);
535
- newElementsByKey[calculatedKey] = {
536
- elementState,
537
- indexState,
538
- indexValue: index,
539
- node
540
- };
541
- }
542
- });
543
- const positioningOffset = {};
544
- let newElementsCount = 0;
545
- let offset = 0;
546
- let currentElement = null;
547
- newRenderedElements.forEach((newRenderedElement, index) => {
548
- var _a, _b, _c;
549
- if (positioningOffset[index]) {
550
- offset = offset + positioningOffset[index];
551
- }
552
- const [newNode, calculatedKey, newState] = newRenderedElement;
553
- const existingElement = elementsByKey[calculatedKey];
554
- if (existingElement) {
555
- const { velesElementNode: existingElementNode } = getComponentVelesNode(existingElement.node);
556
- if (existingElement.indexValue + offset === index) {
557
- currentElement = existingElementNode.html;
558
- return;
559
- }
560
- if (existingElement.indexValue + offset > index) {
561
- if (currentElement) {
562
- currentElement.after(existingElementNode.html);
563
- positioningOffset[existingElement.indexValue + 1] = -1;
564
- } else {
565
- const firstRenderedElement = (_a = renderedElements[0]) == null ? void 0 : _a[0];
566
- if (firstRenderedElement) {
567
- const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
568
- firstRenderedVelesNode.html.before(existingElementNode.html);
569
- } else {
570
- }
571
- }
572
- currentElement = existingElementNode.html;
573
- offset = offset + 1;
869
+ const firstRenderedElement = (_a = renderedElements[0]) == null ? void 0 : _a[0];
870
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
871
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
872
+ firstRenderedElement.executedVersion
873
+ );
874
+ firstRenderedVelesNode.html.before(existingElementNode.html);
574
875
  } else {
575
- if (currentElement) {
576
- currentElement.after(existingElementNode.html);
577
- positioningOffset[existingElement.indexValue + 1] = 1;
578
- } else {
579
- const firstRenderedElement = (_b = renderedElements[0]) == null ? void 0 : _b[0];
580
- if (firstRenderedElement) {
581
- const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
582
- firstRenderedVelesNode.html.before(existingElementNode.html);
583
- } else {
584
- }
585
- }
586
- currentElement = existingElementNode.html;
587
- offset = offset - 1;
588
876
  }
877
+ }
878
+ currentElement = existingElementNode.html;
879
+ offset = offset + 1;
880
+ } else {
881
+ if (currentElement) {
882
+ currentElement.after(existingElementNode.html);
883
+ positioningOffset[existingElement.indexValue + 1] = 1;
589
884
  } else {
590
- const { velesElementNode: newNodeVelesElement } = getComponentVelesNode(newNode);
591
- newNodeVelesElement.parentVelesElement = parentVelesElement;
592
- if (currentElement) {
593
- currentElement.after(newNodeVelesElement.html);
885
+ const firstRenderedElement = (_b = renderedElements[0]) == null ? void 0 : _b[0];
886
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
887
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
888
+ firstRenderedElement.executedVersion
889
+ );
890
+ firstRenderedVelesNode.html.before(existingElementNode.html);
594
891
  } else {
595
- const firstRenderedElement = (_c = renderedElements[0]) == null ? void 0 : _c[0];
596
- if (firstRenderedElement) {
597
- const { velesElementNode: firstRenderedVelesNode } = getComponentVelesNode(firstRenderedElement);
598
- firstRenderedVelesNode.html.before(newNodeVelesElement.html);
599
- } else {
600
- parentVelesElement.html.prepend(newNodeVelesElement.html);
601
- }
602
892
  }
603
- offset = offset + 1;
604
- currentElement = newNodeVelesElement.html;
605
- newElementsCount = newElementsCount + 1;
606
- callMountHandlers(newNode);
607
893
  }
608
- });
609
- if (renderedElements.length === newRenderedElements.length + newElementsCount) {
894
+ currentElement = existingElementNode.html;
895
+ offset = offset - 1;
896
+ }
897
+ } else {
898
+ const newNodeVelesElement = getExecutedComponentVelesNode(
899
+ newNode.executedVersion
900
+ );
901
+ newNodeVelesElement.parentVelesElement = parentVelesElement;
902
+ if (currentElement) {
903
+ currentElement.after(newNodeVelesElement.html);
610
904
  } else {
611
- renderedElements.forEach(([oldNode, calculatedKey]) => {
612
- if (renderedExistingElements[calculatedKey] === true) {
613
- return;
614
- } else {
615
- const { velesElementNode: oldRenderedVelesNode } = getComponentVelesNode(oldNode);
616
- oldRenderedVelesNode.html.remove();
617
- oldNode._privateMethods._callUnmountHandlers();
618
- if ("velesNode" in wrapperVelesElementNode) {
619
- wrapperVelesElementNode.childComponents = wrapperVelesElementNode.childComponents.filter(
620
- (childComponent) => childComponent !== oldNode
621
- );
622
- } else {
623
- throw new Error("Wrapper iterator element is a string");
624
- }
625
- }
626
- });
905
+ const firstRenderedElement = (_c = renderedElements[0]) == null ? void 0 : _c[0];
906
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
907
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
908
+ firstRenderedElement.executedVersion
909
+ );
910
+ firstRenderedVelesNode.html.before(newNodeVelesElement.html);
911
+ } else {
912
+ parentVelesElement.html.prepend(newNodeVelesElement.html);
913
+ }
627
914
  }
628
- trackingIterator.renderedElements = newRenderedElements;
629
- trackingIterator.elementsByKey = newElementsByKey;
915
+ offset = offset + 1;
916
+ currentElement = newNodeVelesElement.html;
917
+ newElementsCount = newElementsCount + 1;
918
+ callMountHandlers(newNode.executedVersion);
630
919
  }
631
920
  });
921
+ if (renderedElements.length === newRenderedElements.length + newElementsCount) {
922
+ } else {
923
+ renderedElements.forEach(([oldNode, calculatedKey]) => {
924
+ if (renderedExistingElements[calculatedKey] === true) {
925
+ return;
926
+ } else {
927
+ const oldRenderedVelesNode = getExecutedComponentVelesNode(
928
+ oldNode.executedVersion
929
+ );
930
+ oldRenderedVelesNode.html.remove();
931
+ callUnmountHandlers(oldNode.executedVersion);
932
+ if ("executedVelesNode" in wrapperVelesElementNode) {
933
+ wrapperVelesElementNode.childComponents = wrapperVelesElementNode.childComponents.filter(
934
+ (childComponent) => childComponent !== oldNode.executedVersion
935
+ );
936
+ } else {
937
+ throw new Error("Wrapper iterator element is a string");
938
+ }
939
+ if ("velesNode" in wrapperComponent) {
940
+ wrapperComponent.childComponents = wrapperComponent.childComponents.filter(
941
+ (childComponent) => childComponent !== oldNode
942
+ );
943
+ }
944
+ }
945
+ });
946
+ }
947
+ if ("executedVelesNode" in wrapperVelesElementNode) {
948
+ wrapperVelesElementNode.childComponents = newChildRenderedComponents;
949
+ }
950
+ if ("velesNode" in wrapperComponent) {
951
+ wrapperComponent.childComponents = newChildComponents;
952
+ }
953
+ trackingIterator.renderedElements = newRenderedElements;
954
+ trackingIterator.elementsByKey = newElementsByKey;
955
+ }
956
+ }
957
+
958
+ // src/create-state/trigger-updates.ts
959
+ function triggerUpdates({
960
+ value,
961
+ createState: createState2,
962
+ trackers,
963
+ getValue
964
+ }) {
965
+ const newTrackingSelectorElements = [];
966
+ trackers.trackingSelectorElements.forEach(
967
+ (selectorTrackingElement) => updateUseValueSelector({
968
+ value,
969
+ selectorTrackingElement,
970
+ newTrackingSelectorElements,
971
+ trackers,
972
+ getValue
973
+ })
974
+ );
975
+ trackers.trackingSelectorElements = unique(
976
+ trackers.trackingSelectorElements.concat(newTrackingSelectorElements)
977
+ );
978
+ trackers.trackingAttributes.forEach((element) => {
979
+ updateUseAttributeValue({ element, value });
980
+ });
981
+ trackers.trackingEffects.forEach((trackingEffect) => {
982
+ const { cb, selectedValue, selector, comparator } = trackingEffect;
983
+ const newSelectedValue = selector ? selector(value) : value;
984
+ if (comparator ? comparator(selectedValue, newSelectedValue) : selectedValue === newSelectedValue) {
985
+ return;
986
+ }
987
+ cb(newSelectedValue);
988
+ trackingEffect.selectedValue = newSelectedValue;
989
+ });
990
+ trackers.trackingIterators.forEach((trackingIterator) => {
991
+ updateUseValueIteratorValue({ value, trackingIterator, createState: createState2 });
992
+ });
993
+ }
994
+
995
+ // src/create-state/index.ts
996
+ function createState(initialValue, subscribeCallback) {
997
+ let value = initialValue;
998
+ let previousValue = void 0;
999
+ const trackers = {
1000
+ trackingEffects: [],
1001
+ trackingSelectorElements: [],
1002
+ trackingAttributes: [],
1003
+ trackingIterators: []
632
1004
  };
633
1005
  const result = {
634
1006
  // supposed to be used at the component level
@@ -637,7 +1009,7 @@ function createState(initialValue, subscribeCallback) {
637
1009
  },
638
1010
  trackValueSelector(selector, cb, options = {}) {
639
1011
  const trackedValue = selector ? selector(value) : value;
640
- trackingEffects.push({
1012
+ trackers.trackingEffects.push({
641
1013
  cb,
642
1014
  selector,
643
1015
  comparator: options.comparator,
@@ -653,7 +1025,7 @@ function createState(initialValue, subscribeCallback) {
653
1025
  }
654
1026
  }
655
1027
  onUnmount(() => {
656
- trackingEffects = trackingEffects.filter(
1028
+ trackers.trackingEffects = trackers.trackingEffects.filter(
657
1029
  (trackingCallback) => trackingCallback.cb !== cb
658
1030
  );
659
1031
  });
@@ -663,70 +1035,79 @@ function createState(initialValue, subscribeCallback) {
663
1035
  },
664
1036
  useValueSelector(selector, cb, comparator = identity) {
665
1037
  const selectedValue = selector ? selector(value) : value;
666
- const returnedNode = cb ? cb(selectedValue) : String(selectedValue);
1038
+ const returnedNode = cb ? cb(selectedValue) : selectedValue == void 0 ? "" : String(selectedValue);
667
1039
  const node = !returnedNode || typeof returnedNode === "string" ? createTextElement(returnedNode) : returnedNode;
1040
+ const currentContext2 = getCurrentContext();
1041
+ node.needExecutedVersion = true;
668
1042
  const trackingSelectorElement = {
669
1043
  selector,
670
1044
  selectedValue,
671
1045
  cb,
672
1046
  node,
673
- comparator
1047
+ comparator,
1048
+ savedContext: currentContext2
674
1049
  };
675
- trackingSelectorElements.push(trackingSelectorElement);
676
- node._privateMethods._addUnmountHandler(() => {
677
- trackingSelectorElements = trackingSelectorElements.filter(
678
- (el) => trackingSelectorElement !== el
679
- );
1050
+ addUseValueMountHandler({
1051
+ usedValue: value,
1052
+ getValue: () => value,
1053
+ trackers,
1054
+ trackingSelectorElement
680
1055
  });
681
1056
  return node;
682
1057
  },
683
1058
  useValueIterator(options, cb) {
684
- const children = [];
685
- const elementsByKey = {};
686
- const elements = options.selector ? options.selector(value) : value;
687
- if (!Array.isArray(elements)) {
688
- console.error("useValueIterator received non-array value");
689
- return null;
690
- }
691
- elements.forEach((element, index) => {
692
- let calculatedKey = "";
693
- if (typeof options.key === "string" && typeof element === "object" && element !== null && options.key in element) {
694
- calculatedKey = element[options.key];
695
- } else if (typeof options.key === "function") {
696
- calculatedKey = options.key({ element, index });
697
- } else {
698
- }
699
- const elementState = createState(element);
700
- const indexState = createState(index);
701
- if (!calculatedKey) {
702
- return;
703
- }
704
- let node = cb({ elementState, indexState });
705
- elementsByKey[calculatedKey] = {
706
- node,
707
- indexState,
708
- indexValue: index,
709
- elementState
710
- };
711
- children.push([node, calculatedKey, elementState]);
712
- });
1059
+ const currentContext2 = getCurrentContext();
713
1060
  const trackingParams = {};
714
- trackingIterators.push(trackingParams);
715
- const wrapperComponent = createElement(() => {
716
- onUnmount(() => {
717
- trackingIterators = trackingIterators.filter(
718
- (currentTrackingParams) => currentTrackingParams !== trackingParams
719
- );
1061
+ trackingParams.savedContext = currentContext2;
1062
+ const wrapperComponent = createElement((_props, componentAPI) => {
1063
+ const children = [];
1064
+ const elementsByKey = {};
1065
+ const elements = options.selector ? options.selector(value) : value;
1066
+ if (!Array.isArray(elements)) {
1067
+ console.error("useValueIterator received non-array value");
1068
+ return null;
1069
+ }
1070
+ elements.forEach((element, index) => {
1071
+ let calculatedKey = "";
1072
+ if (typeof options.key === "string" && typeof element === "object" && element !== null && options.key in element) {
1073
+ calculatedKey = element[options.key];
1074
+ } else if (typeof options.key === "function") {
1075
+ calculatedKey = options.key({ element, index });
1076
+ } else {
1077
+ }
1078
+ const elementState = createState(element);
1079
+ const indexState = createState(index);
1080
+ if (!calculatedKey) {
1081
+ return;
1082
+ }
1083
+ let node = cb({ elementState, indexState });
1084
+ node.needExecutedVersion = true;
1085
+ elementsByKey[calculatedKey] = {
1086
+ node,
1087
+ indexState,
1088
+ indexValue: index,
1089
+ elementState
1090
+ };
1091
+ children.push([node, calculatedKey, elementState]);
1092
+ });
1093
+ trackingParams.elementsByKey = elementsByKey;
1094
+ trackingParams.renderedElements = children;
1095
+ trackers.trackingIterators.push(trackingParams);
1096
+ onMount(() => {
1097
+ componentAPI.onUnmount(() => {
1098
+ trackers.trackingIterators = trackers.trackingIterators.filter(
1099
+ (currentTrackingParams) => currentTrackingParams !== trackingParams
1100
+ );
1101
+ });
720
1102
  });
721
1103
  return createElement("div", {
722
1104
  phantom: true,
723
1105
  children: children.map((child) => child[0])
724
1106
  });
725
1107
  });
1108
+ wrapperComponent.needExecutedVersion = true;
726
1109
  trackingParams.cb = cb;
727
1110
  trackingParams.key = options.key;
728
- trackingParams.elementsByKey = elementsByKey;
729
- trackingParams.renderedElements = children;
730
1111
  trackingParams.wrapperComponent = wrapperComponent;
731
1112
  if (options.selector) {
732
1113
  trackingParams.selector = options.selector;
@@ -734,14 +1115,30 @@ function createState(initialValue, subscribeCallback) {
734
1115
  return wrapperComponent;
735
1116
  },
736
1117
  useAttribute: (cb) => {
1118
+ const originalValue = value;
1119
+ let wasMounted = false;
737
1120
  const attributeValue = cb ? cb(value) : value;
738
1121
  const attributeHelper = (htmlElement, attributeName, node) => {
739
- const trackingElement = { cb, htmlElement, attributeName };
740
- trackingAttributes.push(trackingElement);
741
- node._privateMethods._addUnmountHandler(() => {
742
- trackingAttributes = trackingAttributes.filter(
743
- (trackingAttribute) => trackingAttribute !== trackingElement
744
- );
1122
+ const trackingElement = {
1123
+ cb,
1124
+ htmlElement,
1125
+ attributeName,
1126
+ attributeValue
1127
+ };
1128
+ node._privateMethods._addMountHandler(() => {
1129
+ trackers.trackingAttributes.push(trackingElement);
1130
+ if (!wasMounted && value === originalValue) {
1131
+ } else {
1132
+ updateUseAttributeValue({ element: trackingElement, value });
1133
+ }
1134
+ if (!wasMounted) {
1135
+ wasMounted = true;
1136
+ }
1137
+ node._privateMethods._addUnmountHandler(() => {
1138
+ trackers.trackingAttributes = trackers.trackingAttributes.filter(
1139
+ (trackingAttribute) => trackingAttribute !== trackingElement
1140
+ );
1141
+ });
745
1142
  });
746
1143
  return attributeValue;
747
1144
  };
@@ -766,7 +1163,7 @@ function createState(initialValue, subscribeCallback) {
766
1163
  if (newValue !== value) {
767
1164
  previousValue = value;
768
1165
  value = newValue;
769
- _triggerUpdates();
1166
+ triggerUpdates({ value, createState, trackers, getValue: () => value });
770
1167
  }
771
1168
  }
772
1169
  };