j-templates 7.0.66 → 7.0.68

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.
@@ -169,7 +169,9 @@ exports.DOMNodeConfig = {
169
169
  target.appendChild(children[x]);
170
170
  },
171
171
  reconcileChild(target, child) {
172
- if (target.childElementCount > 1 || target.firstChild !== child)
172
+ if (target.childElementCount === 0)
173
+ target.appendChild(child);
174
+ else if (target.childElementCount > 1 || target.firstChild !== child)
173
175
  target.replaceChildren(child);
174
176
  },
175
177
  };
@@ -1,7 +1,7 @@
1
1
  import { ComponentEvents } from "./component.types";
2
- import { ObservableScope } from "../Store/Tree/observableScope";
3
2
  import { FunctionOr, vNode as vNodeType } from "./vNode.types";
4
3
  import { RecursivePartial } from "../Utils/utils.types";
4
+ import { IObservableScope } from "../Store/Tree/observableScope";
5
5
  /**
6
6
  * Base Component class.
7
7
  *
@@ -25,7 +25,7 @@ export declare class Component<D = void, T = void, E = {}> {
25
25
  /**
26
26
  * Internal scoped Observable for component state.
27
27
  */
28
- protected get Scope(): ObservableScope<D>;
28
+ protected get Scope(): IObservableScope<D>;
29
29
  /**
30
30
  * Current data value from the scoped Observable.
31
31
  */
package/Node/component.js CHANGED
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Component = void 0;
4
4
  const decorators_1 = require("../Utils/decorators");
5
- const observableScope_1 = require("../Store/Tree/observableScope");
6
5
  const vNode_1 = require("./vNode");
6
+ const Store_1 = require("../Store");
7
7
  /**
8
8
  * Base Component class.
9
9
  *
@@ -34,7 +34,7 @@ class Component {
34
34
  * Current data value from the scoped Observable.
35
35
  */
36
36
  get Data() {
37
- return this.scope.Value;
37
+ return Store_1.ObservableScope.Value(this.scope);
38
38
  }
39
39
  /**
40
40
  * Accessor for the component's virtual node.
@@ -71,10 +71,16 @@ class Component {
71
71
  constructor(vNode, config, templates) {
72
72
  this.vNode = vNode;
73
73
  const { data, on } = config;
74
+ /* if (typeof data === "function")
75
+ this.scope = new ObservableScope<D>(data as () => D | Promise<D>);
76
+ else this.scope = new ObservableScope<D>(() => data); */
74
77
  if (typeof data === "function")
75
- this.scope = new observableScope_1.ObservableScope(data);
78
+ this.scope = Store_1.ObservableScope.Create(data);
76
79
  else
77
- this.scope = new observableScope_1.ObservableScope(() => data);
80
+ this.scope = {
81
+ type: "static",
82
+ value: data,
83
+ };
78
84
  this.componentEvents = on;
79
85
  this.templates = templates || {};
80
86
  }
@@ -108,7 +114,7 @@ class Component {
108
114
  * Destroys the component, cleaning up its scoped data and decorators.
109
115
  */
110
116
  Destroy() {
111
- this.scope.Destroy();
117
+ Store_1.ObservableScope.Destroy(this.scope);
112
118
  decorators_1.Destroy.All(this);
113
119
  }
114
120
  }
package/Node/vNode.js CHANGED
@@ -104,12 +104,13 @@ function InitNode(vnode) {
104
104
  if (props) {
105
105
  const assignProperties = nodeConfig_1.NodeConfig.createPropertyAssignment(node);
106
106
  if (typeof props === "function") {
107
- const [value, scope] = Store_1.ObservableScope.CreateIf(props);
108
- if (scope) {
109
- vnode.scopes.push(scope);
110
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignProperties));
111
- }
112
- assignProperties(value);
107
+ const scope = Store_1.ObservableScope.Create(props);
108
+ // const [value, scope] = ObservableScope.CreateIf(props as () => any);
109
+ // if (scope) {
110
+ vnode.scopes.push(scope);
111
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignProperties));
112
+ // }
113
+ assignProperties(Store_1.ObservableScope.Value(scope));
113
114
  }
114
115
  else
115
116
  assignProperties(props);
@@ -117,12 +118,13 @@ function InitNode(vnode) {
117
118
  if (on) {
118
119
  const assignEvents = nodeConfig_1.NodeConfig.createEventAssignment(node);
119
120
  if (typeof on === "function") {
120
- const [value, scope] = Store_1.ObservableScope.CreateIf(on);
121
- if (scope) {
122
- vnode.scopes.push(scope);
123
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignEvents));
124
- }
125
- assignEvents(value);
121
+ const scope = Store_1.ObservableScope.Create(on);
122
+ // const [value, scope] = ObservableScope.CreateIf(on);
123
+ // if (scope) {
124
+ vnode.scopes.push(scope);
125
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignEvents));
126
+ // }
127
+ assignEvents(Store_1.ObservableScope.Value(scope));
126
128
  }
127
129
  else
128
130
  assignEvents(on);
