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.
@@ -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,33 @@ 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(
244
+ key[2].toLocaleLowerCase() + key.slice(3),
245
+ value
246
+ );
247
+ } else {
248
+ if (typeof value === "boolean") {
249
+ if (value)
250
+ htmlElement.setAttribute(key, "");
251
+ } else {
252
+ htmlElement.setAttribute(key, value);
258
253
  }
259
- };
260
- return velesComponent;
254
+ }
261
255
  }
262
256
 
263
257
  // src/create-element/create-element.ts
@@ -274,23 +268,25 @@ function createElement(element, props = {}) {
274
268
  htmlElement: newElement,
275
269
  velesNode
276
270
  });
277
- let unmountHandlers = [];
278
- const callUnmountHandlers = () => {
279
- unmountHandlers.forEach((cb) => cb());
280
- unmountHandlers = [];
281
- childComponents.forEach((childComponent) => {
282
- childComponent._privateMethods._callUnmountHandlers();
283
- });
284
- };
271
+ const unmountHandlers = [];
285
272
  velesNode.html = newElement;
286
273
  velesNode.velesNode = true;
287
274
  velesNode.childComponents = childComponents;
288
275
  velesNode.phantom = phantom;
276
+ const mountHandlers = [];
289
277
  velesNode._privateMethods = {
290
- _addUnmountHandler: (cb) => {
278
+ _addMountHandler(cb) {
279
+ mountHandlers.push(cb);
280
+ },
281
+ _callMountHandlers() {
282
+ mountHandlers.forEach((cb) => cb());
283
+ },
284
+ _addUnmountHandler(cb) {
291
285
  unmountHandlers.push(cb);
292
286
  },
293
- _callUnmountHandlers: callUnmountHandlers
287
+ _callUnmountHandlers() {
288
+ unmountHandlers.forEach((cb) => cb());
289
+ }
294
290
  };
295
291
  assignAttributes({ props: otherProps, htmlElement: newElement, velesNode });
296
292
  return velesNode;
@@ -302,333 +298,712 @@ function createElement(element, props = {}) {
302
298
  );
303
299
  }
