j-templates 7.0.56 → 7.0.58

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
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.vNode = void 0;
4
4
  const Store_1 = require("../Store");
5
+ const observableScope_1 = require("../Store/Tree/observableScope");
5
6
  const emitter_1 = require("../Utils/emitter");
7
+ const functions_1 = require("../Utils/functions");
6
8
  const injector_1 = require("../Utils/injector");
7
9
  const thread_1 = require("../Utils/thread");
8
10
  const nodeConfig_1 = require("./nodeConfig");
@@ -58,8 +60,10 @@ var vNode;
58
60
  vnode.component?.Destroy();
59
61
  Store_1.ObservableScope.DestroyAll(vnode.scopes);
60
62
  vnode.onDestroyed && emitter_1.Emitter.Emit(vnode.onDestroyed);
61
- for (let x = 0; vnode.children && x < vnode.children.length; x++)
63
+ for (let x = 0; vnode.children && x < vnode.children.length; x++) {
62
64
  DestroyAll(vnode.children[x][1]);
65
+ Store_1.ObservableScope.Destroy(vnode.children[x][2]);
66
+ }
63
67
  }
64
68
  vNode.Destroy = Destroy;
65
69
  function DestroyAll(vnodes) {
@@ -97,10 +101,11 @@ function InitNode(vnode) {
97
101
  if (props) {
98
102
  const assignProperties = nodeConfig_1.NodeConfig.createPropertyAssignment(node);
99
103
  if (typeof props === "function") {
100
- const scope = Store_1.ObservableScope.Create(props);
101
- vnode.scopes.push(scope);
102
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignProperties));
103
- const value = Store_1.ObservableScope.Peek(scope);
104
+ const [value, scope] = Store_1.ObservableScope.CreateIf(props);
105
+ if (scope) {
106
+ vnode.scopes.push(scope);
107
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignProperties));
108
+ }
104
109
  assignProperties(value);
105
110
  }
106
111
  else
@@ -109,27 +114,24 @@ function InitNode(vnode) {
109
114
  if (on) {
110
115
  const assignEvents = nodeConfig_1.NodeConfig.createEventAssignment(node);
111
116
  if (typeof on === "function") {
112
- const scope = Store_1.ObservableScope.Create(on);
113
- vnode.scopes.push(scope);
114
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignEvents));
115
- const value = Store_1.ObservableScope.Peek(scope);
117
+ const [value, scope] = Store_1.ObservableScope.CreateIf(on);
118
+ if (scope) {
119
+ vnode.scopes.push(scope);
120
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignEvents));
121
+ }
116
122
  assignEvents(value);
117
123
  }
118
124
  else
119
125
  assignEvents(on);
120
- vnode.onDestroyed ??= emitter_1.Emitter.Create();
121
- emitter_1.Emitter.On(vnode.onDestroyed, function () {
122
- assignEvents(null);
123
- return true;
124
- });
125
126
  }
126
127
  if (attrs) {
127
128
  const assignAttributes = nodeConfig_1.NodeConfig.createAttributeAssignment(node);
128
129
  if (typeof attrs === "function") {
129
- const scope = Store_1.ObservableScope.Create(attrs);
130
- vnode.scopes.push(scope);
131
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignAttributes));
132
- const value = Store_1.ObservableScope.Peek(scope);
130
+ const [value, scope] = Store_1.ObservableScope.CreateIf(attrs);
131
+ if (scope) {
132
+ vnode.scopes.push(scope);
133
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignAttributes));
134
+ }
133
135
  assignAttributes(value);
134
136
  }
135
137
  else
@@ -144,7 +146,7 @@ function InitNode(vnode) {
144
146
  Children(vnode, componentChildren, DefaultData);
145
147
  }
146
148
  else if (childrenArray) {
147
- vnode.children = [[undefined, childrenArray]];
149
+ vnode.children = [[undefined, childrenArray, null]];
148
150
  vNode.InitAll(childrenArray);
149
151
  }
