j-templates 5.0.51 → 5.0.53
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/DOM/createEventAssignment.d.ts +3 -0
- package/DOM/createEventAssignment.js +29 -0
- package/DOM/createPropertyAssignment.d.ts +4 -0
- package/DOM/createPropertyAssignment.js +74 -0
- package/DOM/domNodeConfig.d.ts +1 -1
- package/DOM/domNodeConfig.js +27 -12
- package/DOM/elements.d.ts +1 -0
- package/DOM/elements.js +27 -24
- package/DOM/index.d.ts +2 -1
- package/DOM/index.js +2 -1
- package/DOM/svgElements.js +3 -4
- package/Node/boundNode.js +16 -34
- package/Node/boundNode.types.d.ts +10 -4
- package/Node/component.js +1 -0
- package/Node/componentNode.js +0 -1
- package/Node/elementNode.js +12 -13
- package/Node/nodeConfig.d.ts +10 -1
- package/Node/nodeRef.d.ts +1 -1
- package/Node/nodeRef.js +58 -26
- package/Node/nodeRef.types.d.ts +2 -4
- package/Store/Diff/diffTree.js +1 -2
- package/Store/Tree/observableComputed.d.ts +4 -0
- package/Store/Tree/observableComputed.js +45 -0
- package/Store/Tree/observableNode.d.ts +3 -0
- package/Store/Tree/observableNode.js +195 -0
- package/Store/Tree/observableScope.d.ts +7 -12
- package/Store/Tree/observableScope.js +38 -34
- package/Store/index.d.ts +2 -0
- package/Store/index.js +5 -1
- package/Utils/decorators.d.ts +3 -1
- package/Utils/decorators.js +36 -39
- package/Utils/emitter.d.ts +2 -6
- package/Utils/emitter.js +8 -1
- package/Utils/json.d.ts +7 -0
- package/Utils/json.js +109 -0
- package/Utils/list.js +7 -7
- package/Utils/thread.d.ts +9 -15
- package/Utils/thread.js +36 -29
- package/jTemplates.js +1 -3176
- package/jTemplates.js.map +1 -1
- package/package.json +1 -1
- package/web.export.d.ts +1 -1
- package/web.export.js +3 -1
- package/DOM/utils.d.ts +0 -1
- package/DOM/utils.js +0 -47
package/Node/nodeRef.js
CHANGED
|
@@ -6,6 +6,7 @@ const injector_1 = require("../Utils/injector");
|
|
|
6
6
|
const boundNode_1 = require("./boundNode");
|
|
7
7
|
const elementNode_1 = require("./elementNode");
|
|
8
8
|
const componentNode_1 = require("./componentNode");
|
|
9
|
+
const Store_1 = require("../Store");
|
|
9
10
|
var NodeRefType;
|
|
10
11
|
(function (NodeRefType) {
|
|
11
12
|
NodeRefType[NodeRefType["NodeRef"] = 0] = "NodeRef";
|
|
@@ -33,12 +34,12 @@ var NodeRef;
|
|
|
33
34
|
injector: injector_1.Injector.Current() || new injector_1.Injector(),
|
|
34
35
|
parent: null,
|
|
35
36
|
childNodes: null,
|
|
36
|
-
destroyed: false
|
|
37
|
-
destroyables: []
|
|
37
|
+
destroyed: false
|
|
38
38
|
};
|
|
39
39
|
case NodeRefType.BoundNode:
|
|
40
40
|
return {
|
|
41
41
|
node: null,
|
|
42
|
+
nodeDef: null,
|
|
42
43
|
nodeType: nodeType,
|
|
43
44
|
nodeNamespace: namespace,
|
|
44
45
|
type: NodeRefType.BoundNode,
|
|
@@ -46,16 +47,18 @@ var NodeRef;
|
|
|
46
47
|
parent: null,
|
|
47
48
|
childNodes: null,
|
|
48
49
|
destroyed: false,
|
|
49
|
-
destroyables: [],
|
|
50
|
-
lastProperties: null,
|
|
51
50
|
lastEvents: null,
|
|
52
51
|
setProperties: false,
|
|
52
|
+
assignProperties: null,
|
|
53
|
+
assignEvents: null,
|
|
53
54
|
setAttributes: false,
|
|
54
|
-
setEvents: false
|
|
55
|
+
setEvents: false,
|
|
56
|
+
scopes: null
|
|
55
57
|
};
|
|
56
58
|
case NodeRefType.ElementNode:
|
|
57
59
|
return {
|
|
58
60
|
node: null,
|
|
61
|
+
nodeDef: null,
|
|
59
62
|
nodeType: nodeType,
|
|
60
63
|
nodeNamespace: namespace,
|
|
61
64
|
type: NodeRefType.ElementNode,
|
|
@@ -63,19 +66,21 @@ var NodeRef;
|
|
|
63
66
|
parent: null,
|
|
64
67
|
childNodes: null,
|
|
65
68
|
destroyed: false,
|
|
66
|
-
destroyables: [],
|
|
67
|
-
lastProperties: null,
|
|
68
69
|
lastEvents: null,
|
|
69
70
|
setProperties: false,
|
|
71
|
+
assignProperties: null,
|
|
72
|
+
assignEvents: null,
|
|
70
73
|
setAttributes: false,
|
|
71
74
|
setEvents: false,
|
|
72
75
|
childrenFunc: null,
|
|
73
76
|
nodeList: null,
|
|
74
|
-
setData: false
|
|
77
|
+
setData: false,
|
|
78
|
+
scopes: null
|
|
75
79
|
};
|
|
76
80
|
case NodeRefType.ComponentNode:
|
|
77
81
|
return {
|
|
78
82
|
node: null,
|
|
83
|
+
nodeDef: null,
|
|
79
84
|
nodeType: nodeType,
|
|
80
85
|
nodeNamespace: namespace,
|
|
81
86
|
type: NodeRefType.ComponentNode,
|
|
@@ -83,14 +88,14 @@ var NodeRef;
|
|
|
83
88
|
parent: null,
|
|
84
89
|
childNodes: null,
|
|
85
90
|
destroyed: false,
|
|
86
|
-
destroyables: [],
|
|
87
|
-
lastProperties: null,
|
|
88
|
-
lastEvents: null,
|
|
89
91
|
setProperties: false,
|
|
92
|
+
assignProperties: null,
|
|
93
|
+
assignEvents: null,
|
|
90
94
|
setAttributes: false,
|
|
91
95
|
setEvents: false,
|
|
92
96
|
component: null,
|
|
93
|
-
componentEvents: null
|
|
97
|
+
componentEvents: null,
|
|
98
|
+
scopes: null
|
|
94
99
|
};
|
|
95
100
|
}
|
|
96
101
|
}
|
|
@@ -98,8 +103,8 @@ var NodeRef;
|
|
|
98
103
|
function Init(nodeRef) {
|
|
99
104
|
if (nodeRef.node)
|
|
100
105
|
return;
|
|
101
|
-
nodeRef.node = nodeConfig_1.NodeConfig.createNode(nodeRef.nodeType, nodeRef.nodeNamespace);
|
|
102
|
-
nodeRef.childNodes =
|
|
106
|
+
nodeRef.node = nodeRef.nodeType === 'text' ? nodeConfig_1.NodeConfig.createTextNode() : nodeConfig_1.NodeConfig.createNode(nodeRef.nodeType, nodeRef.nodeNamespace);
|
|
107
|
+
nodeRef.childNodes = nodeRef.nodeType !== 'text' ? [] : null;
|
|
103
108
|
switch (nodeRef.type) {
|
|
104
109
|
case NodeRefType.BoundNode:
|
|
105
110
|
boundNode_1.BoundNode.Init(nodeRef);
|
|
@@ -113,17 +118,23 @@ var NodeRef;
|
|
|
113
118
|
}
|
|
114
119
|
}
|
|
115
120
|
NodeRef.Init = Init;
|
|
121
|
+
function AddChildToNode(parentNode, child) {
|
|
122
|
+
if (Array.isArray(parentNode.childNodes))
|
|
123
|
+
parentNode.childNodes.push(child);
|
|
124
|
+
else
|
|
125
|
+
parentNode.childNodes.add(child);
|
|
126
|
+
}
|
|
116
127
|
function InitAll(parentNode, nodeRefs) {
|
|
117
128
|
for (var x = 0; x < nodeRefs.length; x++) {
|
|
118
129
|
nodeRefs[x].parent = parentNode;
|
|
119
|
-
parentNode
|
|
130
|
+
AddChildToNode(parentNode, nodeRefs[x]);
|
|
120
131
|
Init(nodeRefs[x]);
|
|
121
132
|
}
|
|
122
133
|
}
|
|
123
134
|
NodeRef.InitAll = InitAll;
|
|
124
135
|
function AddChild(node, child) {
|
|
125
136
|
child.parent = node;
|
|
126
|
-
node
|
|
137
|
+
AddChildToNode(node, child);
|
|
127
138
|
nodeConfig_1.NodeConfig.addChild(node.node, child.node);
|
|
128
139
|
}
|
|
129
140
|
NodeRef.AddChild = AddChild;
|
|
@@ -131,7 +142,7 @@ var NodeRef;
|
|
|
131
142
|
if (currentChild && currentChild.parent !== node)
|
|
132
143
|
throw "currentChild is not valid";
|
|
133
144
|
newChild.parent = node;
|
|
134
|
-
node
|
|
145
|
+
AddChildToNode(node, newChild);
|
|
135
146
|
nodeConfig_1.NodeConfig.addChildAfter(node.node, currentChild && currentChild.node, newChild.node);
|
|
136
147
|
}
|
|
137
148
|
NodeRef.AddChildAfter = AddChildAfter;
|
|
@@ -143,6 +154,8 @@ var NodeRef;
|
|
|
143
154
|
}
|
|
144
155
|
let priorNode;
|
|
145
156
|
let curDataNode = nextChildren?.head;
|
|
157
|
+
let insert = false;
|
|
158
|
+
let remove = false;
|
|
146
159
|
while (curDataNode) {
|
|
147
160
|
for (let x = 0; x < curDataNode.data.nodes.length; x++) {
|
|
148
161
|
const actualNode = priorNode ? nodeConfig_1.NodeConfig.getNextSibling(priorNode) : nodeConfig_1.NodeConfig.getFirstChild(rootNode);
|
|
@@ -150,20 +163,27 @@ var NodeRef;
|
|
|
150
163
|
const expectedNode = virtualNode.node;
|
|
151
164
|
if (actualNode !== expectedNode) {
|
|
152
165
|
nodeConfig_1.NodeConfig.addChildBefore(rootNode, actualNode, expectedNode);
|
|
166
|
+
!remove && insert && actualNode && nodeConfig_1.NodeConfig.removeChild(rootNode, actualNode);
|
|
167
|
+
remove = insert;
|
|
168
|
+
insert = true;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
insert = false;
|
|
172
|
+
remove = false;
|
|
153
173
|
}
|
|
154
174
|
priorNode = expectedNode;
|
|
155
175
|
}
|
|
156
176
|
curDataNode = curDataNode.next;
|
|
157
177
|
}
|
|
158
|
-
let
|
|
159
|
-
while (
|
|
160
|
-
nodeConfig_1.NodeConfig.removeChild(rootNode,
|
|
161
|
-
|
|
178
|
+
let lastChild = nodeConfig_1.NodeConfig.getLastChild(rootNode);
|
|
179
|
+
while (priorNode && priorNode !== lastChild) {
|
|
180
|
+
nodeConfig_1.NodeConfig.removeChild(rootNode, lastChild);
|
|
181
|
+
lastChild = nodeConfig_1.NodeConfig.getLastChild(rootNode);
|
|
162
182
|
}
|
|
163
183
|
}
|
|
164
184
|
NodeRef.ReconcileChildren = ReconcileChildren;
|
|
165
185
|
function DetachChild(node, child) {
|
|
166
|
-
if (node.childNodes.delete(child)) {
|
|
186
|
+
if (!Array.isArray(node.childNodes) && node.childNodes.delete(child)) {
|
|
167
187
|
nodeConfig_1.NodeConfig.removeChild(node.node, child.node);
|
|
168
188
|
child.parent = null;
|
|
169
189
|
}
|
|
@@ -173,13 +193,25 @@ var NodeRef;
|
|
|
173
193
|
if (node.destroyed)
|
|
174
194
|
return;
|
|
175
195
|
node.destroyed = true;
|
|
176
|
-
node.childNodes
|
|
177
|
-
|
|
178
|
-
|
|
196
|
+
if (Array.isArray(node.childNodes))
|
|
197
|
+
for (let x = 0; x < node.childNodes.length; x++)
|
|
198
|
+
Destroy(node.childNodes[x]);
|
|
199
|
+
else
|
|
200
|
+
node.childNodes?.forEach(Destroy);
|
|
201
|
+
switch (node.type) {
|
|
202
|
+
case NodeRefType.ComponentNode:
|
|
203
|
+
node.component?.Destroy();
|
|
204
|
+
case NodeRefType.ElementNode:
|
|
205
|
+
node.assignEvents?.(null);
|
|
206
|
+
case NodeRefType.BoundNode:
|
|
207
|
+
for (let x = 0; node.scopes && x < node.scopes.length; x++)
|
|
208
|
+
Store_1.ObservableScope.Destroy(node.scopes[x]);
|
|
209
|
+
}
|
|
210
|
+
node.node = null;
|
|
179
211
|
}
|
|
180
212
|
NodeRef.Destroy = Destroy;
|
|
181
213
|
function DestroyAll(nodes) {
|
|
182
|
-
for (
|
|
214
|
+
for (let x = 0; x < nodes.length; x++)
|
|
183
215
|
Destroy(nodes[x]);
|
|
184
216
|
}
|
|
185
217
|
NodeRef.DestroyAll = DestroyAll;
|
package/Node/nodeRef.types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Injector } from "../Utils/injector";
|
|
2
|
-
import { IDestroyable } from "../Utils/utils.types";
|
|
3
2
|
import { IBoundNode } from "./boundNode.types";
|
|
4
3
|
import { IComponentNode } from "./componentNode.types";
|
|
5
4
|
import { IElementNode } from "./elementNode.types";
|
|
@@ -11,11 +10,10 @@ export interface INodeRefBase {
|
|
|
11
10
|
nodeNamespace: string;
|
|
12
11
|
injector: Injector;
|
|
13
12
|
parent: INodeRefBase;
|
|
14
|
-
childNodes: Set<INodeRefBase>;
|
|
13
|
+
childNodes: INodeRefBase[] | Set<INodeRefBase>;
|
|
15
14
|
destroyed: boolean;
|
|
16
|
-
destroyables: IDestroyable[];
|
|
17
15
|
}
|
|
18
16
|
export interface INodeRef extends INodeRefBase {
|
|
19
17
|
type: NodeRefType.NodeRef;
|
|
20
18
|
}
|
|
21
|
-
export type NodeRefTypes = INodeRef | IBoundNode | IElementNode<
|
|
19
|
+
export type NodeRefTypes = INodeRef | IBoundNode | IElementNode<unknown> | IComponentNode<unknown, unknown, unknown>;
|
package/Store/Diff/diffTree.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DiffTreeScope =
|
|
3
|
+
exports.DiffTreeScope = DiffTreeScope;
|
|
4
4
|
function DiffTreeScope(worker) {
|
|
5
5
|
const ctx = this;
|
|
6
6
|
if (ctx && worker) {
|
|
@@ -229,4 +229,3 @@ function DiffTreeScope(worker) {
|
|
|
229
229
|
}
|
|
230
230
|
return DiffTree;
|
|
231
231
|
}
|
|
232
|
-
exports.DiffTreeScope = DiffTreeScope;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObservableComputed = void 0;
|
|
4
|
+
const json_1 = require("../../Utils/json");
|
|
5
|
+
const list_1 = require("../../Utils/list");
|
|
6
|
+
const observableNode_1 = require("./observableNode");
|
|
7
|
+
const observableScope_1 = require("./observableScope");
|
|
8
|
+
const scheduleNodes = new Set();
|
|
9
|
+
const computedQueue = list_1.List.Create();
|
|
10
|
+
function updateNode({ node, scope }) {
|
|
11
|
+
const value = observableScope_1.ObservableScope.Value(scope);
|
|
12
|
+
node.data = (0, json_1.JsonDeepClone)(value);
|
|
13
|
+
}
|
|
14
|
+
let computing = false;
|
|
15
|
+
function scheduleComputed(node, scope) {
|
|
16
|
+
if (scheduleNodes.has(node))
|
|
17
|
+
return;
|
|
18
|
+
scheduleNodes.add(node);
|
|
19
|
+
list_1.List.Add(computedQueue, { node, scope });
|
|
20
|
+
if (!computing) {
|
|
21
|
+
computing = true;
|
|
22
|
+
queueMicrotask(function () {
|
|
23
|
+
computing = false;
|
|
24
|
+
scheduleNodes.clear();
|
|
25
|
+
list_1.List.ForEach(computedQueue, updateNode);
|
|
26
|
+
list_1.List.Clear(computedQueue);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function CreateComputed(compute) {
|
|
31
|
+
const scope = observableScope_1.ObservableScope.Create(compute);
|
|
32
|
+
const node = observableNode_1.ObservableNode.Create({ data: null });
|
|
33
|
+
observableScope_1.ObservableScope.Watch(scope, function (scope) {
|
|
34
|
+
scheduleComputed(node, scope);
|
|
35
|
+
});
|
|
36
|
+
node.data = observableScope_1.ObservableScope.Value(scope);
|
|
37
|
+
return observableScope_1.ObservableScope.Create(function () { return node.data; }, [scope]);
|
|
38
|
+
}
|
|
39
|
+
var ObservableComputed;
|
|
40
|
+
(function (ObservableComputed) {
|
|
41
|
+
function Create(compute) {
|
|
42
|
+
return CreateComputed(compute);
|
|
43
|
+
}
|
|
44
|
+
ObservableComputed.Create = Create;
|
|
45
|
+
})(ObservableComputed || (exports.ObservableComputed = ObservableComputed = {}));
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObservableNode = void 0;
|
|
4
|
+
const json_1 = require("../../Utils/json");
|
|
5
|
+
const observableScope_1 = require("./observableScope");
|
|
6
|
+
const proxyCache = new WeakMap();
|
|
7
|
+
const scopeCache = new WeakMap();
|
|
8
|
+
function getOwnPropertyDescriptor(target, prop) {
|
|
9
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
10
|
+
return {
|
|
11
|
+
...descriptor,
|
|
12
|
+
configurable: true
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function getOwnPropertyDescriptorArray(target, prop) {
|
|
16
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
17
|
+
return {
|
|
18
|
+
...descriptor,
|
|
19
|
+
configurable: true
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function has(value, prop) {
|
|
23
|
+
return Object.hasOwn(value, prop);
|
|
24
|
+
}
|
|
25
|
+
function hasArray(value, prop) {
|
|
26
|
+
return Object.hasOwn(value, prop);
|
|
27
|
+
}
|
|
28
|
+
function ownKeys(value) {
|
|
29
|
+
return Object.keys(value);
|
|
30
|
+
}
|
|
31
|
+
function ownKeysArray(value) {
|
|
32
|
+
return Object.keys(value);
|
|
33
|
+
}
|
|
34
|
+
function UnwrapProxy(value) {
|
|
35
|
+
if (!value)
|
|
36
|
+
return value;
|
|
37
|
+
if (value.toJSON && typeof value.toJSON === 'function')
|
|
38
|
+
return value.toJSON();
|
|
39
|
+
const type = (0, json_1.JsonType)(value);
|
|
40
|
+
switch (type) {
|
|
41
|
+
case 'object': {
|
|
42
|
+
const keys = Object.keys(value);
|
|
43
|
+
for (let x = 0; x < keys.length; x++)
|
|
44
|
+
value[keys[x]] = UnwrapProxy(value[keys[x]]);
|
|
45
|
+
}
|
|
46
|
+
case 'array': {
|
|
47
|
+
for (let x = 0; x < value.length; x++)
|
|
48
|
+
value[x] = UnwrapProxy(value[x]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
function CreateArrayProxy(value) {
|
|
54
|
+
const scope = observableScope_1.ObservableScope.Create(() => value);
|
|
55
|
+
const proxy = new Proxy(value, {
|
|
56
|
+
get: ArrayProxyGetter,
|
|
57
|
+
set: ArrayProxySetter,
|
|
58
|
+
has: hasArray,
|
|
59
|
+
ownKeys: ownKeysArray,
|
|
60
|
+
getOwnPropertyDescriptor: getOwnPropertyDescriptorArray
|
|
61
|
+
});
|
|
62
|
+
scopeCache.set(value, scope);
|
|
63
|
+
proxyCache.set(value, proxy);
|
|
64
|
+
return proxy;
|
|
65
|
+
}
|
|
66
|
+
function CreateObjectProxy(value) {
|
|
67
|
+
const scope = observableScope_1.ObservableScope.Create(() => value);
|
|
68
|
+
const proxy = new Proxy(value, {
|
|
69
|
+
get: ObjectProxyGetter,
|
|
70
|
+
set: ObjectProxySetter,
|
|
71
|
+
has,
|
|
72
|
+
ownKeys,
|
|
73
|
+
getOwnPropertyDescriptor
|
|
74
|
+
});
|
|
75
|
+
scopeCache.set(value, scope);
|
|
76
|
+
proxyCache.set(value, proxy);
|
|
77
|
+
return proxy;
|
|
78
|
+
}
|
|
79
|
+
function ArrayProxySetter(array, prop, value) {
|
|
80
|
+
value = UnwrapProxy(value);
|
|
81
|
+
array[prop] = value;
|
|
82
|
+
const scope = scopeCache.get(array);
|
|
83
|
+
observableScope_1.ObservableScope.Update(scope);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
function ArrayProxyGetter(array, prop) {
|
|
87
|
+
const scope = scopeCache.get(array);
|
|
88
|
+
array = observableScope_1.ObservableScope.Value(scope);
|
|
89
|
+
switch (prop) {
|
|
90
|
+
case "toJSON":
|
|
91
|
+
return function () {
|
|
92
|
+
return array;
|
|
93
|
+
};
|
|
94
|
+
default: {
|
|
95
|
+
const arrayValue = array[prop];
|
|
96
|
+
if (typeof prop === 'symbol')
|
|
97
|
+
return arrayValue;
|
|
98
|
+
if (typeof arrayValue === 'function')
|
|
99
|
+
return function ArrayFunction(...args) {
|
|
100
|
+
const proxyArray = array.slice();
|
|
101
|
+
for (let x = 0; x < proxyArray.length; x++)
|
|
102
|
+
proxyArray[x] = proxyCache.get(proxyArray[x]) ?? CreateProxy(proxyArray[x]);
|
|
103
|
+
let result = proxyArray[prop](...args);
|
|
104
|
+
switch (prop) {
|
|
105
|
+
case 'push':
|
|
106
|
+
case 'unshift':
|
|
107
|
+
case 'splice':
|
|
108
|
+
case 'pop':
|
|
109
|
+
case 'shift':
|
|
110
|
+
case 'sort':
|
|
111
|
+
case 'reverse':
|
|
112
|
+
array.length = proxyArray.length;
|
|
113
|
+
for (let x = 0; x < proxyArray.length; x++)
|
|
114
|
+
array[x] = UnwrapProxy(proxyArray[x]);
|
|
115
|
+
observableScope_1.ObservableScope.Update(scope);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
const proxy = CreateProxy(arrayValue);
|
|
121
|
+
return proxy;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
let applyingDiff = false;
|
|
126
|
+
function ObjectProxySetter(object, prop, value) {
|
|
127
|
+
const scope = scopeCache.get(object);
|
|
128
|
+
value = UnwrapProxy(value);
|
|
129
|
+
if (applyingDiff) {
|
|
130
|
+
object[prop] = value;
|
|
131
|
+
observableScope_1.ObservableScope.Update(scope);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
applyingDiff = true;
|
|
135
|
+
const proxy = proxyCache.get(object);
|
|
136
|
+
const json = proxy.toJSON();
|
|
137
|
+
const diff = (0, json_1.JsonDiff)(value, json[prop]);
|
|
138
|
+
for (let x = 0; x < diff.length; x++) {
|
|
139
|
+
if (diff[x].path.length === 0) {
|
|
140
|
+
proxy[prop] = diff[x].value;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const path = diff[x].path;
|
|
144
|
+
let curr = proxy[prop];
|
|
145
|
+
let y = 0;
|
|
146
|
+
for (; y < path.length - 1; y++)
|
|
147
|
+
curr = curr[path[y]];
|
|
148
|
+
curr[path[y]] = diff[x].value;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
applyingDiff = false;
|
|
152
|
+
}
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
function ObjectProxyGetter(object, prop) {
|
|
156
|
+
const scope = scopeCache.get(object);
|
|
157
|
+
object = observableScope_1.ObservableScope.Value(scope);
|
|
158
|
+
switch (prop) {
|
|
159
|
+
case "toJSON":
|
|
160
|
+
return function () {
|
|
161
|
+
return object;
|
|
162
|
+
};
|
|
163
|
+
default: {
|
|
164
|
+
const proxyValue = object[prop];
|
|
165
|
+
if (typeof prop === 'symbol')
|
|
166
|
+
return proxyValue;
|
|
167
|
+
const proxy = CreateProxy(proxyValue);
|
|
168
|
+
return proxy;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function CreateProxy(value) {
|
|
173
|
+
const type = (0, json_1.JsonType)(value);
|
|
174
|
+
switch (type) {
|
|
175
|
+
case 'object': {
|
|
176
|
+
const proxy = proxyCache.get(value) ?? CreateObjectProxy(value);
|
|
177
|
+
return proxy;
|
|
178
|
+
}
|
|
179
|
+
case 'array': {
|
|
180
|
+
const proxy = proxyCache.get(value) ?? CreateArrayProxy(value);
|
|
181
|
+
observableScope_1.ObservableScope.Touch(scopeCache.get(value));
|
|
182
|
+
return proxy;
|
|
183
|
+
}
|
|
184
|
+
default:
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
var ObservableNode;
|
|
189
|
+
(function (ObservableNode) {
|
|
190
|
+
function Create(value) {
|
|
191
|
+
value = UnwrapProxy(value);
|
|
192
|
+
return CreateProxy(value);
|
|
193
|
+
}
|
|
194
|
+
ObservableNode.Create = Create;
|
|
195
|
+
})(ObservableNode || (exports.ObservableNode = ObservableNode = {}));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Emitter } from "../../Utils/emitter";
|
|
1
|
+
import { Emitter, EmitterCallback } from "../../Utils/emitter";
|
|
2
2
|
import { IDestroyable } from "../../Utils/utils.types";
|
|
3
3
|
export declare class ObservableScopeValue<T> {
|
|
4
4
|
protected scope: IObservableScope<T>;
|
|
@@ -33,25 +33,20 @@ export interface IObservableScope<T> extends IDestroyable {
|
|
|
33
33
|
dirty: boolean;
|
|
34
34
|
emitter: Emitter;
|
|
35
35
|
emitters: Set<Emitter>;
|
|
36
|
-
setCallback:
|
|
37
|
-
(): void;
|
|
38
|
-
};
|
|
36
|
+
setCallback: EmitterCallback;
|
|
39
37
|
destroyed: boolean;
|
|
38
|
+
dependencies?: IObservableScope<unknown>[];
|
|
40
39
|
}
|
|
41
40
|
export declare namespace ObservableScope {
|
|
42
41
|
function Create<T>(valueFunction: {
|
|
43
42
|
(): T | Promise<T>;
|
|
44
|
-
} | T): IObservableScope<T>;
|
|
43
|
+
} | T, dependencies?: IObservableScope<unknown>[]): IObservableScope<T>;
|
|
45
44
|
function Register(emitter: Emitter): void;
|
|
46
45
|
function Value<T>(scope: IObservableScope<T>): T;
|
|
47
46
|
function Watching(): boolean;
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function Unwatch<T>(scope: IObservableScope<T>, callback: {
|
|
52
|
-
(scope: IObservableScope<T> | ObservableScopeValue<T>): void;
|
|
53
|
-
}): void;
|
|
47
|
+
function Touch<T>(scope: IObservableScope<T>): void;
|
|
48
|
+
function Watch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T>]>): void;
|
|
49
|
+
function Unwatch<T>(scope: IObservableScope<T>, callback: EmitterCallback<[IObservableScope<T> | ObservableScopeValue<T>]>): void;
|
|
54
50
|
function Update(scope: IObservableScope<any>): void;
|
|
55
|
-
function Emit(scope: IObservableScope<any>): void;
|
|
56
51
|
function Destroy<T>(scope: IObservableScope<T>): void;
|
|
57
52
|
}
|
|
@@ -47,38 +47,34 @@ class ObservableScope extends ObservableScopeWrapper {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
exports.ObservableScope = ObservableScope;
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
let currentSet = null;
|
|
51
|
+
let watching = false;
|
|
52
52
|
function WatchAction(action) {
|
|
53
|
-
|
|
53
|
+
const parentSet = currentSet;
|
|
54
54
|
currentSet = null;
|
|
55
|
-
|
|
55
|
+
const parentWatching = watching;
|
|
56
56
|
watching = true;
|
|
57
57
|
action();
|
|
58
|
-
|
|
58
|
+
const lastSet = currentSet;
|
|
59
59
|
currentSet = parentSet;
|
|
60
60
|
watching = parentWatching;
|
|
61
61
|
return lastSet;
|
|
62
62
|
}
|
|
63
63
|
(function (ObservableScope) {
|
|
64
|
-
function Create(valueFunction) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
getFunction: valueFunction,
|
|
73
|
-
async: valueFunction[Symbol.toStringTag] === 'AsyncFunction',
|
|
74
|
-
value: null,
|
|
75
|
-
dirty: true,
|
|
76
|
-
emitter: emitter_1.Emitter.Create(),
|
|
64
|
+
function Create(valueFunction, dependencies) {
|
|
65
|
+
const hasFunction = typeof valueFunction === 'function';
|
|
66
|
+
const scope = {
|
|
67
|
+
getFunction: hasFunction ? valueFunction : null,
|
|
68
|
+
value: hasFunction ? null : valueFunction,
|
|
69
|
+
async: hasFunction ? valueFunction[Symbol.toStringTag] === 'AsyncFunction' : false,
|
|
70
|
+
dirty: hasFunction,
|
|
71
|
+
emitter: hasFunction ? emitter_1.Emitter.Create() : null,
|
|
77
72
|
emitters: null,
|
|
78
73
|
destroyed: false,
|
|
79
|
-
setCallback: function () {
|
|
80
|
-
OnSet(scope);
|
|
81
|
-
}
|
|
74
|
+
setCallback: hasFunction ? function () {
|
|
75
|
+
return OnSet(scope);
|
|
76
|
+
} : null,
|
|
77
|
+
dependencies
|
|
82
78
|
};
|
|
83
79
|
return scope;
|
|
84
80
|
}
|
|
@@ -86,7 +82,7 @@ function WatchAction(action) {
|
|
|
86
82
|
function Register(emitter) {
|
|
87
83
|
if (!watching || !emitter)
|
|
88
84
|
return;
|
|
89
|
-
currentSet
|
|
85
|
+
currentSet ??= new Set();
|
|
90
86
|
currentSet.add(emitter);
|
|
91
87
|
}
|
|
92
88
|
ObservableScope.Register = Register;
|
|
@@ -102,6 +98,12 @@ function WatchAction(action) {
|
|
|
102
98
|
return watching;
|
|
103
99
|
}
|
|
104
100
|
ObservableScope.Watching = Watching;
|
|
101
|
+
function Touch(scope) {
|
|
102
|
+
if (!scope || !scope.emitter)
|
|
103
|
+
return;
|
|
104
|
+
Register(scope.emitter);
|
|
105
|
+
}
|
|
106
|
+
ObservableScope.Touch = Touch;
|
|
105
107
|
function Watch(scope, callback) {
|
|
106
108
|
if (!scope || !scope.emitter)
|
|
107
109
|
return;
|
|
@@ -118,27 +120,24 @@ function WatchAction(action) {
|
|
|
118
120
|
OnSet(scope);
|
|
119
121
|
}
|
|
120
122
|
ObservableScope.Update = Update;
|
|
121
|
-
function Emit(scope) {
|
|
122
|
-
emitter_1.Emitter.Emit(scope.emitter);
|
|
123
|
-
}
|
|
124
|
-
ObservableScope.Emit = Emit;
|
|
125
123
|
function Destroy(scope) {
|
|
126
124
|
DestroyScope(scope);
|
|
127
125
|
}
|
|
128
126
|
ObservableScope.Destroy = Destroy;
|
|
129
127
|
})(ObservableScope || (exports.ObservableScope = ObservableScope = {}));
|
|
130
128
|
function OnSet(scope) {
|
|
131
|
-
if (!scope || scope.dirty)
|
|
132
|
-
return;
|
|
133
|
-
scope.dirty =
|
|
129
|
+
if (!scope || scope.dirty || scope.destroyed)
|
|
130
|
+
return scope.destroyed;
|
|
131
|
+
scope.dirty = !!scope.getFunction;
|
|
134
132
|
emitter_1.Emitter.Emit(scope.emitter, scope);
|
|
133
|
+
return false;
|
|
135
134
|
}
|
|
136
135
|
function UpdateValue(scope) {
|
|
137
136
|
if (!scope.dirty)
|
|
138
137
|
return;
|
|
139
138
|
scope.dirty = false;
|
|
140
|
-
|
|
141
|
-
|
|
139
|
+
let value = null;
|
|
140
|
+
const emitters = scope.getFunction && WatchAction(() => value = scope.getFunction());
|
|
142
141
|
if (scope.async)
|
|
143
142
|
Promise.resolve(value).then(val => {
|
|
144
143
|
scope.value = val;
|
|
@@ -151,17 +150,22 @@ function UpdateValue(scope) {
|
|
|
151
150
|
function DestroyScope(scope) {
|
|
152
151
|
if (!scope)
|
|
153
152
|
return;
|
|
154
|
-
|
|
153
|
+
if (scope.dependencies !== undefined)
|
|
154
|
+
for (let x = 0; x < scope.dependencies.length; x++)
|
|
155
|
+
DestroyScope(scope.dependencies[x]);
|
|
155
156
|
scope.emitters && scope.emitters.clear();
|
|
156
157
|
scope.emitter && scope.emitter.clear();
|
|
158
|
+
scope.getFunction = null;
|
|
159
|
+
scope.setCallback = null;
|
|
157
160
|
scope.destroyed = true;
|
|
158
161
|
}
|
|
159
162
|
function UpdateEmitters(scope, newEmitters) {
|
|
160
|
-
if (newEmitters)
|
|
163
|
+
if (newEmitters) {
|
|
161
164
|
newEmitters.forEach(e => {
|
|
162
|
-
if (!scope.emitters
|
|
165
|
+
if (!scope.emitters?.delete(e))
|
|
163
166
|
emitter_1.Emitter.On(e, scope.setCallback);
|
|
164
167
|
});
|
|
168
|
+
}
|
|
165
169
|
if (scope.emitters)
|
|
166
170
|
scope.emitters.forEach(e => emitter_1.Emitter.Remove(e, scope.setCallback));
|
|
167
171
|
scope.emitters = newEmitters;
|
package/Store/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export { Store } from "./Store/store";
|
|
|
2
2
|
export { StoreSync } from "./Store/storeSync";
|
|
3
3
|
export { StoreAsync } from "./Store/storeAsync";
|
|
4
4
|
export { ObservableScope } from "./Tree/observableScope";
|
|
5
|
+
export { ObservableNode } from "./Tree/observableNode";
|
|
6
|
+
export { ObservableComputed } from './Tree/observableComputed';
|