304
300
 
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);
301
+ // src/context/index.ts
302
+ var publicContextStack = [];
303
+ function addPublicContext(specificContext) {
304
+ if (specificContext) {
305
+ publicContextStack.push(specificContext);
306
+ } else {
307
+ if (publicContextStack.length === 0) {
308
+ publicContextStack.push({});
309
+ } else {
310
+ const currentContext2 = publicContextStack[publicContextStack.length - 1];
311
+ publicContextStack.push(currentContext2);
312
+ }
313
+ }
314
+ }
315
+ function popPublicContext() {
316
+ publicContextStack.pop();
317
+ }
318
+ function getCurrentContext() {
319
+ return publicContextStack[publicContextStack.length - 1];
320
+ }
321
+
322
+ // src/_utils.ts
323
+ function getExecutedComponentVelesNode(component) {
324
+ if ("executedVelesStringElement" in component) {
325
+ return component;
326
+ }
327
+ let childNode = component;
328
+ while ("executedVelesComponent" in childNode) {
329
+ if ("executedVelesStringElement" in childNode.tree) {
330
+ return childNode.tree;
331
+ } else {
332
+ childNode = childNode.tree;
333
+ }
334
+ }
335
+ return childNode;
336
+ }
337
+ function renderTree(component, { parentVelesElement } = {}) {
338
+ if ("velesStringElement" in component) {
339
+ const executedString = {
340
+ executedVelesStringElement: true,
341
+ _privateMethods: component._privateMethods,
342
+ html: component.html,
343
+ parentVelesElement
344
+ };
345
+ if (component.needExecutedVersion) {
346
+ component.executedVersion = executedString;
347
+ }
348
+ return executedString;
349
+ } else if ("velesComponentObject" in component) {
350
+ addPublicContext();
351
+ const componentTree = executeComponent(component);
352
+ const executedComponent = {};
353
+ executedComponent.executedVelesComponent = true;
354
+ executedComponent.tree = renderTree(componentTree.tree);
355
+ popPublicContext();
356
+ executedComponent._privateMethods = {
357
+ ...componentTree._privateMethods,
358
+ _callMountHandlers: () => {
359
+ component._privateMethods._callMountHandlers();
360
+ componentTree._privateMethods._callMountHandlers();
316
361
  },
317
362
  _callUnmountHandlers: () => {
318
- unmountHandlers.forEach((cb) => cb());
363
+ component._privateMethods._callUnmountHandlers();
364
+ componentTree._privateMethods._callUnmountHandlers();
365
+ }
366
+ };
367
+ const newNode = getExecutedComponentVelesNode(executedComponent);
368
+ if (parentVelesElement) {
369
+ if (component.insertAfter) {
370
+ if ("velesComponentObject" in component.insertAfter) {
371
+ const lastNode = insertNode({
372
+ velesElement: newNode,
373
+ adjacentNode: component.insertAfter.html,
374
+ parentVelesElement
375
+ });
376
+ component.html = lastNode;
377
+ } else {
378
+ const lastNode = insertNode({
379
+ velesElement: newNode,
380
+ adjacentNode: component.insertAfter,
381
+ parentVelesElement
382
+ });
383
+ component.html = lastNode;
384
+ }
385
+ } else {
386
+ const lastNode = insertNode({
387
+ velesElement: newNode,
388
+ // it means we are inserting the first element
389
+ adjacentNode: null,
390
+ parentVelesElement
391
+ });
392
+ component.html = lastNode;
319
393
  }
394
+ newNode.parentVelesElement = parentVelesElement;
320
395
  }
321
- };
396
+ if (component.needExecutedVersion) {
397
+ component.executedVersion = executedComponent;
398
+ }
399
+ return executedComponent;
400
+ } else if ("velesNode" in component) {
401
+ const executedNode = {};
402
+ executedNode.executedVelesNode = true;
403
+ executedNode._privateMethods = component._privateMethods;
404
+ executedNode.html = component.html;
405
+ if (parentVelesElement) {
406
+ executedNode.parentVelesElement = parentVelesElement;
407
+ }
408
+ if (component.phantom) {
409
+ executedNode.phantom = component.phantom;
410
+ }
411
+ executedNode.childComponents = component.childComponents.map(
412
+ (childComponent) => renderTree(childComponent, { parentVelesElement: executedNode })
413
+ );
414
+ if (component.needExecutedVersion) {
415
+ component.executedVersion = executedNode;
416
+ }
417
+ return executedNode;
418
+ }
322
419
  }
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;
420
+ function insertNode({
421
+ velesElement,
422
+ adjacentNode,
423
+ parentVelesElement
424
+ }) {
425
+ if (velesElement.phantom) {
426
+ let lastInsertedNode = null;
427
+ velesElement.childComponents.forEach(
428
+ (childComponentofPhantom) => {
429
+ if ("executedVelesNode" in childComponentofPhantom) {
430
+ if (lastInsertedNode) {
431
+ lastInsertedNode.after(childComponentofPhantom.html);
432
+ } else {
433
+ if (adjacentNode) {
434
+ adjacentNode.after(childComponentofPhantom.html);
435
+ } else {
436
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
437
+ }
438
+ }
439
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
440
+ lastInsertedNode = childComponentofPhantom.html;
441
+ } else if ("executedVelesStringElement" in childComponentofPhantom) {
442
+ if (lastInsertedNode) {
443
+ lastInsertedNode.after(childComponentofPhantom.html);
444
+ } else {
445
+ if (adjacentNode) {
446
+ adjacentNode.after(childComponentofPhantom.html);
447
+ } else {
448
+ parentVelesElement.html.prepend(childComponentofPhantom.html);
449
+ }
450
+ }
451
+ childComponentofPhantom.parentVelesElement = parentVelesElement;
452
+ lastInsertedNode = childComponentofPhantom.html;
453
+ } else {
454
+ const executedNode = getExecutedComponentVelesNode(
455
+ childComponentofPhantom
456
+ );
457
+ if (lastInsertedNode) {
458
+ lastInsertedNode.after(executedNode.html);
459
+ } else {
460
+ if (adjacentNode) {
461
+ adjacentNode.after(executedNode.html);
462
+ } else {
463
+ parentVelesElement.html.prepend(executedNode.html);
464
+ }
465
+ }
466
+ executedNode.parentVelesElement = parentVelesElement;
467
+ lastInsertedNode = executedNode.html;
468
+ }
340
469
  }
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
470
+ );
471
+ velesElement.parentVelesElement = parentVelesElement;
472
+ return lastInsertedNode;
473
+ } else {
474
+ if (adjacentNode) {
475
+ adjacentNode.after(velesElement.html);
476
+ } else {
477
+ parentVelesElement.html.prepend(velesElement.html);
478
+ }
479
+ velesElement.parentVelesElement = parentVelesElement;
480
+ return velesElement.html;
481
+ }
482
+ }
483
+ function callMountHandlers(component) {
484
+ component._privateMethods._callMountHandlers();
485
+ if ("executedVelesStringElement" in component) {
486
+ return;
487
+ }
488
+ if ("executedVelesComponent" in component) {
489
+ callMountHandlers(component.tree);
490
+ }
491
+ if ("executedVelesNode" in component) {
492
+ component.childComponents.forEach(
493
+ (childComponent) => callMountHandlers(childComponent)
494
+ );
495
+ }
496
+ }
497
+ function callUnmountHandlers(component) {
498
+ if ("executedVelesStringElement" in component) {
499
+ } else if ("executedVelesComponent" in component) {
500
+ callUnmountHandlers(component.tree);
501
+ } else if ("executedVelesNode" in component) {
502
+ component.childComponents.forEach(
503
+ (childComponent) => callUnmountHandlers(childComponent)
504
+ );
505
+ }
506
+ component._privateMethods._callUnmountHandlers();
507
+ }
508
+ function identity(value1, value2) {
509
+ return value1 === value2;
510
+ }
511
+ function unique(arr) {
512
+ const map = /* @__PURE__ */ new Map();
513
+ const resultArr = [];
514
+ arr.forEach((element) => {
515
+ if (map.has(element))
516
+ return;
517
+ map.set(element, true);
518
+ resultArr.push(element);
519
+ });
520
+ return resultArr;
521
+ }
522
+
523
+ // src/create-state/update-usevalue-selector-value.ts
524
+ function updateUseValueSelector({
525
+ value,
526
+ selectorTrackingElement,
527
+ newTrackingSelectorElements,
528
+ trackers,
529
+ getValue
530
+ }) {
531
+ const { selectedValue, selector, cb, node, comparator, savedContext } = selectorTrackingElement;
532
+ const newSelectedValue = selector ? selector(value) : value;
533
+ if (comparator(selectedValue, newSelectedValue)) {
534
+ newTrackingSelectorElements.push(selectorTrackingElement);
535
+ return;
536
+ }
537
+ addPublicContext(savedContext);
538
+ const returnednewNode = cb ? cb(newSelectedValue) : newSelectedValue == void 0 ? "" : String(newSelectedValue);
539
+ const newNode = !returnednewNode || typeof returnednewNode === "string" ? createTextElement(returnednewNode) : returnednewNode;
540
+ const newRenderedNode = renderTree(newNode);
541
+ popPublicContext();
542
+ newNode.executedVersion = newRenderedNode;
543
+ if (!node.executedVersion) {
544
+ console.error("the node was not mounted");
545
+ return;
546
+ }
547
+ const oldVelesElementNode = getExecutedComponentVelesNode(
548
+ node.executedVersion
549
+ );
550
+ const newVelesElementNode = getExecutedComponentVelesNode(newRenderedNode);
551
+ const parentVelesElement = node.parentVelesElement;
552
+ const parentVelesElementRendered = oldVelesElementNode.parentVelesElement;
553
+ const newTrackingSelectorElement = {
554
+ selector,
555
+ selectedValue: newSelectedValue,
556
+ cb,
557
+ node: newNode,
558
+ comparator,
559
+ savedContext
560
+ };
561
+ if (parentVelesElementRendered) {
562
+ newNode.parentVelesElement = parentVelesElement;
563
+ newVelesElementNode.parentVelesElement = parentVelesElementRendered;
564
+ if ("executedVelesNode" in newVelesElementNode && newVelesElementNode.phantom) {
565
+ const insertAllPhantomChildren = (adjacentNode) => {
566
+ newVelesElementNode.childComponents.forEach(
567
+ (childComponentofPhantom) => {
568
+ if ("executedVelesNode" in childComponentofPhantom) {
569
+ adjacentNode.html.before(childComponentofPhantom.html);
570
+ childComponentofPhantom.parentVelesElement = adjacentNode.parentVelesElement;
571
+ } else {
572
+ const velesElementNode = getExecutedComponentVelesNode(
573
+ childComponentofPhantom
574
+ );
575
+ if (!velesElementNode) {
576
+ console.error("can't find HTML tree in a component chain");
577
+ } else {
578
+ adjacentNode.html.before(velesElementNode.html);
579
+ velesElementNode.parentVelesElement = adjacentNode.parentVelesElement;
580
+ }
581
+ }
582
+ }
583
+ );
352
584
  };
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
- }
585
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
586
+ let isInserted = false;
587
+ oldVelesElementNode.childComponents.forEach(
588
+ (childComponentofPhantom) => {
589
+ if ("executedVelesNode" in childComponentofPhantom) {
590
+ if (!isInserted) {
591
+ insertAllPhantomChildren(childComponentofPhantom);
592
+ isInserted = true;
373
593
  }
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
- }
594
+ childComponentofPhantom.html.remove();
595
+ } else {
596
+ const velesElementNode = getExecutedComponentVelesNode(
597
+ childComponentofPhantom
598
+ );
599
+ if (!velesElementNode) {
600
+ console.error("can't find HTML tree in a component chain");
601
+ } else {
602
+ if (!isInserted) {
603
+ insertAllPhantomChildren(velesElementNode);
604
+ isInserted = true;
399
605
  }
606
+ velesElementNode.html.remove();
400
607
  }
401
- );
402
- } else {
403
- insertAllPhantomChildren(oldVelesElementNode);
404
- oldVelesElementNode.html.remove();
608
+ }
405
609
  }
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
- }
610
+ );
611
+ } else {
612
+ insertAllPhantomChildren(oldVelesElementNode);
613
+ oldVelesElementNode.html.remove();
614
+ }
615
+ } else {
616
+ if ("executedVelesNode" in oldVelesElementNode && oldVelesElementNode.phantom) {
617
+ let isInserted = false;
618
+ oldVelesElementNode.childComponents.forEach(
619
+ (childComponentofPhantom) => {
620
+ if ("executedVelesNode" in childComponentofPhantom) {
621
+ if (!isInserted) {
622
+ childComponentofPhantom.html.before(newVelesElementNode.html);
623
+ isInserted = true;
624
+ }
625
+ childComponentofPhantom.html.remove();
626
+ } else {
627
+ const velesElementNode = getExecutedComponentVelesNode(
628
+ childComponentofPhantom
629
+ );
630
+ if (!velesElementNode) {
631
+ console.error("can't find HTML tree in a component chain");
632
+ } else {
633
+ if (!isInserted) {
634
+ velesElementNode.html.before(newVelesElementNode.html);
635
+ isInserted = true;
432
636
  }
637
+ velesElementNode.html.remove();
433
638
  }
434
- );
435
- } else {
436
- parentVelesElement.html.replaceChild(
437
- newVelesElementNode.html,
438
- oldVelesElementNode.html
439
- );
639
+ }
440
640
  }
441
- }
442
- parentVelesElement.childComponents = parentVelesElement.childComponents.map(
443
- (childComponent) => childComponent === node ? newNode : node
444
641
  );
445
- node._privateMethods._callUnmountHandlers();
446
- callMountHandlers(newNode);
447
- newNode._privateMethods._addUnmountHandler(() => {
448
- trackingSelectorElements = trackingSelectorElements.filter(
449
- (el) => el !== newTrackingSelectorElement
450
- );
451
- });
452
642
  } else {
453
- console.log("parent node was not found");
643
+ try {
644
+ parentVelesElementRendered.html.replaceChild(
645
+ newVelesElementNode.html,
646
+ oldVelesElementNode.html
647
+ );
648
+ } catch (e) {
649
+ console.error("failed to update...");
650
+ console.log(document.body.innerHTML);
651
+ console.log(oldVelesElementNode.parentVelesElement.html.innerHTML);
652
+ console.log(
653
+ //@ts-expect-error
654
+ oldVelesElementNode.parentVelesElement.childComponents[0].html.textContent
655
+ );
656
+ }
454
657
  }