150
152
  else if (children) {
@@ -153,55 +155,52 @@ function InitNode(vnode) {
153
155
  UpdateChildren(vnode, true, !!childrenArray);
154
156
  }
155
157
  function Children(vnode, children, data) {
156
- const childrenScope = CreateChildrenScope(vnode, children, data);
157
- vnode.scopes.push(childrenScope);
158
- Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function (scope) {
159
- if (vnode.destroyed)
160
- return;
161
- const startChildren = vnode.children;
162
- AssignChildren(vnode, scope);
163
- startChildren !== vnode.children && UpdateChildren(vnode);
164
- }));
165
- AssignChildren(vnode, childrenScope);
158
+ const [childNodes, childrenScope] = CreateChildrenScope(vnode, children, data);
159
+ if (childrenScope) {
160
+ vnode.scopes.push(childrenScope);
161
+ Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function (scope) {
162
+ if (vnode.destroyed)
163
+ return;
164
+ const startChildren = vnode.children;
165
+ const newChildren = Store_1.ObservableScope.Value(scope);
166
+ if (startChildren !== newChildren) {
167
+ vnode.children = newChildren;
168
+ UpdateChildren(vnode);
169
+ }
170
+ }));
171
+ }
172
+ vnode.children = childNodes;
173
+ }
174
+ function AssignChildren(vnode, childrenScope) {
175
+ const children = Store_1.ObservableScope.Peek(childrenScope);
176
+ vnode.children = children;
166
177
  }
167
178
  const DEFAULT_DATA = [undefined];
168
179
  function DefaultData() {
169
180
  return DEFAULT_DATA;
170
181
  }
171
- function CreateChildrenScope(vnode, children, data) {
172
- let dataScope;
173
- if (data !== undefined) {
174
- dataScope = Store_1.ObservableScope.Create(data);
182
+ function CreateChildrenScope(vnode, children, data = DefaultData) {
183
+ if ((0, functions_1.IsAsync)(data)) {
184
+ const asyncData = data;
175
185
  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];
186
+ return (0, observableScope_1.CalcScope)(async function () {
187
+ return asyncData();
188
+ });
182
189
  };
183
190
  }
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
- });
192
- return scope;
191
+ return Store_1.ObservableScope.CreateIf(WrapChildren(vnode.injector, children, data));
193
192
  }
