j-templates 7.0.55 → 7.0.56

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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CreateAssignment = CreateAssignment;
4
+ const DEFAULT_ASSIGNMENT = {};
4
5
  function CreateAssignment(target, createAssignment) {
5
6
  let last;
6
7
  let writeTo = {};
@@ -8,6 +9,7 @@ function CreateAssignment(target, createAssignment) {
8
9
  if (next === last)
9
10
  return;
10
11
  last = next;
12
+ next = !next ? DEFAULT_ASSIGNMENT : next;
11
13
  for (const key in writeTo) {
12
14
  writeTo[key](next);
13
15
  }
@@ -22,6 +22,7 @@ function CreatePropertyAssignment(target, property) {
22
22
  childAssignment(nextValue);
23
23
  }
24
24
  }
25
+ lastValue = nextValue;
25
26
  };
26
27
  }
27
28
  function CreateRootPropertyAssignment(target, property) {
@@ -59,6 +60,7 @@ function CreateRootPropertyAssignment(target, property) {
59
60
  }
60
61
  }
61
62
  }
63
+ lastValue = nextValue;
62
64
  };
63
65
  }
64
66
  function AssignNodeValue(target, value) {
@@ -100,6 +100,9 @@ exports.DOMNodeConfig = {
100
100
  setText(target, text) {
101
101
  target.nodeValue = text;
102
102
  },
103
+ copyText(source, target) {
104
+ target.nodeValue = source.nodeValue;
105
+ },
103
106
  getAttribute(target, attribute) {
104
107
  return target.getAttribute(attribute);
105
108
  },
@@ -133,15 +136,8 @@ exports.DOMNodeConfig = {
133
136
  },
134
137
  reconcileChildren(target, children) {
135
138
  if (!target.firstChild) {
136
- if (children.length > 10) {
137
- const fragment = new DocumentFragment();
138
- for (let x = 0; x < children.length; x++)
139
- fragment.appendChild(children[x]);
140
- target.appendChild(fragment);
141
- }
142
- else
143
- for (let x = 0; x < children.length; x++)
144
- target.appendChild(children[x]);
139
+ for (let x = 0; x < children.length; x++)
140
+ target.appendChild(children[x]);
145
141
  return;
146
142
  }
147
143
  if (children.length === 0) {
@@ -169,14 +165,11 @@ exports.DOMNodeConfig = {
169
165
  }
170
166
  while (target.lastChild !== children[x - 1])
171
167
  target.removeChild(target.lastChild);
172
- if (children.length - x > 10) {
173
- const fragment = new DocumentFragment();
174
- for (; x < children.length; x++)
175
- fragment.appendChild(children[x]);
176
- target.appendChild(fragment);
177
- }
178
- else
179
- for (; x < children.length; x++)
180
- target.appendChild(children[x]);
168
+ for (; x < children.length; x++)
169
+ target.appendChild(children[x]);
170
+ },
171
+ reconcileChild(target, child) {
172
+ if (target.childElementCount > 1 || target.firstChild !== child)
173
+ target.replaceChildren(child);
181
174
  },
182
175
  };
@@ -4,6 +4,7 @@ export interface INodeConfig {
4
4
  scheduleUpdate(callback: () => void): void;
5
5
  wrapPriorityUpdates<P extends any[]>(callback: (...args: P) => void): (...args: P) => void;
6
6
  setText(target: any, text: string): void;
7
+ copyText(source: any, target: any): void;
7
8
  isTextNode(target: any): boolean;
8
9
  getAttribute(target: any, attribute: string): string;
9
10
  setAttribute(target: any, attribute: string, value: string): void;
@@ -38,5 +39,6 @@ export interface INodeConfig {
38
39
  getNextSibling(target: any): any;
39
40
  replaceChildren(target: any, children: any[]): void;
40
41
  reconcileChildren(target: any, children: any[]): void;
42
+ reconcileChild(target: any, child: any): void;
41
43
  }
42
44
  export declare const NodeConfig: INodeConfig;
package/Node/vNode.js CHANGED
@@ -4,7 +4,6 @@ exports.vNode = void 0;
4
4
  const Store_1 = require("../Store");
5
5
  const emitter_1 = require("../Utils/emitter");
6
6
  const injector_1 = require("../Utils/injector");
7
- const list_1 = require("../Utils/list");
8
7
  const thread_1 = require("../Utils/thread");
9
8
  const nodeConfig_1 = require("./nodeConfig");
10
9
  var vNode;
@@ -13,7 +12,11 @@ var vNode;
13
12
  return {
14
13
  definition,
15
14
  type: definition.type,
16
- injector: injector_1.Injector.Current() ?? new injector_1.Injector(),
15
+ injector: definition.componentConstructor
16
+ ? injector_1.Injector.Scope(injector_1.Injector.Current(), function () {
17
+ return new injector_1.Injector();
18
+ })
19
+ : (injector_1.Injector.Current() ?? new injector_1.Injector()),
17
20
  node: definition.node ?? null,
18
21
  children: null,
19
22
  destroyed: false,
@@ -55,7 +58,8 @@ var vNode;
55
58
  vnode.component?.Destroy();
56
59
  Store_1.ObservableScope.DestroyAll(vnode.scopes);
57
60
  vnode.onDestroyed && emitter_1.Emitter.Emit(vnode.onDestroyed);
58
- vnode.children && DestroyAll(vnode.children);
61
+ for (let x = 0; vnode.children && x < vnode.children.length; x++)
62
+ DestroyAll(vnode.children[x][1]);
59
63
  }
60
64
  vNode.Destroy = Destroy;
61
65
  function DestroyAll(vnodes) {
@@ -134,29 +138,14 @@ function InitNode(vnode) {
134
138
  if (componentConstructor) {
135
139
  vnode.component = new componentConstructor(vnode);
136
140
  vnode.component.Bound();
137
- const componentScope = Store_1.ObservableScope.Create(function () {
138
- let nodes = injector_1.Injector.Scope(vnode.injector, function () {
139
- return vnode.component.Template();
140
- });
141
- if (!Array.isArray(nodes))
142
- nodes = [nodes];
143
- return nodes;
144
- });
145
- vnode.scopes.push(componentScope);
146
- Store_1.ObservableScope.Watch(componentScope, CreateScheduledCallback(function () {
147
- if (vnode.destroyed)
148
- return;
149
- const nodes = injector_1.Injector.Scope(vnode.injector, Store_1.ObservableScope.Peek, componentScope);
150
- vNode.DestroyAll(vnode.children);
151
- vnode.children = nodes;
152
- UpdateChildren(vnode);
153
- }));
154
- const nodes = Store_1.ObservableScope.Peek(componentScope);
155
- vnode.children = nodes;
141
+ function componentChildren() {
142
+ return vnode.component.Template();
143
+ }
144
+ Children(vnode, componentChildren, DefaultData);
156
145
  }
157
146
  else if (childrenArray) {
158
- vnode.children = childrenArray;
159
- vNode.InitAll(vnode.children);
147
+ vnode.children = [[undefined, childrenArray]];
148
+ vNode.InitAll(childrenArray);
160
149
  }
161
150
  else if (children) {
162
151
  Children(vnode, children, data);
@@ -175,80 +164,114 @@ function Children(vnode, children, data) {
175
164
  }));
176
165
  AssignChildren(vnode, childrenScope);
177
166
  }
167
+ const DEFAULT_DATA = [undefined];
168
+ function DefaultData() {
169
+ return DEFAULT_DATA;
170
+ }
178
171
  function CreateChildrenScope(vnode, children, data) {
179
- if (data === undefined)
180
- return Store_1.ObservableScope.Create(WrapStaticChildren(vnode, children));
181
- const dataScope = Store_1.ObservableScope.Create(data);
182
- data = function () {
183
- const result = Store_1.ObservableScope.Value(dataScope);
184
- if (!result)
185
- return [];
186
- if (Array.isArray(result))
187
- return result;
188
- return [result];
189
- };
190
- const nodeList = list_1.List.Create();
191
- const scope = Store_1.ObservableScope.Create(WrapChildren(vnode.injector, children, data, nodeList));
192
- Store_1.ObservableScope.OnDestroyed(scope, function () {
193
- DestroyNodeList(nodeList);
194
- Store_1.ObservableScope.Destroy(dataScope);
195
- return true;
196
- });
172
+ let dataScope;
173
+ if (data !== undefined) {
174
+ dataScope = Store_1.ObservableScope.Create(data);
175
+ data = function () {
176
+ const result = Store_1.ObservableScope.Value(dataScope);
177
+ if (!result)
178
+ return [];
179
+ if (Array.isArray(result))
180
+ return result;
181
+ return [result];
182
+ };
183
+ }
184
+ else
185
+ data = DefaultData;
186
+ const scope = Store_1.ObservableScope.Create(WrapChildren(vnode.injector, children, data));
187
+ dataScope &&
188
+ Store_1.ObservableScope.OnDestroyed(scope, function () {
189
+ Store_1.ObservableScope.Destroy(dataScope);
190
+ return true;
191
+ });
197
192
  return scope;
198
193
  }
199
- function WrapStaticChildren(vnode, children) {
200
- return function () {
201
- vnode.children && vNode.DestroyAll(vnode.children);
202
- const childNodes = injector_1.Injector.Scope(vnode.injector, children, undefined);
203
- return CreateNodeArray(childNodes, vnode.children);
204
- };
205
- }
206
- function WrapChildren(injector, children, data, nodeList) {
194
+ function WrapChildren(injector, children, data) {
195
+ let nodeArray = [];
207
196
  return function () {
208
197
  const nextData = data();
209
198
  switch (nextData.length) {
210
- case 0:
211
- DestroyNodeList(nodeList);
199
+ case 0: {
200
+ for (let x = 0; x < nodeArray.length; x++) {
201
+ vNode.DestroyAll(nodeArray[x][1]);
202
+ }
203
+ nodeArray.splice(0);
212
204
  return [];
205
+ }
213
206
  default: {
214
- const nodeListMap = list_1.List.ToListMap(nodeList, GetData);
215
- const nextNodeList = list_1.List.Create();
216
- const nextNodeArray = [];
217
- for (let x = 0; x < nextData.length; x++) {
218
- const data = nextData[x];
219
- const existingNodeList = nodeListMap.get(data);
220
- const existingNode = existingNodeList && list_1.List.PopNode(existingNodeList);
221
- if (existingNode) {
222
- list_1.List.AddNode(nextNodeList, existingNode);
223
- if (existingNode.data.scope.dirty) {
224
- vNode.DestroyAll(existingNode.data.nodes);
225
- existingNode.data.nodes = Store_1.ObservableScope.Value(existingNode.data.scope);
226
- }
227
- else
228
- Store_1.ObservableScope.Touch(existingNode.data.scope);
229
- }
230
- else {
231
- nodeListMap.delete(data);
232
- const childrenScope = Store_1.ObservableScope.Create(function () {
233
- const childNodes = injector_1.Injector.Scope(injector, children, data);
234
- return CreateNodeArray(childNodes);
235
- });
236
- list_1.List.Add(nextNodeList, {
237
- data,
238
- nodes: Store_1.ObservableScope.Value(childrenScope),
239
- scope: childrenScope,
240
- });
241
- }
242
- nextNodeArray.push(...nextNodeList.tail.data.nodes);
243
- }
244
- for (let value of nodeListMap.values())
245
- DestroyNodeList(value);
246
- list_1.List.Append(nodeList, nextNodeList);
247
- return nextNodeArray;
207
+ if (nodeArray.length < 21)
208
+ nodeArray = EvaluateNextNodesSmall(injector, children, nextData, nodeArray);
209
+ else
210
+ nodeArray = EvaluateNextNodesLarge(injector, children, nextData, nodeArray);
248
211
  }
249
212
  }
213
+ return nodeArray;
250
214
  };
251
215
  }
216
+ function EvaluateNextNodesSmall(injector, getNextChildren, nextData, nodeArray) {
217
+ if (nextData.length === 1) {
218
+ const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, nextData[0]);
219
+ const children = CreateNodeArray(nextChildren, nodeArray[0]?.[1]);
220
+ for (let x = 0; x < nodeArray.length; x++)
221
+ vNode.DestroyAll(nodeArray[x][1]);
222
+ return [[undefined, children]];
223
+ }
224
+ let nodeArrayLength = nodeArray.length;
225
+ const nextNodes = new Array(nextData.length);
226
+ for (let x = 0; x < nextData.length; x++) {
227
+ const data = nextData[x];
228
+ const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, data);
229
+ let i = 0;
230
+ for (; i < nodeArrayLength && nodeArray[i][0] !== data; i++) { }
231
+ if (i !== nodeArrayLength) {
232
+ nextNodes[x] = nodeArray[i];
233
+ nodeArray[i] = nodeArray[nodeArray.length - 1];
234
+ nodeArrayLength--;
235
+ }
236
+ else
237
+ nextNodes[x] = [data, CreateNodeArray(nextChildren)];
238
+ }
239
+ for (let x = 0; x < nodeArrayLength; x++)
240
+ vNode.DestroyAll(nodeArray[x][1]);
241
+ return nextNodes;
242
+ }
243
+ function EvaluateNextNodesLarge(injector, getNextChildren, nextData, nodeArray) {
244
+ const nextNodes = new Array(nextData.length);
245
+ const dataMap = new Map();
246
+ for (let x = 0; x < nodeArray.length; x++) {
247
+ const arr = dataMap.get(nodeArray[x][0]) ?? [];
248
+ arr.push(nodeArray[x][1]);
249
+ dataMap.set(nodeArray[x][0], arr);
250
+ }
251
+ for (let x = 0; x < nextData.length; x++) {
252
+ const data = nextData[x];
253
+ const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, data);
254
+ const currentChildren = dataMap.get(data);
255
+ let currentChildIndex = currentChildren ? currentChildren.length - 1 : -1;
256
+ for (; currentChildIndex >= 0 && currentChildren[currentChildIndex] === null; currentChildIndex--) { }
257
+ if (currentChildIndex !== -1) {
258
+ nextNodes[x] = [data, currentChildren[currentChildIndex]];
259
+ currentChildren[currentChildIndex] = null;
260
+ if (currentChildIndex === 0)
261
+ dataMap.delete(data);
262
+ }
263
+ else {
264
+ nextNodes[x] = [data, CreateNodeArray(nextChildren)];
265
+ }
266
+ }
267
+ for (const value of dataMap.values()) {
268
+ for (let x = 0; x < value.length; x++) {
269
+ for (let y = 0; y < value[x].length; y++)
270
+ value[x][y] && vNode.Destroy(value[x][y]);
271
+ }
272
+ }
273
+ return nextNodes;
274
+ }
252
275
  function CreateNodeArray(children, previousChildren) {
253
276
  if (Array.isArray(children))
254
277
  return children;
@@ -262,22 +285,10 @@ function CreateNodeArray(children, previousChildren) {
262
285
  ? previousChildren
263
286
  : [firstPrevChild];
264
287
  }
265
- return [
266
- vNode.CreateText(children),
267
- ];
288
+ return [vNode.CreateText(children)];
268
289
  }
269
290
  return [children];
270
291
  }