455
- newTrackingSelectorElements.push(newTrackingSelectorElement);
456
- });
457
- trackingSelectorElements = unique(
458
- trackingSelectorElements.concat(newTrackingSelectorElements)
658
+ }
659
+ parentVelesElementRendered.childComponents = parentVelesElementRendered.childComponents.map(
660
+ (childComponent) => childComponent === node.executedVersion ? newRenderedNode : childComponent
459
661
  );
460
- trackingAttributes.forEach(({ cb, htmlElement, attributeName }) => {
461
- const newAttributeValue = cb ? cb(value) : value;
462
- htmlElement.setAttribute(attributeName, newAttributeValue);
662
+ if (parentVelesElement == null ? void 0 : parentVelesElement.childComponents) {
663
+ parentVelesElement.childComponents = parentVelesElement.childComponents.map(
664
+ (childComponent) => childComponent === node ? newNode : childComponent
665
+ );
666
+ }
667
+ callUnmountHandlers(node.executedVersion);
668
+ addUseValueMountHandler({
669
+ usedValue: value,
670
+ trackers,
671
+ getValue,
672
+ trackingSelectorElement: newTrackingSelectorElement
463
673
  });
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;
674
+ callMountHandlers(newRenderedNode);
675
+ newNode._privateMethods._addUnmountHandler(() => {
676
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
677
+ (el) => el !== newTrackingSelectorElement
678
+ );
472
679
  });
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;
680
+ } else {
681
+ console.log("parent node was not found");
682
+ }
683
+ newTrackingSelectorElements.push(newTrackingSelectorElement);
684
+ }
685
+ function addUseValueMountHandler({
686
+ usedValue,
687
+ getValue,
688
+ trackers,
689
+ trackingSelectorElement
690
+ }) {
691
+ trackingSelectorElement.node._privateMethods._addMountHandler(() => {
692
+ const currentValue = getValue();
693
+ if (usedValue === currentValue) {
694
+ trackers.trackingSelectorElements.push(trackingSelectorElement);
695
+ trackingSelectorElement.node._privateMethods._addUnmountHandler(() => {
696
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
697
+ (el) => trackingSelectorElement !== el
698
+ );
699
+ });
700
+ } else {
701
+ const newTrackingSelectorElements = [];
702
+ updateUseValueSelector({
703
+ value: getValue(),
704
+ trackers,
705
+ selectorTrackingElement: trackingSelectorElement,
706
+ newTrackingSelectorElements,
707
+ getValue
708
+ });
709
+ if (newTrackingSelectorElements[0]) {
710
+ const newTrackingSelectorElement = newTrackingSelectorElements[0];
711
+ if (newTrackingSelectorElement.node === trackingSelectorElement.node) {
712
+ trackers.trackingSelectorElements.push(newTrackingSelectorElement);
713
+ newTrackingSelectorElement.node._privateMethods._addUnmountHandler(
714
+ () => {
715
+ trackers.trackingSelectorElements = trackers.trackingSelectorElements.filter(
716
+ (el) => trackingSelectorElement !== el
717
+ );
718
+ }
719
+ );
720
+ } else {
721
+ }
722
+ } else {
485
723
  }
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");
724
+ }
725
+ });
726
+ }
727
+
728
+ // src/create-state/update-useattribute-value.ts
729
+ function updateUseAttributeValue({
730
+ element,
731
+ value
732
+ }) {
733
+ const { cb, htmlElement, attributeName, attributeValue } = element;
734
+ const newAttributeValue = cb ? cb(value) : value;
735
+ if (typeof newAttributeValue === "boolean") {
736
+ if (newAttributeValue) {
737
+ htmlElement.setAttribute(attributeName, "");
738
+ } else {
739
+ htmlElement.removeAttribute(attributeName);
740
+ }
741
+ } else if (attributeName.startsWith("on")) {
742
+ if (attributeValue === newAttributeValue) {
743
+ return;
744
+ }
745
+ const eventName = attributeName[2].toLocaleLowerCase() + attributeName.slice(3);
746
+ if (attributeValue) {
747
+ htmlElement.removeEventListener(eventName, attributeValue);
748
+ }
749
+ if (newAttributeValue && typeof newAttributeValue === "function") {
750
+ htmlElement.addEventListener(eventName, newAttributeValue);
751
+ }
752
+ element.attributeValue = newAttributeValue;
753
+ } else {
754
+ htmlElement.setAttribute(attributeName, newAttributeValue);
755
+ }
756
+ }
757
+
758
+ // src/create-state/update-usevalueiterator-value.ts
759
+ function updateUseValueIteratorValue({
760
+ value,
761
+ trackingIterator,
762
+ createState: createState2
763
+ }) {
764
+ const {
765
+ cb,
766
+ key,
767
+ renderedElements,
768
+ elementsByKey,
769
+ wrapperComponent,
770
+ selector,
771
+ savedContext
772
+ } = trackingIterator;
773
+ if (!wrapperComponent) {
774
+ console.error("there is no wrapper component for the iterator");
775
+ return;
776
+ }
777
+ if (!wrapperComponent.executedVersion) {
778
+ console.error("it seems the wrapper component was not mounted");
779
+ return;
780
+ }
781
+ const wrapperVelesElementNode = getExecutedComponentVelesNode(
782
+ wrapperComponent.executedVersion
783
+ );
784
+ const parentVelesElement = wrapperVelesElementNode.parentVelesElement;
785
+ if (!parentVelesElement) {
786
+ console.error("there is no parent Veles node for the iterator wrapper");
787
+ return;
788
+ }
789
+ const elements = selector ? selector(value) : value;
790
+ if (Array.isArray(elements)) {
791
+ const newRenderedElements = [];
792
+ const newElementsByKey = {};
793
+ const renderedExistingElements = {};
794
+ elements.forEach((element, index) => {
795
+ let calculatedKey = "";
796
+ if (typeof key === "string" && typeof element === "object" && element !== null && key in element) {
797
+ calculatedKey = element[key];
798
+ } else if (typeof key === "function") {
799
+ calculatedKey = key({ element, index });
800
+ } else {
801
+ }
802
+ if (!calculatedKey) {
490
803
  return;
491
804
  }
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
- };
805
+ const existingElement = elementsByKey[calculatedKey];
806
+ if (existingElement) {
807
+ renderedExistingElements[calculatedKey] = true;
808
+ const currentValue = existingElement.elementState.getValue();
809
+ if (currentValue !== element) {
810
+ existingElement.elementState.setValue(element);
811
+ }
812
+ const currentIndex = existingElement.indexState.getValue();
813
+ if (currentIndex !== index) {
814
+ existingElement.indexState.setValue(index);
815
+ }
816
+ newRenderedElements.push([
817
+ existingElement.node,
818
+ calculatedKey,
819
+ existingElement.elementState
820
+ ]);
821
+ newElementsByKey[calculatedKey] = {
822
+ elementState: existingElement.elementState,
823
+ indexState: existingElement.indexState,
824
+ indexValue: index,
825
+ node: existingElement.node
826
+ };
827
+ } else {
828
+ const elementState = createState2(element);
829
+ const indexState = createState2(index);
830
+ addPublicContext(savedContext);
831
+ const node = cb({ elementState, indexState });
832
+ const renderedNode = renderTree(node);
833
+ node.executedVersion = renderedNode;
834
+ popPublicContext();
835
+ newRenderedElements.push([node, calculatedKey, elementState]);
836
+ newElementsByKey[calculatedKey] = {
837
+ elementState,
838
+ indexState,
839
+ indexValue: index,
840
+ node
841
+ };
842
+ }
843
+ });
844
+ const newChildRenderedComponents = [];
845
+ const newChildComponents = [];
846
+ const positioningOffset = {};
847
+ let newElementsCount = 0;
848
+ let offset = 0;
849
+ let currentElement = null;
850
+ newRenderedElements.forEach((newRenderedElement, index) => {
851
+ var _a, _b, _c;
852
+ newChildRenderedComponents.push(newRenderedElement[0].executedVersion);
853
+ newChildComponents.push(newRenderedElement[0]);
854
+ if (positioningOffset[index]) {
855
+ offset = offset + positioningOffset[index];
856
+ }
857
+ const [newNode, calculatedKey, newState] = newRenderedElement;
858
+ const existingElement = elementsByKey[calculatedKey];
859
+ if (existingElement) {
860
+ const existingElementNode = getExecutedComponentVelesNode(
861
+ existingElement.node.executedVersion
862
+ );
863
+ if (existingElement.indexValue + offset === index) {
864
+ currentElement = existingElementNode.html;
865
+ return;
866
+ }
867
+ if (existingElement.indexValue + offset > index) {
868
+ if (currentElement) {
869
+ currentElement.after(existingElementNode.html);
870
+ positioningOffset[existingElement.indexValue + 1] = -1;
530
871
  } 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;
872
+ const firstRenderedElement = (_a = renderedElements[0]) == null ? void 0 : _a[0];
873
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
874
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
875
+ firstRenderedElement.executedVersion
876
+ );
877
+ firstRenderedVelesNode.html.before(existingElementNode.html);
574
878
  } 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
879
  }
880
+ }
881
+ currentElement = existingElementNode.html;
882
+ offset = offset + 1;
883
+ } else {
884
+ if (currentElement) {
885
+ currentElement.after(existingElementNode.html);
886
+ positioningOffset[existingElement.indexValue + 1] = 1;
589
887
  } else {
590
- const { velesElementNode: newNodeVelesElement } = getComponentVelesNode(newNode);
591
- newNodeVelesElement.parentVelesElement = parentVelesElement;
592
- if (currentElement) {
593
- currentElement.after(newNodeVelesElement.html);
888
+ const firstRenderedElement = (_b = renderedElements[0]) == null ? void 0 : _b[0];
889
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
890
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
891
+ firstRenderedElement.executedVersion
892
+ );
893
+ firstRenderedVelesNode.html.before(existingElementNode.html);
594
894
  } 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
895
  }
