j-templates 7.0.31 → 7.0.32

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/Node/vNode.js CHANGED
@@ -6,11 +6,16 @@ const injector_1 = require("../Utils/injector");
6
6
  const list_1 = require("../Utils/list");
7
7
  const thread_1 = require("../Utils/thread");
8
8
  const nodeConfig_1 = require("./nodeConfig");
9
+ const DEFAULT_VNODE_ARRAY = [undefined];
10
+ function DEFAULT_VNODE_DATA() {
11
+ return DEFAULT_VNODE_ARRAY;
12
+ }
9
13
  var vNode;
10
14
  (function (vNode) {
11
15
  function Create(definition) {
12
16
  return {
13
17
  definition,
18
+ type: definition.type,
14
19
  injector: injector_1.Injector.Current() ?? new injector_1.Injector(),
15
20
  node: null,
16
21
  children: null,
@@ -133,48 +138,100 @@ function InitNode(vnode) {
133
138
  vNode.InitAll(vnode.children);
134
139
  }
135
140
  else if (children) {
136
- if (data) {
137
- DynamicChildren(vnode, children, ToArray(data));
138
- }
139
- else
140
- StaticChildren(vnode, children);
141
+ Children(vnode, children, data ? ToArray(data) : DEFAULT_VNODE_DATA);
141
142
  }
142
143
  UpdateChildren(vnode, true, !!childrenArray);
143
144
  }
144
- function StaticChildren(vnode, children) {
145
- const childrenScope = Store_1.ObservableScope.Create(WrapStaticChildren(vnode.injector, children));
145
+ function Children(vnode, children, data) {
146
+ const nodeList = list_1.List.Create();
147
+ const childrenScope = Store_1.ObservableScope.Create(WrapChildren(vnode.injector, children, data, nodeList));
148
+ Store_1.ObservableScope.OnDestroyed(childrenScope, function () {
149
+ DestroyNodeList(nodeList);
150
+ });
146
151
  vnode.scopes.push(childrenScope);
147
- const child = Store_1.ObservableScope.Peek(childrenScope);
148
- switch (typeof child) {
152
+ Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function (scope) {
153
+ if (vnode.destroyed)
154
+ return;
155
+ AssignChildren(vnode, scope);
156
+ UpdateChildren(vnode);
157
+ }));
158
+ AssignChildren(vnode, childrenScope);
159
+ }
160
+ function AssignChildren(vnode, childrenScope) {
161
+ const children = Store_1.ObservableScope.Value(childrenScope);
162
+ switch (typeof children) {
149
163
  case 'string': {
150
- const node = injector_1.Injector.Scope(vnode.injector, vNode.Create, {
151
- type: "text",
152
- namespace: null,
153
- props() {
154
- return { nodeValue: Store_1.ObservableScope.Value(childrenScope) };
155
- },
156
- });
157
- vnode.children = [node];
164
+ if (vnode.children?.[0]?.type !== 'text') {
165
+ vnode.children && vNode.DestroyAll(vnode.children);
166
+ const node = injector_1.Injector.Scope(vnode.injector, vNode.Create, {
167
+ type: "text",
168
+ namespace: null,
169
+ props() {
170
+ return { nodeValue: Store_1.ObservableScope.Value(childrenScope) };
171
+ },
172
+ });
173
+ vnode.children = [node];
174
+ }
158
175
  break;
159
176
  }
160
177
  default: {
161
- Store_1.ObservableScope.Touch(childrenScope);
162
- Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function () {
163
- if (vnode.destroyed)
164
- return;
165
- vNode.DestroyAll(vnode.children);
166
- const nodes = Store_1.ObservableScope.Peek(childrenScope);
167
- vnode.children = Array.isArray(nodes) ? nodes : [nodes];
168
- UpdateChildren(vnode);
169
- }));
170
- vnode.children = (Array.isArray(child) ? child : [child]);
178
+ vnode.children = Array.isArray(children) ? children : [children];
171
179
  break;
172
180
  }
173
181
  }
174
182
  }
175
- function WrapStaticChildren(injector, children) {
183
+ function WrapChildren(injector, children, data, nodeList) {
176
184
  return function () {
177
- return injector_1.Injector.Scope(injector, children, undefined);
185
+ const nextData = data();
186
+ switch (nextData.length) {
187
+ case 0:
188
+ DestroyNodeList(nodeList);
189
+ return [];
190
+ case 1:
191
+ DestroyNodeList(nodeList);
192
+ return injector_1.Injector.Scope(injector, children, nextData[0]);
193
+ default: {
194
+ const nodeListMap = list_1.List.ToListMap(nodeList, GetData);
195
+ const nextNodeList = list_1.List.Create();
196
+ const nextNodeArray = [];
197
+ for (let x = 0; x < nextData.length; x++) {
198
+ const data = nextData[x];
199
+ const existingNodeList = nodeListMap.get(data);
200
+ const existingNode = existingNodeList && list_1.List.PopNode(existingNodeList);
201
+ if (existingNode) {
202
+ list_1.List.AddNode(nextNodeList, existingNode);
203
+ if (existingNode.data.scope.dirty) {
204
+ vNode.DestroyAll(existingNode.data.nodes);
205
+ existingNode.data.nodes = Store_1.ObservableScope.Value(existingNode.data.scope);
206
+ }
207
+ }
208
+ else {
209
+ nodeListMap.delete(data);
210
+ const childrenScope = Store_1.ObservableScope.Create(function () {
211
+ const childNodes = injector_1.Injector.Scope(injector, children, data);
212
+ const nodes = typeof childNodes === 'string' ? [vNode.Create({
213
+ type: 'text',
214
+ namespace: null,
215
+ props: {
216
+ nodeValue: childNodes
217
+ }
218
+ })] : Array.isArray(childNodes) ? childNodes : [childNodes];
219
+ return nodes;
220
+ });
221
+ list_1.List.Add(nextNodeList, {
222
+ data,
223
+ nodes: Store_1.ObservableScope.Value(childrenScope),
224
+ scope: childrenScope
225
+ });
226
+ }
227
+ nextNodeArray.push(...nextNodeList.tail.data.nodes);
228
+ }
229
+ for (let value of nodeListMap.values())
230
+ DestroyNodeList(value);
231
+ list_1.List.Append(nodeList, nextNodeList);
232
+ return nextNodeArray;
233
+ }
234
+ }
178
235
  };
179
236
  }
180
237
  function DestroyNodeList(nodeList) {
@@ -182,77 +239,11 @@ function DestroyNodeList(nodeList) {
182
239
  vNode.DestroyAll(node.data.nodes);
183
240
  Store_1.ObservableScope.Destroy(node.data.scope);
184
241
  }
185
- }
186
- function DynamicChildren(vnode, children, data) {
187
- const dataScope = Store_1.ObservableScope.Create(data);
188
- vnode.scopes.push(dataScope);
189
- const nodeList = list_1.List.Create();
190
- const childrenScope = Store_1.ObservableScope.Create(WrapDynamicChildren(dataScope, nodeList, vnode.injector, children));
191
- vnode.scopes.push(childrenScope);
192
- Store_1.ObservableScope.OnDestroyed(dataScope, function () {
193
- DestroyNodeList(nodeList);
194
- });
195
- Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function () {
196
- if (vnode.destroyed)
197
- return;
198
- vnode.children = Store_1.ObservableScope.Peek(childrenScope);
199
- UpdateChildren(vnode);
200
- }));
201
- vnode.children = Store_1.ObservableScope.Value(childrenScope);
242
+ list_1.List.Clear(nodeList);
202
243
  }
