j-templates 5.0.53 → 6.0.0
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/component.d.ts +0 -2
- package/Node/component.js +2 -8
- package/Store/Diff/diffAsync.d.ts +9 -8
- package/Store/Diff/diffAsync.js +1 -15
- package/Store/Diff/diffSync.d.ts +4 -6
- package/Store/Diff/diffSync.js +4 -10
- package/Store/Diff/diffTree.d.ts +4 -10
- package/Store/Diff/diffTree.js +96 -189
- package/Store/Diff/diffWorker.js +5 -4
- package/Store/Store/store.d.ts +0 -14
- package/Store/Store/store.js +0 -31
- package/Store/Store/storeAsync.d.ts +0 -20
- package/Store/Store/storeAsync.js +0 -49
- package/Store/Store/storeAsyncWriter.d.ts +0 -15
- package/Store/Store/storeAsyncWriter.js +0 -52
- package/Store/Store/storeSync.d.ts +0 -15
- package/Store/Store/storeSync.js +0 -33
- package/Store/Store/storeSyncWriter.d.ts +0 -12
- package/Store/Store/storeSyncWriter.js +0 -41
- package/Store/Store/storeWriter.d.ts +0 -9
- package/Store/Store/storeWriter.js +0 -30
- package/Store/Tree/observableComputed.d.ts +0 -4
- package/Store/Tree/observableComputed.js +0 -45
- package/Store/Tree/observableNode.d.ts +2 -0
- package/Store/Tree/observableNode.js +186 -131
- package/Store/Tree/observableScope.js +16 -12
- package/Store/index.d.ts +0 -4
- package/Store/index.js +1 -9
- package/Utils/array.d.ts +1 -0
- package/Utils/array.js +15 -0
- package/Utils/decorators.d.ts +8 -8
- package/Utils/decorators.js +77 -165
- package/Utils/emitter.d.ts +2 -1
- package/Utils/emitter.js +20 -11
- package/Utils/json.d.ts +8 -3
- package/Utils/json.js +116 -96
- package/Utils/jsonDeepClone.d.ts +1 -0
- package/Utils/jsonDeepClone.js +20 -0
- package/Utils/router.d.ts +0 -23
- package/Utils/router.js +0 -116
- package/jTemplates.js +1 -1
- package/jTemplates.js.map +1 -1
- package/package.json +1 -1
- package/web.export.d.ts +1 -1
- package/web.export.js +1 -4
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { DiffAsync } from "../Diff/diffAsync";
|
|
2
|
-
import { ObservableTree } from "../Tree/observableTree";
|
|
3
|
-
export declare class StoreAsyncWriter {
|
|
4
|
-
private idFunc;
|
|
5
|
-
private diffAsync;
|
|
6
|
-
private observableTree;
|
|
7
|
-
constructor(idFunc: {
|
|
8
|
-
(val: any): string;
|
|
9
|
-
}, diffAsync: DiffAsync, observableTree: ObservableTree);
|
|
10
|
-
Write<T>(source: T, data: any): Promise<void>;
|
|
11
|
-
Merge<T>(source: T, data: Partial<T>): Promise<void>;
|
|
12
|
-
Push<T>(source: Array<T>, data: T): Promise<void>;
|
|
13
|
-
Splice<T>(source: Array<T>, start: number, deleteCount?: number, ...items: Array<T>): Promise<void>;
|
|
14
|
-
private ApplyChanges;
|
|
15
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StoreAsyncWriter = void 0;
|
|
4
|
-
class StoreAsyncWriter {
|
|
5
|
-
idFunc;
|
|
6
|
-
diffAsync;
|
|
7
|
-
observableTree;
|
|
8
|
-
constructor(idFunc, diffAsync, observableTree) {
|
|
9
|
-
this.idFunc = idFunc;
|
|
10
|
-
this.diffAsync = diffAsync;
|
|
11
|
-
this.observableTree = observableTree;
|
|
12
|
-
}
|
|
13
|
-
async Write(source, data) {
|
|
14
|
-
let path;
|
|
15
|
-
if (source) {
|
|
16
|
-
path = this.observableTree.GetPathOf(source);
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
path = this.idFunc(data);
|
|
20
|
-
if (!path)
|
|
21
|
-
throw new Error("data must have an id");
|
|
22
|
-
}
|
|
23
|
-
let diff = await this.diffAsync.DiffPath(path, data);
|
|
24
|
-
this.ApplyChanges(diff);
|
|
25
|
-
}
|
|
26
|
-
async Merge(source, data) {
|
|
27
|
-
const rootPath = this.observableTree.GetPathOf(source);
|
|
28
|
-
const keys = Object.keys(data);
|
|
29
|
-
const message = keys.map(key => ({ path: `${rootPath}.${key}`, value: data[key] }));
|
|
30
|
-
const diff = await this.diffAsync.DiffBatch(message);
|
|
31
|
-
this.ApplyChanges(diff);
|
|
32
|
-
}
|
|
33
|
-
async Push(source, data) {
|
|
34
|
-
const rootPath = this.observableTree.GetPathOf(source);
|
|
35
|
-
var lengthPath = `${rootPath}.length`;
|
|
36
|
-
var length = await this.diffAsync.GetPath(lengthPath);
|
|
37
|
-
var diff = await this.diffAsync.DiffPath(`${rootPath}.${length}`, data);
|
|
38
|
-
this.ApplyChanges(diff);
|
|
39
|
-
}
|
|
40
|
-
async Splice(source, start, deleteCount, ...items) {
|
|
41
|
-
var rootPath = this.observableTree.GetPathOf(source);
|
|
42
|
-
var array = await this.diffAsync.GetPath(rootPath);
|
|
43
|
-
array = array.slice();
|
|
44
|
-
array.splice(start, deleteCount, ...items);
|
|
45
|
-
var diff = await this.diffAsync.DiffPath(rootPath, array);
|
|
46
|
-
this.ApplyChanges(diff);
|
|
47
|
-
}
|
|
48
|
-
ApplyChanges(diff) {
|
|
49
|
-
this.observableTree.WriteAll(diff);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
exports.StoreAsyncWriter = StoreAsyncWriter;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { StoreSyncWriter } from "./storeSyncWriter";
|
|
2
|
-
export declare class StoreSync<T> {
|
|
3
|
-
private diffSync;
|
|
4
|
-
private observableTree;
|
|
5
|
-
private storeWriter;
|
|
6
|
-
private rootScope;
|
|
7
|
-
get Root(): import("..").ObservableScope<T>;
|
|
8
|
-
constructor(init?: T);
|
|
9
|
-
Action(action: {
|
|
10
|
-
(root: T, writer: StoreSyncWriter): void;
|
|
11
|
-
}): void;
|
|
12
|
-
Write(data: T): void;
|
|
13
|
-
Merge(data: Partial<T>): void;
|
|
14
|
-
Destroy(): void;
|
|
15
|
-
}
|
package/Store/Store/storeSync.js
CHANGED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StoreSync = void 0;
|
|
4
|
-
const diffSync_1 = require("../Diff/diffSync");
|
|
5
|
-
const observableTree_1 = require("../Tree/observableTree");
|
|
6
|
-
const storeSyncWriter_1 = require("./storeSyncWriter");
|
|
7
|
-
class StoreSync {
|
|
8
|
-
diffSync = new diffSync_1.DiffSync();
|
|
9
|
-
observableTree = new observableTree_1.ObservableTree();
|
|
10
|
-
storeWriter = new storeSyncWriter_1.StoreSyncWriter(this.diffSync, this.observableTree);
|
|
11
|
-
rootScope = this.observableTree.Scope("ROOT", root => root);
|
|
12
|
-
get Root() {
|
|
13
|
-
return this.rootScope;
|
|
14
|
-
}
|
|
15
|
-
constructor(init) {
|
|
16
|
-
if (init)
|
|
17
|
-
this.Write(init);
|
|
18
|
-
}
|
|
19
|
-
Action(action) {
|
|
20
|
-
var proxy = this.observableTree.Get("ROOT");
|
|
21
|
-
action(proxy, this.storeWriter);
|
|
22
|
-
}
|
|
23
|
-
Write(data) {
|
|
24
|
-
this.Action((root, writer) => writer.Write(root, data));
|
|
25
|
-
}
|
|
26
|
-
Merge(data) {
|
|
27
|
-
this.Action((root, writer) => writer.Merge(root, data));
|
|
28
|
-
}
|
|
29
|
-
Destroy() {
|
|
30
|
-
this.rootScope.Destroy();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
exports.StoreSync = StoreSync;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { ObservableTree } from "../Tree/observableTree";
|
|
2
|
-
import { DiffSync } from "../Diff/diffSync";
|
|
3
|
-
export declare class StoreSyncWriter {
|
|
4
|
-
private diffSync;
|
|
5
|
-
private observableTree;
|
|
6
|
-
constructor(diffSync: DiffSync, observableTree: ObservableTree);
|
|
7
|
-
Write<T>(source: T, data: any): void;
|
|
8
|
-
Merge<T>(source: T, data: Partial<T>): void;
|
|
9
|
-
Push<T>(source: Array<T>, data: T): void;
|
|
10
|
-
Splice<T>(source: Array<T>, start: number, deleteCount?: number, ...items: Array<T>): void;
|
|
11
|
-
private ApplyChanges;
|
|
12
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StoreSyncWriter = void 0;
|
|
4
|
-
class StoreSyncWriter {
|
|
5
|
-
diffSync;
|
|
6
|
-
observableTree;
|
|
7
|
-
constructor(diffSync, observableTree) {
|
|
8
|
-
this.diffSync = diffSync;
|
|
9
|
-
this.observableTree = observableTree;
|
|
10
|
-
}
|
|
11
|
-
Write(source, data) {
|
|
12
|
-
var rootPath = source && this.observableTree.GetPathOf(source) || "ROOT";
|
|
13
|
-
var diff = this.diffSync.DiffPath(rootPath, data);
|
|
14
|
-
this.ApplyChanges(diff);
|
|
15
|
-
}
|
|
16
|
-
Merge(source, data) {
|
|
17
|
-
var rootPath = this.observableTree.GetPathOf(source);
|
|
18
|
-
var keys = Object.keys(data);
|
|
19
|
-
var message = keys.map(key => ({ path: `${rootPath}.${key}`, value: data[key] }));
|
|
20
|
-
var diff = this.diffSync.DiffBatch(message);
|
|
21
|
-
this.ApplyChanges(diff);
|
|
22
|
-
}
|
|
23
|
-
Push(source, data) {
|
|
24
|
-
var rootPath = this.observableTree.GetPathOf(source);
|
|
25
|
-
var length = source.length;
|
|
26
|
-
this.diffSync.UpdatePath(`${rootPath}.${length}`, data);
|
|
27
|
-
this.observableTree.Write(`${rootPath}.${length}`, data);
|
|
28
|
-
}
|
|
29
|
-
Splice(source, start, deleteCount, ...items) {
|
|
30
|
-
var rootPath = this.observableTree.GetPathOf(source);
|
|
31
|
-
var proxy = this.observableTree.Get(rootPath);
|
|
32
|
-
const array = proxy.toJSON().slice();
|
|
33
|
-
array.splice(start, deleteCount, ...items);
|
|
34
|
-
this.diffSync.UpdatePath(rootPath, array);
|
|
35
|
-
this.observableTree.Write(rootPath, array);
|
|
36
|
-
}
|
|
37
|
-
ApplyChanges(diff) {
|
|
38
|
-
this.observableTree.WriteAll(diff);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.StoreSyncWriter = StoreSyncWriter;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ObservableTree } from "../Tree/observableTree";
|
|
2
|
-
export declare class StoreWriter {
|
|
3
|
-
private observableTree;
|
|
4
|
-
constructor(observableTree: ObservableTree);
|
|
5
|
-
Write<T>(source: T, data: any): void;
|
|
6
|
-
Merge<T>(source: T, data: Partial<T>): void;
|
|
7
|
-
Push<T>(source: Array<T>, data: T): void;
|
|
8
|
-
Splice<T>(source: Array<T>, start: number, deleteCount?: number, ...items: Array<T>): void;
|
|
9
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StoreWriter = void 0;
|
|
4
|
-
class StoreWriter {
|
|
5
|
-
observableTree;
|
|
6
|
-
constructor(observableTree) {
|
|
7
|
-
this.observableTree = observableTree;
|
|
8
|
-
}
|
|
9
|
-
Write(source, data) {
|
|
10
|
-
const rootPath = source && this.observableTree.GetPathOf(source) || "ROOT";
|
|
11
|
-
this.observableTree.Write(rootPath, data);
|
|
12
|
-
}
|
|
13
|
-
Merge(source, data) {
|
|
14
|
-
const rootPath = this.observableTree.GetPathOf(source);
|
|
15
|
-
for (const key in data)
|
|
16
|
-
this.observableTree.Write(`${rootPath}.${key}`, data[key]);
|
|
17
|
-
}
|
|
18
|
-
Push(source, data) {
|
|
19
|
-
const rootPath = this.observableTree.GetPathOf(source);
|
|
20
|
-
this.observableTree.Write(`${rootPath}.${source.length}`, data);
|
|
21
|
-
}
|
|
22
|
-
Splice(source, start, deleteCount, ...items) {
|
|
23
|
-
const json = source.toJSON();
|
|
24
|
-
const copy = json.slice();
|
|
25
|
-
copy.splice(start, deleteCount, ...items);
|
|
26
|
-
const rootPath = this.observableTree.GetPathOf(source);
|
|
27
|
-
this.observableTree.Write(rootPath, copy);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.StoreWriter = StoreWriter;
|
|
@@ -1,45 +0,0 @@
|
|
|
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 = {}));
|
|
@@ -3,20 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ObservableNode = void 0;
|
|
4
4
|
const json_1 = require("../../Utils/json");
|
|
5
5
|
const observableScope_1 = require("./observableScope");
|
|
6
|
+
const IS_OBSERVABLE_NODE = "____observableNode";
|
|
6
7
|
const proxyCache = new WeakMap();
|
|
7
8
|
const scopeCache = new WeakMap();
|
|
8
9
|
function getOwnPropertyDescriptor(target, prop) {
|
|
9
10
|
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
10
11
|
return {
|
|
11
12
|
...descriptor,
|
|
12
|
-
configurable: true
|
|
13
|
+
configurable: true,
|
|
13
14
|
};
|
|
14
15
|
}
|
|
15
16
|
function getOwnPropertyDescriptorArray(target, prop) {
|
|
16
17
|
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
17
18
|
return {
|
|
18
19
|
...descriptor,
|
|
19
|
-
configurable: true
|
|
20
|
+
configurable: true,
|
|
20
21
|
};
|
|
21
22
|
}
|
|
22
23
|
function has(value, prop) {
|
|
@@ -32,164 +33,218 @@ function ownKeysArray(value) {
|
|
|
32
33
|
return Object.keys(value);
|
|
33
34
|
}
|
|
34
35
|
function UnwrapProxy(value) {
|
|
35
|
-
|
|
36
|
+
const type = (0, json_1.JsonType)(value);
|
|
37
|
+
if (type === "value")
|
|
36
38
|
return value;
|
|
37
|
-
if (value
|
|
39
|
+
if (value[IS_OBSERVABLE_NODE] === true)
|
|
38
40
|
return value.toJSON();
|
|
39
|
-
const type = (0, json_1.JsonType)(value);
|
|
40
41
|
switch (type) {
|
|
41
|
-
case
|
|
42
|
+
case "object": {
|
|
42
43
|
const keys = Object.keys(value);
|
|
43
44
|
for (let x = 0; x < keys.length; x++)
|
|
44
45
|
value[keys[x]] = UnwrapProxy(value[keys[x]]);
|
|
45
46
|
}
|
|
46
|
-
case
|
|
47
|
+
case "array": {
|
|
47
48
|
for (let x = 0; x < value.length; x++)
|
|
48
49
|
value[x] = UnwrapProxy(value[x]);
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
return value;
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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;
|
|
54
|
+
let applyingDiff = false;
|
|
55
|
+
function CreateProxyFactory(alias) {
|
|
56
|
+
function ToJsonCopy(value) {
|
|
57
|
+
const type = (0, json_1.JsonType)(value);
|
|
58
|
+
switch (type) {
|
|
59
|
+
case "array": {
|
|
60
|
+
const typedValue = value;
|
|
61
|
+
return typedValue.map(ToJsonCopy);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
case "object": {
|
|
65
|
+
const typedValue = alias(value) ?? value;
|
|
66
|
+
const keys = Object.keys(typedValue);
|
|
67
|
+
const copy = {};
|
|
68
|
+
for (let x = 0; x < keys.length; x++)
|
|
69
|
+
copy[keys[x]] = ToJsonCopy(typedValue[keys[x]]);
|
|
70
|
+
return copy;
|
|
71
|
+
}
|
|
72
|
+
default:
|
|
73
|
+
return value;
|
|
122
74
|
}
|
|
123
75
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
76
|
+
function ToJsonDefault(value) {
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
const ToJson = alias !== undefined ? ToJsonCopy : ToJsonDefault;
|
|
80
|
+
function CreateArrayProxy(value) {
|
|
81
|
+
const scope = observableScope_1.ObservableScope.Create(() => value);
|
|
82
|
+
const proxy = new Proxy(value, {
|
|
83
|
+
get: ArrayProxyGetter,
|
|
84
|
+
set: ArrayProxySetter,
|
|
85
|
+
has: hasArray,
|
|
86
|
+
ownKeys: ownKeysArray,
|
|
87
|
+
getOwnPropertyDescriptor: getOwnPropertyDescriptorArray,
|
|
88
|
+
});
|
|
89
|
+
scopeCache.set(value, scope);
|
|
90
|
+
proxyCache.set(value, proxy);
|
|
91
|
+
return proxy;
|
|
92
|
+
}
|
|
93
|
+
function CreateObjectProxy(value) {
|
|
94
|
+
const scope = observableScope_1.ObservableScope.Create(() => value);
|
|
95
|
+
const proxy = new Proxy(value, {
|
|
96
|
+
get: ObjectProxyGetter,
|
|
97
|
+
set: ObjectProxySetter,
|
|
98
|
+
has,
|
|
99
|
+
ownKeys,
|
|
100
|
+
getOwnPropertyDescriptor,
|
|
101
|
+
});
|
|
102
|
+
scopeCache.set(value, scope);
|
|
103
|
+
proxyCache.set(value, proxy);
|
|
104
|
+
return proxy;
|
|
105
|
+
}
|
|
106
|
+
function ArrayProxySetter(array, prop, value) {
|
|
107
|
+
if (prop === IS_OBSERVABLE_NODE)
|
|
108
|
+
throw `Cannot assign read-only property: ${IS_OBSERVABLE_NODE}`;
|
|
109
|
+
value = UnwrapProxy(value);
|
|
110
|
+
array[prop] = value;
|
|
111
|
+
const scope = scopeCache.get(array);
|
|
131
112
|
observableScope_1.ObservableScope.Update(scope);
|
|
113
|
+
return true;
|
|
132
114
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
115
|
+
function ArrayProxyGetter(array, prop) {
|
|
116
|
+
if (prop === IS_OBSERVABLE_NODE)
|
|
117
|
+
return true;
|
|
118
|
+
const scope = scopeCache.get(array);
|
|
119
|
+
array = observableScope_1.ObservableScope.Value(scope);
|
|
120
|
+
switch (prop) {
|
|
121
|
+
case "toJSON":
|
|
122
|
+
return function () {
|
|
123
|
+
return ToJson(array);
|
|
124
|
+
};
|
|
125
|
+
default: {
|
|
126
|
+
const arrayValue = array[prop];
|
|
127
|
+
if (typeof prop === "symbol")
|
|
128
|
+
return arrayValue;
|
|
129
|
+
if (typeof arrayValue === "function")
|
|
130
|
+
return function ArrayFunction(...args) {
|
|
131
|
+
const proxyArray = array.slice();
|
|
132
|
+
for (let x = 0; x < proxyArray.length; x++)
|
|
133
|
+
proxyArray[x] =
|
|
134
|
+
proxyCache.get(proxyArray[x]) ??
|
|
135
|
+
CreateProxyFromValue(proxyArray[x]);
|
|
136
|
+
let result = proxyArray[prop](...args);
|
|
137
|
+
switch (prop) {
|
|
138
|
+
case "push":
|
|
139
|
+
case "unshift":
|
|
140
|
+
case "splice":
|
|
141
|
+
case "pop":
|
|
142
|
+
case "shift":
|
|
143
|
+
case "sort":
|
|
144
|
+
case "reverse":
|
|
145
|
+
array.length = proxyArray.length;
|
|
146
|
+
for (let x = 0; x < proxyArray.length; x++)
|
|
147
|
+
array[x] = UnwrapProxy(proxyArray[x]);
|
|
148
|
+
observableScope_1.ObservableScope.Update(scope);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
};
|
|
153
|
+
const proxy = CreateProxyFromValue(arrayValue);
|
|
154
|
+
return proxy;
|
|
149
155
|
}
|
|
150
156
|
}
|
|
151
|
-
applyingDiff = false;
|
|
152
157
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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;
|
|
158
|
+
function ObjectProxySetter(object, prop, value) {
|
|
159
|
+
if (prop === IS_OBSERVABLE_NODE)
|
|
160
|
+
throw `Cannot assign read-only property: ${IS_OBSERVABLE_NODE}`;
|
|
161
|
+
const scope = scopeCache.get(object);
|
|
162
|
+
value = UnwrapProxy(value);
|
|
163
|
+
if (applyingDiff) {
|
|
164
|
+
object[prop] = value;
|
|
165
|
+
observableScope_1.ObservableScope.Update(scope);
|
|
169
166
|
}
|
|
167
|
+
else {
|
|
168
|
+
applyingDiff = true;
|
|
169
|
+
const diff = (0, json_1.JsonDiff)(value, object[prop]);
|
|
170
|
+
for (let x = 0; x < diff.length; x++) {
|
|
171
|
+
if (diff[x].path.length === 0) {
|
|
172
|
+
const proxy = proxyCache.get(object);
|
|
173
|
+
proxy[prop] = diff[x].value;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
const path = diff[x].path;
|
|
177
|
+
let curr = object[prop];
|
|
178
|
+
let y = 0;
|
|
179
|
+
for (; y < path.length - 1; y++)
|
|
180
|
+
curr = curr[path[y]];
|
|
181
|
+
const target = proxyCache.get(curr) ?? curr;
|
|
182
|
+
target[path[y]] = diff[x].value;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
applyingDiff = false;
|
|
186
|
+
}
|
|
187
|
+
return true;
|
|
170
188
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
189
|
+
function ObjectProxyGetter(object, prop) {
|
|
190
|
+
if (prop === IS_OBSERVABLE_NODE)
|
|
191
|
+
return true;
|
|
192
|
+
const scope = scopeCache.get(object);
|
|
193
|
+
object = observableScope_1.ObservableScope.Value(scope);
|
|
194
|
+
switch (prop) {
|
|
195
|
+
case "toJSON":
|
|
196
|
+
return function () {
|
|
197
|
+
return ToJson(object);
|
|
198
|
+
};
|
|
199
|
+
default: {
|
|
200
|
+
const proxyValue = object[prop];
|
|
201
|
+
if (typeof prop === "symbol")
|
|
202
|
+
return proxyValue;
|
|
203
|
+
const proxy = CreateProxyFromValue(proxyValue);
|
|
204
|
+
return proxy;
|
|
205
|
+
}
|
|
178
206
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
207
|
+
}
|
|
208
|
+
function CreateProxyFromValue(value) {
|
|
209
|
+
const type = (0, json_1.JsonType)(value);
|
|
210
|
+
switch (type) {
|
|
211
|
+
case "object": {
|
|
212
|
+
let proxy = proxyCache.get(value) ?? CreateObjectProxy(value);
|
|
213
|
+
if (alias !== undefined) {
|
|
214
|
+
const aliasValue = alias(proxy);
|
|
215
|
+
if (aliasValue !== undefined)
|
|
216
|
+
proxy = proxyCache.get(aliasValue) ?? CreateObjectProxy(aliasValue);
|
|
217
|
+
}
|
|
218
|
+
return proxy;
|
|
219
|
+
}
|
|
220
|
+
case "array": {
|
|
221
|
+
const proxy = proxyCache.get(value) ?? CreateArrayProxy(value);
|
|
222
|
+
observableScope_1.ObservableScope.Touch(scopeCache.get(value));
|
|
223
|
+
return proxy;
|
|
224
|
+
}
|
|
225
|
+
default:
|
|
226
|
+
return value;
|
|
183
227
|
}
|
|
184
|
-
default:
|
|
185
|
-
return value;
|
|
186
228
|
}
|
|
229
|
+
function CreateProxy(value) {
|
|
230
|
+
value = UnwrapProxy(value);
|
|
231
|
+
return CreateProxyFromValue(value);
|
|
232
|
+
}
|
|
233
|
+
return CreateProxy;
|
|
187
234
|
}
|
|
235
|
+
const DefaultCreateProxy = CreateProxyFactory();
|
|
188
236
|
var ObservableNode;
|
|
189
237
|
(function (ObservableNode) {
|
|
190
238
|
function Create(value) {
|
|
191
|
-
|
|
192
|
-
return CreateProxy(value);
|
|
239
|
+
return DefaultCreateProxy(value);
|
|
193
240
|
}
|
|
194
241
|
ObservableNode.Create = Create;
|
|
242
|
+
function EnableDiff(value) {
|
|
243
|
+
applyingDiff = value;
|
|
244
|
+
}
|
|
245
|
+
ObservableNode.EnableDiff = EnableDiff;
|
|
246
|
+
function CreateFactory(alias) {
|
|
247
|
+
return CreateProxyFactory(alias);
|
|
248
|
+
}
|
|
249
|
+
ObservableNode.CreateFactory = CreateFactory;
|
|
195
250
|
})(ObservableNode || (exports.ObservableNode = ObservableNode = {}));
|