603
- offset = offset + 1;
604
- currentElement = newNodeVelesElement.html;
605
- newElementsCount = newElementsCount + 1;
606
- callMountHandlers(newNode);
607
896
  }
608
- });
609
- if (renderedElements.length === newRenderedElements.length + newElementsCount) {
897
+ currentElement = existingElementNode.html;
898
+ offset = offset - 1;
899
+ }
900
+ } else {
901
+ const newNodeVelesElement = getExecutedComponentVelesNode(
902
+ newNode.executedVersion
903
+ );
904
+ newNodeVelesElement.parentVelesElement = parentVelesElement;
905
+ if (currentElement) {
906
+ currentElement.after(newNodeVelesElement.html);
610
907
  } 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
- });
908
+ const firstRenderedElement = (_c = renderedElements[0]) == null ? void 0 : _c[0];
909
+ if (firstRenderedElement == null ? void 0 : firstRenderedElement.executedVersion) {
910
+ const firstRenderedVelesNode = getExecutedComponentVelesNode(
911
+ firstRenderedElement.executedVersion
912
+ );
913
+ firstRenderedVelesNode.html.before(newNodeVelesElement.html);
914
+ } else {
915
+ parentVelesElement.html.prepend(newNodeVelesElement.html);
916
+ }
627
917
  }