203
244
  function GetData(data) {
204
245
  return data.data;
205
246
  }
206
- function WrapDynamicChildren(dataScope, nodeList, injector, children) {
207
- return function () {
208
- const nextData = Store_1.ObservableScope.Value(dataScope);
209
- const nodeMap = list_1.List.ToNodeMap(nodeList, GetData);
210
- const nextNodeList = list_1.List.Create();
211
- const nextNodeArray = [];
212
- for (let x = 0; x < nextData.length; x++) {
213
- const data = nextData[x];
214
- const existingNodeArray = nodeMap.get(data);
215
- let existingNode = null;
216
- for (let x = 0; existingNodeArray &&
217
- x < existingNodeArray.length &&
218
- existingNode === null; x++) {
219
- existingNode = existingNodeArray[x];
220
- existingNodeArray[x] = null;
221
- }
222
- if (existingNode !== null) {
223
- list_1.List.RemoveNode(nodeList, existingNode);
224
- list_1.List.AddNode(nextNodeList, existingNode);
225
- if (existingNode.data.scope.dirty) {
226
- vNode.DestroyAll(existingNode.data.nodes);
227
- existingNode.data.nodes = Store_1.ObservableScope.Value(existingNode.data.scope);
228
- }
229
- }
230
- else {
231
- const childrenScope = Store_1.ObservableScope.Create(function () {
232
- const childNodes = injector_1.Injector.Scope(injector, children, data);
233
- const nodes = typeof childNodes === 'string' ? [vNode.Create({
234
- type: 'text',
235
- namespace: null,
236
- props: {
237
- nodeValue: childNodes
238
- }
239
- })] : Array.isArray(childNodes) ? childNodes : [childNodes];
240
- return nodes;
241
- });
242
- list_1.List.Add(nextNodeList, {
243
- data,
244
- nodes: Store_1.ObservableScope.Value(childrenScope),
245
- scope: childrenScope,
246
- });
247
- }
248
- nextNodeArray.push(...nextNodeList.tail.data.nodes);
249
- }
250
- DestroyNodeList(nodeList);
251
- list_1.List.Clear(nodeList);
252
- list_1.List.Append(nodeList, nextNodeList);
253
- return nextNodeArray;
254
- };
255
- }
256
247
  function UpdateChildren(vnode, init = false, skipInit = false) {
257
248
  if (!vnode.children)
258
249
  return;
@@ -310,13 +301,13 @@ function ScheduledAssignment(assign) {
310
301
  }
311
302
  function CreateScheduledCallback(callback) {
312
303
  let scheduled = false;
313
- return function () {
304
+ return function (...args) {
314
305
  if (scheduled)
315
306
  return;
316
307
  scheduled = true;
317
308
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
318
309
  scheduled = false;
319
- callback();
310
+ callback(...args);
320
311
  });
321
312
  };
322
313
  }
