j-templates 5.0.47 → 5.0.49

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.
package/jTemplates.js CHANGED
@@ -88,7 +88,16 @@ exports.DOMNodeConfig = {
88
88
  var cEvent = new CustomEvent(event, data);
89
89
  target.dispatchEvent(cEvent);
90
90
  },
91
- setProperties: utils_1.SetProperties
91
+ setProperties: utils_1.SetProperties,
92
+ getFirstChild(target) {
93
+ return target.firstChild;
94
+ },
95
+ getNextSibling(target) {
96
+ return target.nextSibling;
97
+ },
98
+ replaceChildren(target, children) {
99
+ target.replaceChildren(...children);
100
+ },
92
101
  };
93
102
 
94
103
 
@@ -542,6 +551,7 @@ const component_1 = __webpack_require__(/*! ./component */ "./src/Node/component
542
551
  const injector_1 = __webpack_require__(/*! ../Utils/injector */ "./src/Utils/injector.ts");
543
552
  const decorators_1 = __webpack_require__(/*! ../Utils/decorators */ "./src/Utils/decorators.ts");
544
553
  const thread_1 = __webpack_require__(/*! ../Utils/thread */ "./src/Utils/thread.ts");
554
+ const list_1 = __webpack_require__(/*! ../Utils/list */ "./src/Utils/list.ts");
545
555
  var ComponentNode;
546
556
  (function (ComponentNode) {
547
557
  function Fire(event, data) {
@@ -589,7 +599,7 @@ function AddPreReqTemplate(node) {
589
599
  (0, thread_1.Schedule)(function () {
590
600
  if (node.destroyed)
591
601
  return;
592
- nodeRef_1.NodeRef.InitAll(preNodes);
602
+ nodeRef_1.NodeRef.InitAll(node, preNodes);
593
603
  });
594
604
  (0, thread_1.Thread)(function () {
595
605
  if (node.destroyed)
@@ -625,20 +635,24 @@ function AddTemplate(node, init) {
625
635
  return;
626
636
  const nodes = injector_1.Injector.Scope(node.injector, InvokeNodeTemplate, node);
627
637
  (0, thread_1.Schedule)(function () {
628
- nodeRef_1.NodeRef.InitAll(nodes);
638
+ nodeRef_1.NodeRef.InitAll(node, nodes);
629
639
  });
630
640
  (0, thread_1.Thread)(function () {
631
641
  if (node.destroyed)
632
642
  return;
643
+ const list = list_1.List.Create();
644
+ list_1.List.Add(list, {
645
+ value: undefined,
646
+ init: true,
647
+ nodes
648
+ });
633
649
  if (init)
634
- for (var x = 0; x < nodes.length; x++)
635
- nodeRef_1.NodeRef.AddChild(node, nodes[x]);
650
+ nodeRef_1.NodeRef.ReconcileChildren(node, list);
636
651
  else
637
652
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
638
653
  if (node.destroyed)
639
654
  return;
640
- for (var x = 0; x < nodes.length; x++)
641
- nodeRef_1.NodeRef.AddChild(node, nodes[x]);
655
+ nodeRef_1.NodeRef.ReconcileChildren(node, list);
642
656
  });
643
657
  });
644
658
  if (node.component.Bound !== component_1.Component.prototype.Bound)
@@ -660,13 +674,13 @@ function AddTemplate(node, init) {
660
674
 
661
675
  Object.defineProperty(exports, "__esModule", ({ value: true }));
662
676
  exports.ElementNode = void 0;
663
- const boundNode_1 = __webpack_require__(/*! ./boundNode */ "./src/Node/boundNode.ts");
664
- const nodeConfig_1 = __webpack_require__(/*! ./nodeConfig */ "./src/Node/nodeConfig.ts");
677
+ const observableScope_1 = __webpack_require__(/*! ../Store/Tree/observableScope */ "./src/Store/Tree/observableScope.ts");
665
678
  const injector_1 = __webpack_require__(/*! ../Utils/injector */ "./src/Utils/injector.ts");
666
679
  const list_1 = __webpack_require__(/*! ../Utils/list */ "./src/Utils/list.ts");
667
680
  const thread_1 = __webpack_require__(/*! ../Utils/thread */ "./src/Utils/thread.ts");
681
+ const boundNode_1 = __webpack_require__(/*! ./boundNode */ "./src/Node/boundNode.ts");
682
+ const nodeConfig_1 = __webpack_require__(/*! ./nodeConfig */ "./src/Node/nodeConfig.ts");
668
683
  const nodeRef_1 = __webpack_require__(/*! ./nodeRef */ "./src/Node/nodeRef.ts");
669
- const observableScope_1 = __webpack_require__(/*! ../Store/Tree/observableScope */ "./src/Store/Tree/observableScope.ts");
670
684
  var ElementNode;
671
685
  (function (ElementNode) {
672
686
  function Create(type, namespace, nodeDef, children) {
@@ -722,79 +736,97 @@ function SetDefaultData(node) {
722
736
  (0, thread_1.Synch)(function () {
723
737
  const nodes = injector_1.Injector.Scope(node.injector, CreateNodeArray, node.childrenFunc, true);
724
738
  if (nodes.length > 0) {
725
- nodeRef_1.NodeRef.InitAll(nodes);
739
+ nodeRef_1.NodeRef.InitAll(node, nodes);
726
740
  (0, thread_1.Thread)(function () {
727
741
  if (node.destroyed)
728
742
  return;
729
- DetachAndAddNodes(node, [], [nodes]);
743
+ const defaultNodeList = list_1.List.Create();
744
+ list_1.List.Add(defaultNodeList, {
745
+ value: null,
746
+ init: true,
747
+ nodes
748
+ });
749
+ nodeRef_1.NodeRef.ReconcileChildren(node, defaultNodeList);
730
750
  });
731
751
  }
732
752
  });
733
753
  }
734
- function SetData(node, values, init = false) {
735
- (0, thread_1.Synch)(function () {
736
- const newNodesMap = new Map();
737
- const newNodesArrays = new Array(values.length);
738
- for (let x = 0; x < values.length; x++) {
739
- const value = values[x];
740
- let nodes;
741
- if (node.nodesMap) {
742
- let nodeArrayList = node.nodesMap.get(value);
743
- nodes = nodeArrayList && list_1.List.Remove(nodeArrayList);
744
- }
745
- let newNodeArrayList = newNodesMap.get(value);
746
- if (!newNodeArrayList) {
747
- newNodeArrayList = list_1.List.Create();
748
- newNodesMap.set(value, newNodeArrayList);
749
- }
750
- if (!nodes) {
751
- nodes = injector_1.Injector.Scope(node.injector, CreateNodeArray, node.childrenFunc, value);
752
- (0, thread_1.Schedule)(function () {
753
- if (node.destroyed || newNodesMap.size === 0)
754
- return;
755
- nodeRef_1.NodeRef.InitAll(nodes);
756
- list_1.List.Push(newNodeArrayList, nodes);
757
- });
754
+ function GetDataValue(data) {
755
+ return data.value;
756
+ }
757
+ function ReconcileNodeData(node, values) {
758
+ const nextNodeList = list_1.List.Create();
759
+ const initNodeList = list_1.List.Create();
760
+ const currentNodeList = node.nodeList;
761
+ const nodeMap = currentNodeList && list_1.List.ToNodeMap(currentNodeList, GetDataValue);
762
+ for (let x = 0; x < values.length; x++) {
763
+ let curNode;
764
+ if (nodeMap) {
765
+ const nodeArr = nodeMap.get(values[x]);
766
+ if (nodeArr) {
767
+ let y = nodeArr.length - 1;
768
+ for (; y >= 0 && !curNode; y--) {
769
+ curNode = nodeArr[y];
770
+ nodeArr[y] = null;
771
+ }
758
772
  }
759
- else
760
- list_1.List.Push(newNodeArrayList, nodes);
761
- newNodesArrays[x] = nodes;
762
773
  }
763
- var detachNodes = [];
764
- if (node.nodesMap) {
765
- for (var nodeArrayList of node.nodesMap.values())
766
- nodeArrayList.size > 0 && detachNodes.push(DestroyNodeArrayList(nodeArrayList));
767
- node.nodesMap.clear();
774
+ if (curNode) {
775
+ list_1.List.RemoveNode(currentNodeList, curNode);
776
+ list_1.List.AddNode(nextNodeList, curNode);
777
+ if (!curNode.data.init)
778
+ list_1.List.Add(initNodeList, curNode.data);
779
+ }
780
+ else {
781
+ curNode = list_1.List.Add(nextNodeList, {
782
+ value: values[x],
783
+ init: false,
784
+ nodes: injector_1.Injector.Scope(node.injector, CreateNodeArray, node.childrenFunc, values[x])
785
+ });
786
+ list_1.List.Add(initNodeList, curNode.data);
787
+ }
788
+ }
789
+ let curNode = initNodeList.head;
790
+ while (curNode) {
791
+ const data = curNode.data;
792
+ (0, thread_1.Schedule)(function () {
793
+ if (node.destroyed || nextNodeList.size === 0)
794
+ return;
795
+ nodeRef_1.NodeRef.InitAll(node, data.nodes);
796
+ data.init = true;
797
+ });
798
+ curNode = curNode.next;
799
+ }
800
+ if (currentNodeList) {
801
+ let curDetach = currentNodeList.head;
802
+ while (curDetach) {
803
+ nodeRef_1.NodeRef.DestroyAll(curDetach.data.nodes);
804
+ for (let x = 0; x < curDetach.data.nodes.length; x++)
805
+ node.childNodes.delete(curDetach.data.nodes[x]);
806
+ curDetach = curDetach.next;
768
807
  }
769
- node.nodesMap = newNodesMap;
808
+ list_1.List.Clear(currentNodeList);
809
+ }
810
+ node.nodeList = nextNodeList;
811
+ }
812
+ function SetData(node, values, init = false) {
813
+ (0, thread_1.Synch)(function () {
814
+ ReconcileNodeData(node, values);
815
+ const attachNodes = node.nodeList;
770
816
  (0, thread_1.Thread)(function () {
771
817
  if (node.destroyed)
772
818
  return;
773
819
  if (init)
774
- DetachAndAddNodes(node, detachNodes, newNodesMap.size > 0 && newNodesArrays);
820
+ nodeRef_1.NodeRef.ReconcileChildren(node, attachNodes);
775
821
  else
776
822
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
777
- if (node.destroyed)
823
+ if (node.destroyed || attachNodes.size < node.childNodes.size)
778
824
  return;
779
- DetachAndAddNodes(node, detachNodes, newNodesMap.size > 0 && newNodesArrays);
825
+ nodeRef_1.NodeRef.ReconcileChildren(node, attachNodes);
780
826
  });
781
827
  });
782
828
  });
783
829
  }
784
- function DetachAndAddNodes(node, detachNodes, newNodes) {
785
- for (var x = 0; x < detachNodes.length; x++)
786
- list_1.List.ForEach(detachNodes[x], function (nodes) {
787
- for (var x = 0; x < nodes.length; x++)
788
- nodeRef_1.NodeRef.DetachChild(node, nodes[x]);
789
- });
790
- var previousNode = null;
791
- for (var x = 0; newNodes && x < newNodes.length; x++) {
792
- for (var y = 0; y < newNodes[x].length; y++) {
793
- nodeRef_1.NodeRef.AddChildAfter(node, previousNode, newNodes[x][y]);
794
- previousNode = newNodes[x][y];
795
- }
796
- }
797
- }
798
830
  function CreateNodeArray(childrenFunc, value) {
799
831
  var newNodes = childrenFunc(value);
800
832
  if (typeof newNodes === "string") {
@@ -810,10 +842,6 @@ function CreateNodeArray(childrenFunc, value) {
810
842
  return newNodes;
811
843
  return [newNodes];
812
844
  }
813
- function DestroyNodeArrayList(nodeArrayList) {
814
- list_1.List.ForEach(nodeArrayList, nodeRef_1.NodeRef.DestroyAll);
815
- return nodeArrayList;
816
- }
817
845
 
818
846
 
819
847
  /***/ }),
@@ -911,7 +939,7 @@ var NodeRef;
911
939
  setAttributes: false,
912
940
  setEvents: false,
913
941
  childrenFunc: null,
914
- nodesMap: null,
942
+ nodeList: null,
915
943
  setData: false
916
944
  };
917
945
  case NodeRefType.ComponentNode:
@@ -954,9 +982,12 @@ var NodeRef;
954
982
  }
955
983
  }
956
984
  NodeRef.Init = Init;
957
- function InitAll(nodeRefs) {
958
- for (var x = 0; x < nodeRefs.length; x++)
985
+ function InitAll(parentNode, nodeRefs) {
986
+ for (var x = 0; x < nodeRefs.length; x++) {
987
+ nodeRefs[x].parent = parentNode;
988
+ parentNode.childNodes.add(nodeRefs[x]);
959
989
  Init(nodeRefs[x]);
990
+ }
960
991
  }
961
992
  NodeRef.InitAll = InitAll;
962
993
  function AddChild(node, child) {
@@ -966,16 +997,42 @@ var NodeRef;
966
997
  }
967
998
  NodeRef.AddChild = AddChild;
968
999
  function AddChildAfter(node, currentChild, newChild) {
969
- if (currentChild && !node.childNodes.has(currentChild))
1000
+ if (currentChild && currentChild.parent !== node)
970
1001
  throw "currentChild is not valid";
971
1002
  newChild.parent = node;
972
1003
  node.childNodes.add(newChild);
973
1004
  nodeConfig_1.NodeConfig.addChildAfter(node.node, currentChild && currentChild.node, newChild.node);
974
1005
  }
975
1006
  NodeRef.AddChildAfter = AddChildAfter;
1007
+ function ReconcileChildren(node, nextChildren) {
1008
+ const rootNode = node.node;
1009
+ if (nextChildren.size === 0) {
1010
+ nodeConfig_1.NodeConfig.replaceChildren(rootNode, []);
1011
+ return;
1012
+ }
1013
+ let priorNode;
1014
+ let curDataNode = nextChildren?.head;
1015
+ while (curDataNode) {
1016
+ for (let x = 0; x < curDataNode.data.nodes.length; x++) {
1017
+ const actualNode = priorNode ? nodeConfig_1.NodeConfig.getNextSibling(priorNode) : nodeConfig_1.NodeConfig.getFirstChild(rootNode);
1018
+ const virtualNode = curDataNode.data.nodes[x];
1019
+ const expectedNode = virtualNode.node;
1020
+ if (actualNode !== expectedNode) {
1021
+ nodeConfig_1.NodeConfig.addChildBefore(rootNode, actualNode, expectedNode);
1022
+ }
1023
+ priorNode = expectedNode;
1024
+ }
1025
+ curDataNode = curDataNode.next;
1026
+ }
1027
+ let remainingSibling = priorNode && nodeConfig_1.NodeConfig.getNextSibling(priorNode);
1028
+ while (remainingSibling) {
1029
+ nodeConfig_1.NodeConfig.removeChild(rootNode, remainingSibling);
1030
+ remainingSibling = nodeConfig_1.NodeConfig.getNextSibling(priorNode);
1031
+ }
1032
+ }
1033
+ NodeRef.ReconcileChildren = ReconcileChildren;
976
1034
  function DetachChild(node, child) {
977
- if (node.childNodes.has(child)) {
978
- node.childNodes.delete(child);
1035
+ if (node.childNodes.delete(child)) {
979
1036
  nodeConfig_1.NodeConfig.removeChild(node.node, child.node);
980
1037
  child.parent = null;
981
1038
  }
@@ -985,7 +1042,7 @@ var NodeRef;
985
1042
  if (node.destroyed)
986
1043
  return;
987
1044
  node.destroyed = true;
988
- node.childNodes.forEach(Destroy);
1045
+ node.childNodes?.forEach(Destroy);
989
1046
  for (let x = 0; x < node.destroyables.length; x++)
990
1047
  node.destroyables[x]?.Destroy();
991
1048
  }
@@ -2089,14 +2146,12 @@ class ObservableTree {
2089
2146
  if (!parentValue)
2090
2147
  throw new Error("Unable to write path: " + path + ". Falsey value found at: " + pathParts.slice(0, x).join("."));
2091
2148
  const prop = pathParts[x];
2092
- const exists = Object.hasOwn(parentValue, prop);
2093
2149
  const oldValue = parentValue[prop];
2094
2150
  const oldType = TypeOf(oldValue);
2095
2151
  parentValue[prop] = value;
2096
- if (oldType !== Type.Value)
2097
- return this.scopeCache.get(oldValue) ||
2098
- this.scopeCache.get(parentValue);
2099
- const leafScopes = exists && this.leafScopeCache.get(parentValue);
2152
+ if (oldType !== Type.Value || Array.isArray(parentValue))
2153
+ return this.scopeCache.get(parentValue) || this.scopeCache.get(oldValue);
2154
+ const leafScopes = this.leafScopeCache.get(parentValue);
2100
2155
  return leafScopes && leafScopes[prop] || this.scopeCache.get(parentValue);
2101
2156
  }
2102
2157
  UpdatePathCache(path, value) {
@@ -2104,6 +2159,9 @@ class ObservableTree {
2104
2159
  if (type === Type.Value)
2105
2160
  return;
2106
2161
  this.pathCache.set(value, path);
2162
+ this.proxyCache.delete(value);
2163
+ this.scopeCache.delete(value);
2164
+ this.leafScopeCache.delete(value);
2107
2165
  const keys = Object.keys(value);
2108
2166
  for (let x = 0; x < keys.length; x++)
2109
2167
  this.UpdatePathCache(`${path}.${keys[x]}`, value[keys[x]]);
@@ -2791,7 +2849,11 @@ var List;
2791
2849
  }
2792
2850
  List.Pop = Pop;
2793
2851
  function Add(list, data) {
2794
- var node = { previous: null, next: null, data: data };
2852
+ const node = { previous: null, next: null, data: data };
2853
+ return AddNode(list, node);
2854
+ }
2855
+ List.Add = Add;
2856
+ function AddNode(list, node) {
2795
2857
  if (list.size === 0) {
2796
2858
  list.head = node;
2797
2859
  list.tail = node;
@@ -2805,7 +2867,7 @@ var List;
2805
2867
  }
2806
2868
  return node;
2807
2869
  }
2808
- List.Add = Add;
2870
+ List.AddNode = AddNode;
2809
2871
  function AddBefore(list, node, data) {
2810
2872
  if (!node)
2811
2873
  return List.Add(list, data);
@@ -2853,6 +2915,25 @@ var List;
2853
2915
  return node.data;
2854
2916
  }
2855
2917
  List.Remove = Remove;
2918
+ function RemoveNode(list, node) {
2919
+ if (list.head === node) {
2920
+ list.head = node.next;
2921
+ }
2922
+ else if (list.tail === node) {
2923
+ list.tail = node.previous;
2924
+ }
2925
+ else {
2926
+ const prev = node.previous;
2927
+ const next = node.next;
2928
+ prev.next = next;
2929
+ next.previous = prev;
2930
+ }
2931
+ node.next = node.previous = null;
2932
+ list.size--;
2933
+ if (list.size > 0)
2934
+ list.head.previous = list.tail.next = null;
2935
+ }
2936
+ List.RemoveNode = RemoveNode;
2856
2937
  function ForEach(list, callback) {
2857
2938
  var node = list.head;
2858
2939
  while (node) {
@@ -2861,6 +2942,21 @@ var List;
2861
2942
  }
2862
2943
  }
2863
2944
  List.ForEach = ForEach;
2945
+ function ToNodeMap(list, keyCallback) {
2946
+ const map = new Map();
2947
+ let node = list.head;
2948
+ while (node) {
2949
+ const key = keyCallback(node.data);
2950
+ const nodes = map.get(key) || [node];
2951
+ if (nodes[0] !== node)
2952
+ nodes.push(node);
2953
+ else
2954
+ map.set(key, nodes);
2955
+ node = node.next;
2956
+ }
2957
+ return map;
2958
+ }
2959
+ List.ToNodeMap = ToNodeMap;
2864
2960
  })(List || (exports.List = List = {}));
2865
2961
 
2866
2962