271
- function DestroyNodeList(nodeList) {
272
- for (let node = nodeList.head; node !== null; node = node.next) {
273
- vNode.DestroyAll(node.data.nodes);
274
- Store_1.ObservableScope.Destroy(node.data.scope);
275
- }
276
- list_1.List.Clear(nodeList);
277
- }
278
- function GetData(data) {
279
- return data.data;
280
- }
281
292
  function AssignChildren(vnode, childrenScope) {
282
293
  const children = Store_1.ObservableScope.Peek(childrenScope);
283
294
  vnode.children = children;
@@ -286,9 +297,9 @@ function UpdateChildren(vnode, init = false, skipInit = false) {
286
297
  if (!vnode.children)
287
298
  return;
288
299
  if (vnode.children.length === 1 &&
289
- vnode.children[0].type === "text" &&
290
- vnode.children[0].node) {
291
- nodeConfig_1.NodeConfig.reconcileChildren(vnode.node, [vnode.children[0].node]);
300
+ vnode.children[0][1].length === 1 &&
301
+ vnode.children[0][1][0].node) {
302
+ nodeConfig_1.NodeConfig.reconcileChild(vnode.node, vnode.children[0][1][0].node);
292
303
  return;
293
304
  }
294
305
  const children = vnode.children;
@@ -296,24 +307,32 @@ function UpdateChildren(vnode, init = false, skipInit = false) {
296
307
  if (vnode.destroyed || children !== vnode.children)
297
308
  return;
298
309
  for (let x = 0; !skipInit && x < children.length; x++)
299
- if (children[x].definition) {
300
- const childNode = children[x];
301
- (0, thread_1.Schedule)(function () {
302
- if (vnode.destroyed || children !== vnode.children)
303
- return;
304
- vNode.Init(childNode);
305
- });
306
- }
310
+ for (let y = 0; y < children[x][1].length; y++)
311
+ if (children[x][1][y].definition) {
312
+ const childNode = children[x][1][y];
313
+ (0, thread_1.Schedule)(function () {
314
+ if (vnode.destroyed || children !== vnode.children)
315
+ return;
316
+ vNode.Init(childNode);
317
+ });
318
+ }
307
319
  (0, thread_1.Thread)(function (async) {
308
320
  if (vnode.destroyed || children !== vnode.children)
309
321
  return;
310
- if (init || !async)
311
- nodeConfig_1.NodeConfig.reconcileChildren(vnode.node, vnode.children.map((vnode) => vnode.node));
322
+ if (init || !async) {
323
+ if (vnode.children.length === 1 && vnode.children[0][1].length === 1)
324
+ nodeConfig_1.NodeConfig.reconcileChild(vnode.node, vnode.children[0][1][0].node);
325
+ else
326
+ nodeConfig_1.NodeConfig.reconcileChildren(vnode.node, vnode.children.flatMap((row) => row[1].map((vnode) => vnode.node)));
327
+ }
312
328
  else
313
329
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
314
330
  if (vnode.destroyed || children !== vnode.children)
315
331
  return;
316
- nodeConfig_1.NodeConfig.reconcileChildren(vnode.node, vnode.children.map((vnode) => vnode.node));
332
+ if (vnode.children.length === 1 && vnode.children[0][1].length === 1)
333
+ nodeConfig_1.NodeConfig.reconcileChild(vnode.node, vnode.children[0][1][0].node);
334
+ else
335
+ nodeConfig_1.NodeConfig.reconcileChildren(vnode.node, vnode.children.flatMap((row) => row[1].map((vnode) => vnode.node)));
317
336
  });
318
337
  });
319
338
  });
@@ -19,7 +19,7 @@ export type vNode = {
19
19
  definition: vNodeDefinition<any, any, any>;
20
20
  injector: Injector;
21
21
  node: Node | null;
22
- children: vNode[] | null;
22
+ children: [any, vNode[]][] | null;
23
23
  destroyed: boolean;
24
24
  onDestroyed: Emitter | null;
25
25
  scopes: IObservableScope<unknown>[];
@@ -248,11 +248,14 @@ function UpdateEmitters(scope, right) {
248
248
  function DestroyScope(scope) {
249
249
  if (!scope)
250
250
  return;
251
+ emitter_1.Emitter.Clear(scope.emitter);
251
252
  const scopes = scope.calcScopes && Object.values(scope.calcScopes);
252
253
  scopes && ObservableScope.DestroyAll(scopes);
253
254
  scope.calcScopes = null;
255
+ for (let x = 0; x < scope.emitters.length; x++)
256
+ emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
257
+ scope.calcScopes = null;
254
258
  scope.emitters = null;
255
- emitter_1.Emitter.Clear(scope.emitter);
256
259
  scope.emitter = null;
257
260
  scope.getFunction = null;
258
261
  scope.setCallback = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.55",
3
+ "version": "7.0.56",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",