@@ -14,6 +14,7 @@ export type vNodeEvents<E extends {
14
14
  };
15
15
  export type vNodeChildrenFunction<T> = ((data: T) => vNode | vNode[]) | ((data: T) => string);
16
16
  export type vNode = {
17
+ type: string;
17
18
  definition: vNodeDefinition<any, any, any>;
18
19
  injector: Injector;
19
20
  node: Node | null;
@@ -232,7 +232,6 @@ function UpdateEmitters(scope, right) {
232
232
  function DestroyScope(scope) {
233
233
  if (!scope)
234
234
  return;
235
- const emitters = scope.emitters;
236
235
  scope.emitters = null;
237
236
  scope.emitter = null;
238
237
  scope.getFunction = null;
package/Utils/list.d.ts CHANGED
@@ -26,4 +26,5 @@ export declare namespace List {
26
26
  }): void;
27
27
  function Append<T>(appendTo: IList<T>, append: IList<T>): void;
28
28
  function ToNodeMap<T>(list: IList<T>, keyCallback: (data: T) => unknown): Map<any, INode<T>[]>;
29
+ function ToListMap<T>(list: IList<T>, keyCallback: (data: T) => unknown): Map<any, IList<T>>;
29
30
  }
package/Utils/list.js CHANGED
@@ -209,4 +209,18 @@ var List;
209
209
  return map;
210
210
  }
211
211
  List.ToNodeMap = ToNodeMap;
212
+ function ToListMap(list, keyCallback) {
213
+ const map = new Map();
214
+ let node = list.head;
215
+ while (node !== null) {
216
+ const key = keyCallback(node.data);
217
+ const mapList = map.get(key) ?? List.Create();
218
+ List.RemoveNode(list, node);
219
+ List.AddNode(mapList, node);
220
+ map.set(key, mapList);
221
+ node = list.head;
222
+ }
223
+ return map;
224
+ }
225
+ List.ToListMap = ToListMap;
212
226
  })(List || (exports.List = List = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.31",
3
+ "version": "7.0.32",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",