194
193
  function WrapChildren(injector, children, data) {
195
194
  let nodeArray = [];
196
195
  return function () {
197
- const nextData = data();
196
+ const nextData = ToArray(data());
198
197
  switch (nextData.length) {
199
198
  case 0: {
200
199
  for (let x = 0; x < nodeArray.length; x++) {
201
200
  vNode.DestroyAll(nodeArray[x][1]);
201
+ Store_1.ObservableScope.Destroy(nodeArray[x][2]);
202
202
  }
203
203
  nodeArray.splice(0);
204
- return [];
205
204
  }
206
205
  default: {
207
206
  if (nodeArray.length < 21)
@@ -213,31 +212,55 @@ function WrapChildren(injector, children, data) {
213
212
  return nodeArray;
214
213
  };
215
214
  }
215
+ function ToArray(result) {
216
+ if (!result)
217
+ return [];
218
+ if (Array.isArray(result))
219
+ return result;
220
+ return [result];
221
+ }
216
222
  function EvaluateNextNodesSmall(injector, getNextChildren, nextData, nodeArray) {
217
- if (nextData.length === 1) {
223
+ if (nextData === DEFAULT_DATA) {
218
224
  const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, nextData[0]);
219
225
  const children = CreateNodeArray(nextChildren, nodeArray[0]?.[1]);
220
- for (let x = 0; x < nodeArray.length; x++)
226
+ for (let x = 0; x < nodeArray.length; x++) {
221
227
  vNode.DestroyAll(nodeArray[x][1]);
222
- return [[undefined, children]];
228
+ Store_1.ObservableScope.Destroy(nodeArray[x][2]);
229
+ }
230
+ return [
231
+ [undefined, children, null],
232
+ ];
223
233
  }
224
- let nodeArrayLength = nodeArray.length;
225
234
  const nextNodes = new Array(nextData.length);
226
235
  for (let x = 0; x < nextData.length; x++) {
227
236
  const data = nextData[x];
228
- const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, data);
229
237
  let i = 0;
230
- for (; i < nodeArrayLength && nodeArray[i][0] !== data; i++) { }
231
- if (i !== nodeArrayLength) {
238
+ for (; i < nodeArray.length &&
239
+ (nodeArray[i] === null || nodeArray[i][0] !== data); i++) { }
240
+ if (i !== nodeArray.length) {
241
+ if (nodeArray[i][2]) {
242
+ const scope = nodeArray[i][2];
243
+ const value = scope.value;
244
+ const updatedValue = Store_1.ObservableScope.Value(scope);
245
+ if (value !== updatedValue)
246
+ nodeArray[i][1] = CreateNodeArray(updatedValue);
247
+ }
232
248
  nextNodes[x] = nodeArray[i];
233
- nodeArray[i] = nodeArray[nodeArray.length - 1];
234
- nodeArrayLength--;
249
+ nodeArray[i] = null;
250
+ }
251
+ else {
252
+ const [nextChildren, scope] = Store_1.ObservableScope.CreateIf(function () {
253
+ return injector_1.Injector.Scope(injector, getNextChildren, data);
254
+ });
255
+ nextNodes[x] = [data, CreateNodeArray(nextChildren), scope];
256
+ }
257
+ }
258
+ for (let x = 0; x < nodeArray.length; x++) {
259
+ if (nodeArray[x] !== null) {
260
+ vNode.DestroyAll(nodeArray[x][1]);
261
+ Store_1.ObservableScope.Destroy(nodeArray[x][2]);
235
262
  }
236
- else
237
- nextNodes[x] = [data, CreateNodeArray(nextChildren)];
238
263
  }
239
- for (let x = 0; x < nodeArrayLength; x++)
240
- vNode.DestroyAll(nodeArray[x][1]);
241
264
  return nextNodes;
242
265
  }
243
266
  function EvaluateNextNodesLarge(injector, getNextChildren, nextData, nodeArray) {
@@ -245,29 +268,44 @@ function EvaluateNextNodesLarge(injector, getNextChildren, nextData, nodeArray)
245
268
  const dataMap = new Map();
246
269
  for (let x = 0; x < nodeArray.length; x++) {
247
270
  const arr = dataMap.get(nodeArray[x][0]) ?? [];
248
- arr.push(nodeArray[x][1]);
271
+ arr.push(nodeArray[x]);
249
272
  dataMap.set(nodeArray[x][0], arr);
250
273
  }
251
274
  for (let x = 0; x < nextData.length; x++) {
252
275
  const data = nextData[x];
253
- const nextChildren = injector_1.Injector.Scope(injector, getNextChildren, data);
254
276
  const currentChildren = dataMap.get(data);
255
277
  let currentChildIndex = currentChildren ? currentChildren.length - 1 : -1;
256
278
  for (; currentChildIndex >= 0 && currentChildren[currentChildIndex] === null; currentChildIndex--) { }
257
279
  if (currentChildIndex !== -1) {
258
- nextNodes[x] = [data, currentChildren[currentChildIndex]];
280
+ const currentChild = currentChildren[currentChildIndex];
281
+ if (currentChild[2]) {
282
+ const scope = currentChild[2];
283
+ const value = scope.value;
284
+ const updatedValue = Store_1.ObservableScope.Value(scope);
285
+ if (value !== updatedValue)
286
+ currentChild[1] = CreateNodeArray(updatedValue);
287
+ }
288
+ if (currentChild[2]?.dirty) {
289
+ const nextChildren = Store_1.ObservableScope.Value(currentChild[2]);
290
+ currentChild[1] = CreateNodeArray(nextChildren);
291
+ }
292
+ nextNodes[x] = currentChild;
259
293
  currentChildren[currentChildIndex] = null;
260
294
  if (currentChildIndex === 0)
261
295
  dataMap.delete(data);
262
296
  }
263
297
  else {
264
- nextNodes[x] = [data, CreateNodeArray(nextChildren)];
298
+ const [nextChildren, scope] = Store_1.ObservableScope.CreateIf(function () {
299
+ return injector_1.Injector.Scope(injector, getNextChildren, data);
300
+ });
301
+ nextNodes[x] = [data, CreateNodeArray(nextChildren), scope];
265
302
  }
266
303
  }
267
304
  for (const value of dataMap.values()) {
268
305
  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]);
306
+ const row = value[x];
307
+ row && vNode.DestroyAll(row[1]);
308
+ row && Store_1.ObservableScope.Destroy(row[2]);
271
309
  }
272
310
  }
273
311
  return nextNodes;
@@ -289,10 +327,6 @@ function CreateNodeArray(children, previousChildren) {
289
327
  }
290
328
  return [children];
291
329
  }