@@ -130,12 +132,13 @@ function InitNode(vnode) {
130
132
  if (attrs) {
131
133
  const assignAttributes = nodeConfig_1.NodeConfig.createAttributeAssignment(node);
132
134
  if (typeof attrs === "function") {
133
- const [value, scope] = Store_1.ObservableScope.CreateIf(attrs);
134
- if (scope) {
135
- vnode.scopes.push(scope);
136
- Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignAttributes));
137
- }
138
- assignAttributes(value);
135
+ const scope = Store_1.ObservableScope.Create(attrs);
136
+ // const [value, scope] = ObservableScope.CreateIf(attrs);
137
+ // if (scope) {
138
+ vnode.scopes.push(scope);
139
+ Store_1.ObservableScope.Watch(scope, ScheduledAssignment(assignAttributes));
140
+ //}
141
+ assignAttributes(Store_1.ObservableScope.Value(scope));
139
142
  }
140
143
  else
141
144
  assignAttributes(attrs);
@@ -160,7 +163,7 @@ function InitNode(vnode) {
160
163
  UpdateChildren(vnode, true, !!childrenArray);
161
164
  }
162
165
  function Children(vnode, children, data) {
163
- const [childNodes, childrenScope] = CreateChildrenScope(vnode, children, data);
166
+ const childrenScope = CreateChildrenScope(vnode, children, data);
164
167
  if (childrenScope) {
165
168
  vnode.scopes.push(childrenScope);
166
169
  Store_1.ObservableScope.Watch(childrenScope, CreateScheduledCallback(function (scope) {
@@ -175,7 +178,7 @@ function Children(vnode, children, data) {
175
178
  }
176
179
  }));
177
180
  }
178
- vnode.children = childNodes;
181
+ vnode.children = Store_1.ObservableScope.Value(childrenScope);
179
182
  // AssignChildren(vnode, childrenScope);
180
183
  }
181
184
  /* function AssignChildren(
@@ -204,7 +207,7 @@ function CreateChildrenScope(vnode, children, data = DefaultData) {
204
207
  });
205
208
  };
206
209
  }
207
- return Store_1.ObservableScope.CreateIf(WrapChildren(vnode.injector, children, data));
210
+ return Store_1.ObservableScope.Create(WrapChildren(vnode.injector, children, data));
208
211
  }
209
212
  function WrapChildren(injector, children, data) {
210
213
  let nodeArray = [];
@@ -265,10 +268,14 @@ function EvaluateNextNodesSmall(injector, getNextChildren, nextData, nodeArray)
265
268
  nodeArray[i] = null;
266
269
  }
267
270
  else {
268
- const [nextChildren, scope] = Store_1.ObservableScope.CreateIf(function () {
271
+ const scope = Store_1.ObservableScope.Create(function () {
269
272
  return injector_1.Injector.Scope(injector, getNextChildren, data);
270
273
  });
271
- nextNodes[x] = [data, CreateNodeArray(nextChildren), scope];
274
+ nextNodes[x] = [
275
+ data,
276
+ CreateNodeArray(Store_1.ObservableScope.Value(scope)),
277
+ scope,
278
+ ];
272
279
  }
273
280
  }
274
281
  for (let x = 0; x < nodeArray.length; x++) {
@@ -294,27 +301,37 @@ function EvaluateNextNodesLarge(injector, getNextChildren, nextData, nodeArray)
294
301
  for (; currentChildIndex >= 0 && currentChildren[currentChildIndex] === null; currentChildIndex--) { }
295
302
  if (currentChildIndex !== -1) {
296
303
  const currentChild = currentChildren[currentChildIndex];
297
- if (currentChild[2]) {
298
- const scope = currentChild[2];
299
- const value = scope.value;
300
- const updatedValue = Store_1.ObservableScope.Value(scope);
301
- if (value !== updatedValue)
302
- currentChild[1] = CreateNodeArray(updatedValue);
303
- }
304
- if (currentChild[2]?.dirty) {
305
- const nextChildren = Store_1.ObservableScope.Value(currentChild[2]);
306
- currentChild[1] = CreateNodeArray(nextChildren);
304
+ currentChildren[currentChildIndex] = null;
305
+ // if (currentChild[2]) {
306
+ const scope = currentChild[2];
307
+ const value = scope.value;
308
+ const updatedValue = Store_1.ObservableScope.Value(scope);
309
+ if (value !== updatedValue) {
310
+ vNode.DestroyAll(currentChild[1]);
311
+ currentChild[1] = CreateNodeArray(updatedValue);
307
312
  }
313
+ // }
314
+ /* if (currentChild[2]?.dirty) {
315
+ const nextChildren = ObservableScope.Value(currentChild[2]);
316
+ currentChild[1] = CreateNodeArray(nextChildren);
317
+ } */
308
318
  nextNodes[x] = currentChild;
309
- currentChildren[currentChildIndex] = null;
319
+ // currentChildren[currentChildIndex] = null;
310
320
  if (currentChildIndex === 0)
311
321
  dataMap.delete(data);