628
- trackingIterator.renderedElements = newRenderedElements;
629
- trackingIterator.elementsByKey = newElementsByKey;
918
+ offset = offset + 1;
919
+ currentElement = newNodeVelesElement.html;
920
+ newElementsCount = newElementsCount + 1;
921
+ callMountHandlers(newNode.executedVersion);
630
922
  }
631
923
  });
924
+ if (renderedElements.length === newRenderedElements.length + newElementsCount) {
925
+ } else {
926
+ renderedElements.forEach(([oldNode, calculatedKey]) => {
927
+ if (renderedExistingElements[calculatedKey] === true) {
928
+ return;
929
+ } else {
930
+ const oldRenderedVelesNode = getExecutedComponentVelesNode(
931
+ oldNode.executedVersion
932
+ );
933
+ oldRenderedVelesNode.html.remove();
934
+ callUnmountHandlers(oldNode.executedVersion);
935
+ if ("executedVelesNode" in wrapperVelesElementNode) {
936
+ wrapperVelesElementNode.childComponents = wrapperVelesElementNode.childComponents.filter(
937
+ (childComponent) => childComponent !== oldNode.executedVersion
938
+ );
939
+ } else {
940
+ throw new Error("Wrapper iterator element is a string");
941
+ }
942
+ if ("velesNode" in wrapperComponent) {
943
+ wrapperComponent.childComponents = wrapperComponent.childComponents.filter(
944
+ (childComponent) => childComponent !== oldNode
945
+ );
946
+ }
947
+ }
948
+ });
949
+ }
950
+ if ("executedVelesNode" in wrapperVelesElementNode) {
951
+ wrapperVelesElementNode.childComponents = newChildRenderedComponents;
952
+ }
953
+ if ("velesNode" in wrapperComponent) {
954
+ wrapperComponent.childComponents = newChildComponents;
955
+ }
956
+ trackingIterator.renderedElements = newRenderedElements;
957
+ trackingIterator.elementsByKey = newElementsByKey;
958
+ }
959
+ }
960
+
961
+ // src/create-state/trigger-updates.ts
962
+ function triggerUpdates({
963
+ value,
964
+ createState: createState2,
965
+ trackers,
966
+ getValue
967
+ }) {
968
+ const newTrackingSelectorElements = [];
969
+ trackers.trackingSelectorElements.forEach(
970
+ (selectorTrackingElement) => updateUseValueSelector({
971
+ value,
972
+ selectorTrackingElement,
973
+ newTrackingSelectorElements,
974
+ trackers,
975
+ getValue
976
+ })
977
+ );
978
+ trackers.trackingSelectorElements = unique(
979
+ trackers.trackingSelectorElements.concat(newTrackingSelectorElements)
980
+ );
981
+ trackers.trackingAttributes.forEach((element) => {
982
+ updateUseAttributeValue({ element, value });
983
+ });
984
+ trackers.trackingEffects.forEach((trackingEffect) => {
985
+ const { cb, selectedValue, selector, comparator } = trackingEffect;
986
+ const newSelectedValue = selector ? selector(value) : value;
987
+ if (comparator ? comparator(selectedValue, newSelectedValue) : selectedValue === newSelectedValue) {
988
+ return;
989
+ }
990
+ cb(newSelectedValue);
991
+ trackingEffect.selectedValue = newSelectedValue;
992
+ });
993
+ trackers.trackingIterators.forEach((trackingIterator) => {
994
+ updateUseValueIteratorValue({ value, trackingIterator, createState: createState2 });
995
+ });
996
+ }
997
+
998
+ // src/create-state/index.ts
999
+ function createState(initialValue, subscribeCallback) {
1000
+ let value = initialValue;
1001
+ let previousValue = void 0;
1002
+ const trackers = {
1003
+ trackingEffects: [],
1004
+ trackingSelectorElements: [],
1005
+ trackingAttributes: [],
1006
+ trackingIterators: []
632
1007
  };
633
1008
  const result = {
634
1009
  // supposed to be used at the component level
@@ -637,7 +1012,7 @@ function createState(initialValue, subscribeCallback) {
637
1012
  },
638
1013
  trackValueSelector(selector, cb, options = {}) {
639
1014
  const trackedValue = selector ? selector(value) : value;
640
- trackingEffects.push({
1015
+ trackers.trackingEffects.push({
641
1016
  cb,
642
1017
  selector,
643
1018
  comparator: options.comparator,
@@ -653,7 +1028,7 @@ function createState(initialValue, subscribeCallback) {
653
1028
  }
654
1029
  }
655
1030
  onUnmount(() => {
656
- trackingEffects = trackingEffects.filter(
1031
+ trackers.trackingEffects = trackers.trackingEffects.filter(
657
1032
  (trackingCallback) => trackingCallback.cb !== cb
658
1033
  );
659
1034
  });
@@ -663,70 +1038,79 @@ function createState(initialValue, subscribeCallback) {
663
1038
  },
664
1039
  useValueSelector(selector, cb, comparator = identity) {
665
1040
  const selectedValue = selector ? selector(value) : value;
666
- const returnedNode = cb ? cb(selectedValue) : String(selectedValue);
1041
+ const returnedNode = cb ? cb(selectedValue) : selectedValue == void 0 ? "" : String(selectedValue);
667
1042
  const node = !returnedNode || typeof returnedNode === "string" ? createTextElement(returnedNode) : returnedNode;
1043
+ const currentContext2 = getCurrentContext();
1044
+ node.needExecutedVersion = true;
668
1045
  const trackingSelectorElement = {
669
1046
  selector,
670
1047
  selectedValue,
671
1048
  cb,
672
1049
  node,
673
- comparator
1050
+ comparator,
1051
+ savedContext: currentContext2
674
1052
  };
675
- trackingSelectorElements.push(trackingSelectorElement);
676
- node._privateMethods._addUnmountHandler(() => {
677
- trackingSelectorElements = trackingSelectorElements.filter(
678
- (el) => trackingSelectorElement !== el
679
- );
1053
+ addUseValueMountHandler({
1054
+ usedValue: value,
1055
+ getValue: () => value,
1056
+ trackers,
1057
+ trackingSelectorElement
680
1058
  });
681
1059
  return node;
682
1060
  },
683
1061
  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
- });
1062
+ const currentContext2 = getCurrentContext();
713
1063
  const trackingParams = {};
714
- trackingIterators.push(trackingParams);
715
- const wrapperComponent = createElement(() => {
716
- onUnmount(() => {
717
- trackingIterators = trackingIterators.filter(
718
- (currentTrackingParams) => currentTrackingParams !== trackingParams
719
- );
1064
+ trackingParams.savedContext = currentContext2;
1065
+ const wrapperComponent = createElement((_props, componentAPI) => {
1066
+ const children = [];
1067
+ const elementsByKey = {};
1068
+ const elements = options.selector ? options.selector(value) : value;
1069
+ if (!Array.isArray(elements)) {
1070
+ console.error("useValueIterator received non-array value");
1071
+ return null;
1072
+ }
1073
+ elements.forEach((element, index) => {
1074
+ let calculatedKey = "";
1075
+ if (typeof options.key === "string" && typeof element === "object" && element !== null && options.key in element) {
1076
+ calculatedKey = element[options.key];
1077
+ } else if (typeof options.key === "function") {
1078
+ calculatedKey = options.key({ element, index });
1079
+ } else {
1080
+ }
1081
+ const elementState = createState(element);
1082
+ const indexState = createState(index);
1083
+ if (!calculatedKey) {
1084
+ return;
1085
+ }
1086
+ let node = cb({ elementState, indexState });
1087
+ node.needExecutedVersion = true;
1088
+ elementsByKey[calculatedKey] = {
1089
+ node,
1090
+ indexState,
1091
+ indexValue: index,
1092
+ elementState
1093
+ };
1094
+ children.push([node, calculatedKey, elementState]);
1095
+ });
1096
+ trackingParams.elementsByKey = elementsByKey;
1097
+ trackingParams.renderedElements = children;
1098
+ trackers.trackingIterators.push(trackingParams);
1099
+ onMount(() => {
1100
+ componentAPI.onUnmount(() => {
1101
+ trackers.trackingIterators = trackers.trackingIterators.filter(
1102
+ (currentTrackingParams) => currentTrackingParams !== trackingParams
1103
+ );
1104
+ });
720
1105
  });
721
1106
  return createElement("div", {
722
1107
  phantom: true,
723
1108
  children: children.map((child) => child[0])
724
1109
  });
725
1110
  });
1111
+ wrapperComponent.needExecutedVersion = true;
726
1112
  trackingParams.cb = cb;
727
1113
  trackingParams.key = options.key;
728
- trackingParams.elementsByKey = elementsByKey;
729
- trackingParams.renderedElements = children;
730
1114
  trackingParams.wrapperComponent = wrapperComponent;
731
1115
  if (options.selector) {
732
1116
  trackingParams.selector = options.selector;
@@ -734,14 +1118,30 @@ function createState(initialValue, subscribeCallback) {
734
1118
  return wrapperComponent;
735
1119
  },
736
1120
  useAttribute: (cb) => {
1121
+ const originalValue = value;
1122
+ let wasMounted = false;
737
1123
  const attributeValue = cb ? cb(value) : value;
738
1124
  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
- );
1125
+ const trackingElement = {
1126
+ cb,
1127
+ htmlElement,
1128
+ attributeName,
1129
+ attributeValue
1130
+ };
1131
+ node._privateMethods._addMountHandler(() => {
1132
+ trackers.trackingAttributes.push(trackingElement);
1133
+ if (!wasMounted && value === originalValue) {
1134
+ } else {
1135
+ updateUseAttributeValue({ element: trackingElement, value });
1136
+ }
1137
+ if (!wasMounted) {
1138
+ wasMounted = true;
1139
+ }
1140
+ node._privateMethods._addUnmountHandler(() => {
1141
+ trackers.trackingAttributes = trackers.trackingAttributes.filter(
1142
+ (trackingAttribute) => trackingAttribute !== trackingElement
1143
+ );
1144
+ });
745
1145
  });
746
1146
  return attributeValue;
747
1147
  };
@@ -766,7 +1166,7 @@ function createState(initialValue, subscribeCallback) {
766
1166
  if (newValue !== value) {
767
1167
  previousValue = value;
768
1168
  value = newValue;
769
- _triggerUpdates();
1169
+ triggerUpdates({ value, createState, trackers, getValue: () => value });
770
1170
  }
771
1171
  }
772
1172
  };