292
- function AssignChildren(vnode, childrenScope) {
293
- const children = Store_1.ObservableScope.Peek(childrenScope);
294
- vnode.children = children;
295
- }
296
330
  function UpdateChildren(vnode, init = false, skipInit = false) {
297
331
  if (!vnode.children)
298
332
  return;
@@ -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: [any, vNode[]][] | null;
22
+ children: [any, vNode[], IObservableScope<string | vNode | vNode[]> | null][] | null;
23
23
  destroyed: boolean;
24
24
  onDestroyed: Emitter | null;
25
25
  scopes: IObservableScope<unknown>[];
@@ -47,6 +47,9 @@ export declare namespace ObservableScope {
47
47
  function Create<T>(valueFunction: {
48
48
  (): T | Promise<T>;
49
49
  }, calc?: boolean): IObservableScope<T>;
50
+ function CreateIf<T>(valueFunction: {
51
+ (): T | Promise<T>;
52
+ }): [T, IObservableScope<T> | null];
50
53
  function Register(emitter: Emitter): void;
51
54
  function Init<T>(scope: IObservableScope<T>): void;
52
55
  function Peek<T>(scope: IObservableScope<T>): T;
@@ -65,6 +65,21 @@ function WatchScope(scope) {
65
65
  watchState = parent;
66
66
  return value;
67
67
  }
68
+ function WatchFunction(func) {
69
+ const parent = watchState;
70
+ watchState = [[], null, null];
71
+ const async = (0, functions_1.IsAsync)(func);
72
+ const result = func();
73
+ let scope = null;
74
+ if (watchState[0].length > 0 || async) {
75
+ scope = ObservableScope.Create(func);
76
+ UpdateEmitters(scope, watchState[0]);
77
+ scope.calcScopes = watchState[2];
78
+ UpdateValue(scope, result);
79
+ }
80
+ watchState = parent;
81
+ return [async ? null : result, scope];
82
+ }
68
83
  function CalcScope(callback, idOverride) {
69
84
  if (watchState === null)
70
85
  return callback();
@@ -103,6 +118,10 @@ function CalcScope(callback, idOverride) {
103
118
  return scope;
104
119
  }
105
120
  ObservableScope.Create = Create;
121
+ function CreateIf(valueFunction) {
122
+ return WatchFunction(valueFunction);
123
+ }
124
+ ObservableScope.CreateIf = CreateIf;
106
125
  function Register(emitter) {
107
126
  if (watchState === null)
108
127
  return;
@@ -205,11 +224,11 @@ function OnSet(scope) {
205
224
  }
206
225
  DirtyScope(scope);
207
226
  }
208
- function UpdateValue(scope) {
227
+ function UpdateValue(scope, valueOverride = undefined) {
209
228
  if (!scope.dirty)
210
229
  return;
211
230
  scope.dirty = false;
212
- const value = WatchScope(scope);
231
+ const value = valueOverride === undefined ? WatchScope(scope) : valueOverride;
213
232
  if (scope.async) {
214
233
  scope.promise = value.then(function (result) {
215
234
  if (scope.destroyed)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.56",
3
+ "version": "7.0.58",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",