312
322
  }
313
323
  else {
314
- const [nextChildren, scope] = Store_1.ObservableScope.CreateIf(function () {
324
+ const scope = Store_1.ObservableScope.Create(function () {
315
325
  return injector_1.Injector.Scope(injector, getNextChildren, data);
316
326
  });
317
- nextNodes[x] = [data, CreateNodeArray(nextChildren), scope];
327
+ /* const [nextChildren, scope] = ObservableScope.CreateIf(function () {
328
+ return Injector.Scope(injector, getNextChildren, data);
329
+ }); */
330
+ nextNodes[x] = [
331
+ data,
332
+ CreateNodeArray(Store_1.ObservableScope.Value(scope)),
333
+ scope,
334
+ ];
318
335
  }
319
336
  }
320
337
  for (const value of dataMap.values()) {
@@ -394,8 +411,7 @@ function ScheduledAssignment(assign) {
394
411
  return;
395
412
  scheduled = true;
396
413
  nodeConfig_1.NodeConfig.scheduleUpdate(function () {
397
- if (scope.destroyed)
398
- return;
414
+ // if (scope.destroyed) return;
399
415
  scheduled = false;
400
416
  const value = Store_1.ObservableScope.Peek(scope);
401
417
  assign(value);
@@ -4,6 +4,7 @@ export declare const GET_OBSERVABLE_VALUE = "____getObservableValue";
4
4
  export declare const GET_TO_JSON = "toJSON";
5
5
  export declare namespace ObservableNode {
6
6
  function BypassProxy(value: boolean): void;
7
+ function Unwrap<T>(value: T): T;
7
8
  function Create<T>(value: T): T;
8
9
  function Touch(value: unknown, prop?: string | number): void;
9
10
  function ApplyDiff(rootNode: any, diffResult: JsonDiffResult): void;
@@ -87,7 +87,7 @@ function CreateProxyFactory(alias) {
87
87
  const ToJson = alias !== undefined ? ToJsonCopy : ToJsonDefault;
88
88
  const readOnly = alias !== undefined;
89
89
  function CreateArrayProxy(value) {
90
- const scope = observableScope_1.ObservableScope.Create(() => value);
90
+ const scope = observableScope_1.ObservableScope.Create(() => value, false, true);
91
91
  const proxy = new Proxy(value, {
92
92
  get: ArrayProxyGetter,
93
93
  set: ArrayProxySetter,
@@ -235,7 +235,7 @@ function CreateProxyFactory(alias) {
235
235
  leafScopes[prop] ??= observableScope_1.ObservableScope.Create(function () {
236
236
  const value = parent[prop];
237
237
  return CreateProxyFromValue(value);
238
- });
238
+ }, false, true);
239
239
  return observableScope_1.ObservableScope.Value(leafScopes[prop]);
240
240
  }
241
241
  function CreateProxyFromValue(value) {
@@ -268,6 +268,10 @@ var ObservableNode;
268
268
  bypassProxy = value;
269
269
  }
270
270
  ObservableNode.BypassProxy = BypassProxy;
271
+ function Unwrap(value) {
272
+ return UnwrapProxy(value);
273
+ }
274
+ ObservableNode.Unwrap = Unwrap;
271
275
  function Create(value) {
272
276
  return DefaultCreateProxy(value);
273
277
  }
@@ -1,64 +1,39 @@
1
1
  import { Emitter, EmitterCallback } from "../../Utils/emitter";
2
- import { IDestroyable } from "../../Utils/utils.types";
3
- export declare class ObservableScopeValue<T> {
4
- protected scope: IObservableScope<T>;
5
- get Value(): T;
6
- constructor(scope: IObservableScope<T>);
7
- }
8
- export declare class ObservableScopeWrapper<T> extends ObservableScopeValue<T> implements IDestroyable {
9
- private scopeEmitter;
10
- constructor(scope: IObservableScope<T>);
11
- Scope<O>(callback: {
12
- (parent: T): O;
13
- }): ObservableScope<O>;
14
- Watch(callback: {
15
- (scope: ObservableScopeValue<T>): void;
16
- }): void;
17
- Unwatch(callback: {
18
- (scope: ObservableScopeValue<T>): void;
19
- }): void;
20
- Destroy(): void;
21
- }
22
- export declare class ObservableScope<T> extends ObservableScopeWrapper<T> {
23
- constructor(getFunction: {
24
- (): T | Promise<T>;
25
- });
2
+ interface IStaticObservableScope<T> {
3
+ type: "static";
4
+ value: T;
26
5
  }
27
- export interface IObservableScope<T> extends IDestroyable {
28
- getFunction: {
29
- (): T;
30
- };
31
- setCallback: EmitterCallback;
6
+ interface IDynamicObservableScope<T> {
7
+ type: "dynamic";
32
8
  async: boolean;
33
- value: T;
34
- promise: Promise<T> | null;
9
+ greedy: boolean;
35
10
  dirty: boolean;
11
+ destroyed: boolean;
12
+ getFunction: () => Promise<T> | T;
13
+ setCallback: EmitterCallback;
14
+ value: T;
36
15
  emitter: Emitter;
37
16
  emitters: (Emitter | null)[];
17
+ onDestroyed: Emitter | null;
38
18
  calcScopes: {
39
19
  [id: string]: IObservableScope<unknown> | null;
40
20
  } | null;
41
- calc: boolean;
42
- onDestroyed: Emitter | null;
43
- destroyed: boolean;
44
21
  }
22
+ export type IObservableScope<T> = IStaticObservableScope<T> | IDynamicObservableScope<T>;
45
23
  export declare function CalcScope<T>(callback: () => T, idOverride?: string): T;
46
24
  export declare namespace ObservableScope {
47
25
  function Create<T>(valueFunction: {
48
26
  (): T | Promise<T>;
49
- }, calc?: boolean): IObservableScope<T>;
50
- function CreateIf<T>(valueFunction: {
51
- (): T | Promise<T>;
52
- }, greedy?: boolean): [T, IObservableScope<T> | null];
27
+ }, greedy?: boolean, force?: boolean): IObservableScope<T>;
53
28
  function Register(emitter: Emitter): void;
54
- function Init<T>(scope: IObservableScope<T>): void;
55
29
  function Peek<T>(scope: IObservableScope<T>): T;
56
30
  function Value<T>(scope: IObservableScope<T>): T;
57
31
  function Touch<T>(scope: IObservableScope<T>): void;
58
32
  function Watch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T>]>): void;
59
- function Unwatch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T> | ObservableScopeValue<T>]>): void;
33
+ function Unwatch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T>]>): void;
60
34
  function OnDestroyed(scope: IObservableScope<unknown>, callback: EmitterCallback): void;
61
35
  function Update(scope: IObservableScope<any>): void;
62
36
  function Destroy<T>(scope: IObservableScope<T>): void;
63
37
  function DestroyAll(scopes: IObservableScope<unknown>[]): void;
64
38
  }
39
+ export {};
@@ -1,144 +1,222 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ObservableScope = exports.ObservableScopeWrapper = exports.ObservableScopeValue = void 0;
3
+ exports.ObservableScope = void 0;
4
4
  exports.CalcScope = CalcScope;
5
5
  const array_1 = require("../../Utils/array");
6
6
  const emitter_1 = require("../../Utils/emitter");
7
7
  const functions_1 = require("../../Utils/functions");
8
- class ObservableScopeValue {
9
- get Value() {
10
- return ObservableScope.Value(this.scope);
11
- }
12
- constructor(scope) {
13
- this.scope = scope;
14
- }
8
+ function CreateDynamicScope(getFunction, greedy, value) {
9
+ const async = (0, functions_1.IsAsync)(getFunction);
10
+ const scope = {
11
+ type: "dynamic",
12
+ async,
13
+ greedy: greedy || async,
14
+ dirty: false,
15
+ destroyed: false,
16
+ getFunction,
17
+ setCallback: function () {
18
+ return OnSet(scope);
19
+ },
20
+ value,
21
+ emitter: emitter_1.Emitter.Create(),
22
+ emitters: null,
23
+ onDestroyed: null,
24
+ calcScopes: null,
25
+ };
26
+ return scope;
27
+ }
28
+ function CreateStaticScope(initialValue) {
29
+ return {
30
+ type: "static",
31
+ value: initialValue,
32
+ };
15
33
  }
16
- exports.ObservableScopeValue = ObservableScopeValue;
17
- class ObservableScopeWrapper extends ObservableScopeValue {
18
- constructor(scope) {
19
- super(scope);
20
- if (scope.emitter) {
21
- this.scopeEmitter = emitter_1.Emitter.Create();
22
- emitter_1.Emitter.On(scope.emitter, () => emitter_1.Emitter.Emit(this.scopeEmitter, this));
34
+ let scopeQueue = [];
35
+ function ProcessScopeQueue() {
36
+ const queue = scopeQueue;
37
+ scopeQueue = [];
38
+ for (let x = 0; x < queue.length; x++) {
39
+ const scope = queue[x];
40
+ if (!scope.destroyed) {
41
+ const value = scope.value;
42
+ ExecuteScope(scope);
43
+ if (scope.value !== value)
44
+ emitter_1.Emitter.Emit(scope.emitter, scope);
23
45
  }
24
46
  }
25
- Scope(callback) {
26
- return new ObservableScope(() => callback(this.Value));
27
- }
28
- Watch(callback) {
29
- if (!this.scopeEmitter)
30
- return;
31
- emitter_1.Emitter.On(this.scopeEmitter, callback);
32
- callback(this);
47
+ }
48
+ function OnSetQueued(scope) {
49
+ if (scopeQueue.length === 0)
50
+ queueMicrotask(ProcessScopeQueue);
51
+ scopeQueue.push(scope);
52
+ }
53
+ function OnSet(scope) {
54
+ if (scope.type === "static" || scope.destroyed)
55
+ return true;
56
+ if (scope.dirty)
57
+ return false;
58
+ scope.dirty = true;
59
+ if (scope.greedy) {
60
+ OnSetQueued(scope);
61
+ return;
33
62
  }
34
- Unwatch(callback) {
35
- if (!this.scopeEmitter)
36
- return;
37
- emitter_1.Emitter.Remove(this.scopeEmitter, callback);
63
+ emitter_1.Emitter.Emit(scope.emitter, scope);
64
+ return false;
65
+ }
66
+ function RegisterEmitter(emitter) {
67
+ if (watchState.emitterIds) {
68
+ if (!watchState.emitterIds.has(emitter[0])) {
69
+ watchState.emitters.push(emitter);
70
+ watchState.emitterIds.add(emitter[0]);
71
+ }
72
+ return;
38
73
  }
39
- Destroy() {
40
- DestroyScope(this.scope);
41
- this.scopeEmitter && emitter_1.Emitter.Clear(this.scopeEmitter);
74
+ watchState.emitters.push(emitter);
75
+ if (watchState.emitters.length > 50) {
76
+ const idSet = (watchState.emitterIds = new Set());
77
+ let writePos = 0;
78
+ for (let x = 1; x < watchState.emitters.length; x++) {
79
+ if (!idSet.has(watchState.emitters[x][0])) {
80
+ watchState.emitters[++writePos] = watchState.emitters[x];
81
+ idSet.add(watchState.emitters[x][0]);
82
+ }
83
+ }
84
+ writePos++;
85
+ if (writePos < watchState.emitters.length)
86
+ watchState.emitters.splice(writePos);
42
87
  }
43
88
  }
44
- exports.ObservableScopeWrapper = ObservableScopeWrapper;
45
- class ObservableScope extends ObservableScopeWrapper {
46
- constructor(getFunction) {
47
- super(ObservableScope.Create(getFunction));
48
- }
89
+ function RegisterScope(scope) {
90
+ if (watchState === null || scope.type === "static")
91
+ return;
92
+ RegisterEmitter(scope.emitter);
93
+ }
94
+ function GetScopeValue(scope) {
95
+ if (scope.type === "static" || !scope.dirty || scope.destroyed)
96
+ return scope.value;
97
+ ExecuteScope(scope);
98
+ return scope.value;
49
99
  }
50
- exports.ObservableScope = ObservableScope;
51
100
  let watchState = null;
52
- function WatchScope(scope) {
101
+ function WatchFunction(callback, currentCalc = null) {
53
102
  const parent = watchState;
54
- watchState = [[], scope.calcScopes, null];
55
- const value = scope.getFunction();
56
- const emitters = watchState[0];
57
- UpdateEmitters(scope, emitters);
58
- const calcScopes = watchState[1];
59
- if (calcScopes) {
60
- const calcScopeValues = Object.values(calcScopes);
61
- for (let x = 0; x < calcScopeValues.length; x++)
62
- calcScopeValues[x] && ObservableScope.Destroy(calcScopeValues[x]);
63
- }
64
- scope.calcScopes = watchState[2];
103
+ watchState = {
104
+ value: null,
105
+ emitters: [],
106
+ emitterIds: null,
107
+ currentCalc: currentCalc,
108
+ nextCalc: null,
109
+ };
110
+ watchState.value = callback();
111
+ const resultState = watchState;
65
112
  watchState = parent;
66
- return value;
113
+ return resultState;
67
114
  }
68
- function WatchFunction(func, greedy) {
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, greedy);
76
- UpdateEmitters(scope, watchState[0]);
77
- scope.calcScopes = watchState[2];
78
- UpdateValue(scope, result);
115
+ function ExecuteScope(scope) {
116
+ scope.dirty = false;
117
+ const state = WatchFunction(scope.getFunction, scope.calcScopes);
118
+ UpdateEmitters(scope, state.emitters, !!state.emitterIds);
119
+ const calcScopes = state.currentCalc;
120
+ scope.calcScopes = state.nextCalc;
121
+ for (const key in calcScopes)
122
+ DestroyScope(calcScopes[key]);
123
+ if (scope.async)
124
+ state.value.then(function (result) {
125
+ scope.value = result;
126
+ emitter_1.Emitter.Emit(scope.emitter, scope);
127
+ });
128
+ else
129
+ scope.value = state.value;
130
+ }
131
+ function ExecuteFunction(callback, greedy, allowStatic) {
132
+ const async = (0, functions_1.IsAsync)(callback);
133
+ const state = WatchFunction(callback);
134
+ if (!allowStatic || async || state.emitters.length > 0) {
135
+ const scope = CreateDynamicScope(callback, greedy, async ? null : state.value);
136
+ scope.calcScopes = state.nextCalc;
137
+ UpdateEmitters(scope, state.emitters, !!state.emitterIds);
138
+ if (async)
139
+ state.value.then(function (result) {
140
+ scope.value = result;
141
+ emitter_1.Emitter.Emit(scope.emitter, scope);
142
+ });
143
+ return scope;
79
144
  }
80
- watchState = parent;
81
- return [async ? null : result, scope];
145
+ return CreateStaticScope(state.value);
82
146
  }
83
147
  function CalcScope(callback, idOverride) {
84
148
  if (watchState === null)
85
149
  return callback();
86
- const nextScopes = (watchState[2] ??= {});
87
- const currentScopes = watchState[1];
150
+ const nextScopes = (watchState.nextCalc ??= {});
88
151
  const id = idOverride ?? callback.toString();
89
- const currentScope = currentScopes?.[id];
90
- if (currentScope) {
152
+ if (nextScopes[id]) {
153
+ RegisterScope(nextScopes[id]);
154
+ return GetScopeValue(nextScopes[id]);
155
+ }
156
+ const currentScopes = watchState.currentCalc;
157
+ nextScopes[id] = currentScopes?.[id] ?? ExecuteFunction(callback, true, true);
158
+ if (currentScopes?.[id])
91
159
  delete currentScopes[id];
92
- nextScopes[id] = currentScope;
160
+ RegisterScope(nextScopes[id]);
161
+ return GetScopeValue(nextScopes[id]);
162
+ }
163
+ function UpdateEmitters(scope, right, distinct = false) {
164
+ distinct ? emitter_1.Emitter.Sort(right) : emitter_1.Emitter.Distinct(right);
165
+ if (scope.emitters === null) {
166
+ for (let x = 0; x < right.length; x++)
167
+ emitter_1.Emitter.On(right[x], scope.setCallback);
168
+ scope.emitters = right;
169
+ return;
93
170
  }
94
- else if (!nextScopes[id]) {
95
- const scope = ObservableScope.Create(callback, true);
96
- nextScopes[id] = scope;
171
+ if (right.length === 0) {
172
+ if (scope.emitters.length > 0) {
173
+ for (let x = 0; x < scope.emitters.length; x++)
174
+ emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
175
+ scope.emitters = [];
176
+ }
177
+ return;
97
178
  }
98
- return ObservableScope.Value(nextScopes[id]);
179
+ (0, array_1.ReconcileSortedEmitters)(scope.emitters, right, function (emitter) {
180
+ emitter_1.Emitter.On(emitter, scope.setCallback);
181
+ }, function (emitter) {
182
+ emitter_1.Emitter.Remove(emitter, scope.setCallback);
183
+ });
184
+ scope.emitters = right;
99
185
  }
186
+ function DestroyAllScopes(scopes) {
187
+ for (let x = 0; x < scopes.length; x++)
188
+ DestroyScope(scopes[x]);
189
+ }
190
+ function DestroyScope(scope) {
191
+ if (!scope || scope.type === "static")
192
+ return;
193
+ emitter_1.Emitter.Clear(scope.emitter);
194
+ const scopes = scope.calcScopes && Object.values(scope.calcScopes);
195
+ scopes && DestroyAllScopes(scopes);
196
+ scope.calcScopes = null;
197
+ for (let x = 0; x < scope.emitters.length; x++)
198
+ emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
199
+ scope.value = undefined;
200
+ scope.calcScopes = null;
201
+ scope.emitters = null;
202
+ scope.emitter = null;
203
+ scope.getFunction = null;
204
+ scope.setCallback = null;
205
+ scope.destroyed = true;
206
+ scope.onDestroyed && emitter_1.Emitter.Emit(scope.onDestroyed, scope);
207
+ }
208
+ var ObservableScope;
100
209
  (function (ObservableScope) {
101
- function Create(valueFunction, calc = false) {
102
- const scope = {
103
- getFunction: valueFunction,
104
- value: null,
105
- promise: null,
106
- async: (0, functions_1.IsAsync)(valueFunction),
107
- dirty: true,
108
- emitter: emitter_1.Emitter.Create(),
109
- emitters: null,
110
- calcScopes: null,
111
- calc,
112
- destroyed: false,
113
- onDestroyed: null,
114
- setCallback: function () {
115
- return OnSet(scope);
116
- },
117
- };
118
- return scope;
210
+ function Create(valueFunction, greedy = false, force = false) {
211
+ return ExecuteFunction(valueFunction, greedy, !force);
119
212
  }
120
213
  ObservableScope.Create = Create;
121
- function CreateIf(valueFunction, greedy = false) {
122
- return WatchFunction(valueFunction, greedy);
123
- }
124
- ObservableScope.CreateIf = CreateIf;
125
214
  function Register(emitter) {
126
- if (watchState === null)
127
- return;
128
- watchState[0].push(emitter);
215
+ RegisterEmitter(emitter);
129
216
  }
130
217
  ObservableScope.Register = Register;
131
- function Init(scope) {
132
- if (!scope)
133
- return;
134
- UpdateValue(scope);
135
- }
136
- ObservableScope.Init = Init;
137
218
  function Peek(scope) {
138
- if (!scope)
139
- return undefined;
140
- UpdateValue(scope);
141
- return scope.value;
219
+ return GetScopeValue(scope);
142
220
  }
143
221
  ObservableScope.Peek = Peek;
144
222
  function Value(scope) {
@@ -149,30 +227,32 @@ function CalcScope(callback, idOverride) {
149
227
  }
150
228
  ObservableScope.Value = Value;
151
229
  function Touch(scope) {
152
- if (!scope || !scope.emitter)
230
+ if (!scope)
153
231
  return;
154
- Register(scope.emitter);
232
+ RegisterScope(scope);
155
233
  }
156
234
  ObservableScope.Touch = Touch;
157
235
  function Watch(scope, callback) {
158
- if (!scope || !scope.emitter)
236
+ if (!scope || scope.type === "static")
159
237
  return;
160
238
  emitter_1.Emitter.On(scope.emitter, callback);
161
239
  }
162
240
  ObservableScope.Watch = Watch;
163
241
  function Unwatch(scope, callback) {
164
- if (!scope || !scope.emitter)
242
+ if (!scope || scope.type === "static")
165
243
  return;
166
244
  emitter_1.Emitter.Remove(scope.emitter, callback);
167
245
  }
168
246
  ObservableScope.Unwatch = Unwatch;
169
247
  function OnDestroyed(scope, callback) {
248
+ if (scope.type === "static")
249
+ return;
170
250
  scope.onDestroyed ??= emitter_1.Emitter.Create();
171
251
  emitter_1.Emitter.On(scope.onDestroyed, callback);
172
252
  }
173
253
  ObservableScope.OnDestroyed = OnDestroyed;
174
254
  function Update(scope) {
175
- if (!scope || scope.dirty || scope.destroyed)
255
+ if (!scope)
176
256
  return;
177
257
  OnSet(scope);
178
258
  }
@@ -182,102 +262,7 @@ function CalcScope(callback, idOverride) {
182
262
  }
183
263
  ObservableScope.Destroy = Destroy;
184
264
  function DestroyAll(scopes) {
185
- for (let x = 0; x < scopes.length; x++)
186
- Destroy(scopes[x]);
265
+ DestroyAllScopes(scopes);
187
266
  }
188
267
  ObservableScope.DestroyAll = DestroyAll;
189
268
  })(ObservableScope || (exports.ObservableScope = ObservableScope = {}));
190
- function DirtyScope(scope) {
191
- if (scope.dirty || !scope.getFunction)
192
- return;
193
- scope.dirty = true;
194
- if (scope.async)
195
- UpdateValue(scope);
196
- else if (scope.calc) {
197
- const startValue = scope.value;
198
- UpdateValue(scope);
199
- startValue !== scope.value && emitter_1.Emitter.Emit(scope.emitter, scope);
200
- }
201
- else
202
- emitter_1.Emitter.Emit(scope.emitter, scope);
203
- }
204
- let scopeQueue = [];
205
- function ProcessScopeQueue() {
206
- const scopes = scopeQueue;
207
- scopeQueue = [];
208
- const distinct = new Set();
209
- for (let x = 0; x < scopes.length; x++) {
210
- if (!distinct.has(scopes[x])) {
211
- distinct.add(scopes[x]);
212
- DirtyScope(scopes[x]);
213
- }
214
- }
215
- }
216
- function OnSet(scope) {
217
- if (scope.destroyed)
218
- return true;
219
- if (scope.async || scope.calc) {
220
- if (scopeQueue.length === 0)
221
- queueMicrotask(ProcessScopeQueue);
222
- scopeQueue.push(scope);
223
- return;
224
- }
225
- DirtyScope(scope);
226
- }
227
- function UpdateValue(scope, valueOverride = undefined) {
228
- if (!scope.dirty)
229
- return;
230
- scope.dirty = false;
231
- const value = valueOverride === undefined ? WatchScope(scope) : valueOverride;
232
- if (scope.async) {
233
- scope.promise = value.then(function (result) {
234
- if (scope.destroyed)
235
- return;
236
- scope.value = result;
237
- emitter_1.Emitter.Emit(scope.emitter, scope);
238
- return result;
239
- });
240
- }
241
- else
242
- scope.value = value;
243
- }
244
- function UpdateEmitters(scope, right) {
245
- emitter_1.Emitter.Distinct(right);
246
- if (scope.emitters === null) {
247
- for (let x = 0; x < right.length; x++)
248
- emitter_1.Emitter.On(right[x], scope.setCallback);
249
- scope.emitters = right;
250
- return;
251
- }
252
- if (right.length === 0) {
253
- if (scope.emitters.length > 0) {
254
- for (let x = 0; x < scope.emitters.length; x++)
255
- emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
256
- scope.emitters = [];
257
- }
258
- return;
259
- }
260
- (0, array_1.ReconcileSortedEmitters)(scope.emitters, right, function (emitter) {
261
- emitter_1.Emitter.On(emitter, scope.setCallback);
262
- }, function (emitter) {
263
- emitter_1.Emitter.Remove(emitter, scope.setCallback);
264
- });
265
- scope.emitters = right;
266
- }
267
- function DestroyScope(scope) {
268
- if (!scope)
269
- return;
270
- emitter_1.Emitter.Clear(scope.emitter);
271
- const scopes = scope.calcScopes && Object.values(scope.calcScopes);
272
- scopes && ObservableScope.DestroyAll(scopes);
273
- scope.calcScopes = null;
274
- for (let x = 0; x < scope.emitters.length; x++)
275
- emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
276
- scope.calcScopes = null;
277
- scope.emitters = null;
278
- scope.emitter = null;
279
- scope.getFunction = null;
280
- scope.setCallback = null;
281
- scope.destroyed = true;
282
- scope.onDestroyed && emitter_1.Emitter.Emit(scope.onDestroyed);
283
- }
@@ -94,7 +94,7 @@ function CreateComputedScope(getter, defaultValue, store) {
94
94
  const data = observableScope_1.ObservableScope.Value(scope);
95
95
  store.Write(data, "root");
96
96
  });
97
- observableScope_1.ObservableScope.Init(getterScope);
97
+ // ObservableScope.Init(getterScope);
98
98
  const propertyScope = observableScope_1.ObservableScope.Create(() => store.Get("root", defaultValue));
99
99
  observableScope_1.ObservableScope.OnDestroyed(propertyScope, function () {
100
100
  observableScope_1.ObservableScope.Destroy(getterScope);
@@ -273,7 +273,7 @@ function Value() {
273
273
  function CreateValueScope(tuple) {
274
274
  return observableScope_1.ObservableScope.Create(function () {
275
275
  return tuple[1];
276
- });
276
+ }, false, true);
277
277
  }
278
278
  /**
279
279
  * Value decorator implementation for creating value properties.
@@ -359,15 +359,21 @@ function WatchDecorator(target, propertyKey, descriptor, scopeFunction) {
359
359
  function scopeFunctionWrapper() {
360
360
  return scopeFunction(instance);
361
361
  }
362
- const [value, scope] = observableScope_1.ObservableScope.CreateIf(scopeFunctionWrapper, true);
362
+ const scope = observableScope_1.ObservableScope.Create(scopeFunctionWrapper, true);
363
+ const propertyMap = GetScopeMapForInstance(this);
364
+ propertyMap[propertyKey] = [scope, undefined];
365
+ observableScope_1.ObservableScope.Watch(scope, function (scope) {
366
+ instance[propertyKey](observableScope_1.ObservableScope.Value(scope));
367
+ });
368
+ /* const [value, scope] = ObservableScope.CreateIf(scopeFunctionWrapper, true);
363
369
  if (scope) {
364
- const propertyMap = GetScopeMapForInstance(this);
365
- propertyMap[propertyKey] = [scope, undefined];
366
- observableScope_1.ObservableScope.Watch(scope, function (scope) {
367
- instance[propertyKey](observableScope_1.ObservableScope.Value(scope));
368
- });
369
- }
370
- instance[propertyKey](value);
370
+ const propertyMap = GetScopeMapForInstance(this);
371
+ propertyMap[propertyKey as string] = [scope, undefined];
372
+ ObservableScope.Watch(scope, function (scope) {
373
+ (instance as any)[propertyKey](ObservableScope.Value(scope));
374
+ });
375
+ } */
376
+ instance[propertyKey](observableScope_1.ObservableScope.Value(scope));
371
377
  return instance;
372
378
  }
373
379
  const boundArray = GetBoundArrayForPrototype(target);
package/Utils/emitter.js CHANGED
@@ -42,17 +42,19 @@ var Emitter;
42
42
  function Distinct(emitters) {
43
43
  if (emitters.length < 2)
44
44
  return;
45
- emitters.length < 51 ? DistinctSmall(emitters) : DistinctLarge(emitters);
45
+ // emitters.length < 51 ? DistinctSmall(emitters) : DistinctLarge(emitters);
46
+ DistinctSmall(emitters);
46
47
  }
47
48
  Emitter.Distinct = Distinct;
48
49
  function DistinctSmall(emitters) {
49
50
  Sort(emitters);
50
- let writePos = 1;
51
+ let writePos = 0;
51
52
  for (let x = 1; x < emitters.length; x++) {
52
- if (emitters[x][0] !== emitters[writePos - 1][0]) {
53
- emitters[writePos++] = emitters[x];
53
+ if (emitters[x][0] !== emitters[writePos][0]) {
54
+ emitters[++writePos] = emitters[x];
54
55
  }
55
56
  }
57
+ writePos++;
56
58
  if (writePos < emitters.length)
57
59
  emitters.splice(writePos);
58
60
  }
package/Utils/injector.js CHANGED
@@ -54,6 +54,9 @@ exports.Injector = Injector;
54
54
  try {
55
55
  return action(...args);
56
56
  }
57
+ catch (err) {
58
+ console.error("Error evaluating injector scope", err);
59
+ }
57
60
  finally {
58
61
  scope = parent;
59
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "j-templates",
3
- "version": "7.0.66",
3
+ "version": "7.0.68",
4
4
  "description": "j-templates",
5
5
  "license": "MIT",
6
6
  "repository": "https://github.com/TypesInCode/jTemplates",