j-templates 6.0.0 → 6.0.2
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/createPropertyAssignment.js +2 -2
- package/Node/nodeRef.types.d.ts +1 -1
- package/Store/Diff/diffSync.js +2 -1
- package/Store/Diff/diffTree.js +1 -3
- package/Store/Diff/workerQueue.js +2 -2
- package/Store/Store/store.d.ts +10 -0
- package/Store/Store/store.js +57 -0
- package/Store/Store/storeAsync.d.ts +11 -0
- package/Store/Store/storeAsync.js +65 -0
- package/Store/Store/storeSync.d.ts +9 -0
- package/Store/Store/storeSync.js +51 -0
- package/Store/Tree/observableNode.d.ts +3 -0
- package/Store/Tree/observableNode.js +47 -21
- package/Store/Tree/observableScope.d.ts +2 -3
- package/Store/Tree/observableScope.js +23 -16
- package/Store/index.d.ts +2 -0
- package/Store/index.js +5 -1
- package/Utils/array.js +2 -1
- package/Utils/decorators.js +10 -27
- package/Utils/json.d.ts +2 -3
- package/Utils/json.js +3 -7
- package/Utils/jsonDeepClone.js +2 -2
- package/Utils/jsonMerge.d.ts +1 -0
- package/Utils/jsonMerge.js +32 -0
- package/Utils/jsonType.d.ts +1 -0
- package/Utils/jsonType.js +5 -0
- package/jTemplates.js +1 -1
- package/jTemplates.js.map +1 -1
- package/package.json +1 -1
- package/Store/Store/storeAsyncWriter.d.ts +0 -0
- package/Store/Store/storeAsyncWriter.js +0 -0
- package/Store/Store/storeSyncWriter.d.ts +0 -0
- package/Store/Store/storeSyncWriter.js +0 -0
- package/Store/Store/storeWriter.d.ts +0 -0
- package/Store/Store/storeWriter.js +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CreateNodeValueAssignment = CreateNodeValueAssignment;
|
|
4
4
|
exports.CreatePropertyAssignment = CreatePropertyAssignment;
|
|
5
|
-
const
|
|
5
|
+
const jsonType_1 = require("../Utils/jsonType");
|
|
6
6
|
function AssignProp(target, prop) {
|
|
7
7
|
let lastValue = target[prop];
|
|
8
8
|
return function Assign(value) {
|
|
@@ -43,7 +43,7 @@ function ValueAssignment(target, writeTo, next) {
|
|
|
43
43
|
}
|
|
44
44
|
function DefaultAssignment(target, writeTo, next, prop) {
|
|
45
45
|
const value = next[prop];
|
|
46
|
-
writeTo[prop] ??= (0,
|
|
46
|
+
writeTo[prop] ??= (0, jsonType_1.JsonType)(value) === 'value' ?
|
|
47
47
|
AssignProp(target, prop) :
|
|
48
48
|
CreatePropertyAssignment(target[prop], false);
|
|
49
49
|
writeTo[prop](value);
|
package/Node/nodeRef.types.d.ts
CHANGED
|
@@ -16,4 +16,4 @@ export interface INodeRefBase {
|
|
|
16
16
|
export interface INodeRef extends INodeRefBase {
|
|
17
17
|
type: NodeRefType.NodeRef;
|
|
18
18
|
}
|
|
19
|
-
export type NodeRefTypes = INodeRef | IBoundNode | IElementNode<
|
|
19
|
+
export type NodeRefTypes = INodeRef | IBoundNode | IElementNode<any> | IComponentNode<any, any, any>;
|
package/Store/Diff/diffSync.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DiffSync = void 0;
|
|
4
|
+
const json_1 = require("../../Utils/json");
|
|
4
5
|
const jsonDeepClone_1 = require("../../Utils/jsonDeepClone");
|
|
5
6
|
const diffTree_1 = require("./diffTree");
|
|
6
|
-
const diffCnstr = (0, diffTree_1.DiffTreeFactory)();
|
|
7
|
+
const diffCnstr = (0, diffTree_1.DiffTreeFactory)(json_1.JsonDiffFactory);
|
|
7
8
|
class DiffSync {
|
|
8
9
|
diffTree;
|
|
9
10
|
constructor(keyFunc) {
|
package/Store/Diff/diffTree.js
CHANGED
|
@@ -115,7 +115,7 @@ function DiffTreeFactory(jsonDiffFactory, worker) {
|
|
|
115
115
|
}
|
|
116
116
|
class DiffTree {
|
|
117
117
|
keyFunc;
|
|
118
|
-
rootState = {
|
|
118
|
+
rootState = {};
|
|
119
119
|
constructor(keyFunc) {
|
|
120
120
|
this.keyFunc = keyFunc;
|
|
121
121
|
}
|
|
@@ -126,11 +126,9 @@ function DiffTreeFactory(jsonDiffFactory, worker) {
|
|
|
126
126
|
return results;
|
|
127
127
|
}
|
|
128
128
|
DiffPath(path, value) {
|
|
129
|
-
path = (path && `root.${path}`) || "root";
|
|
130
129
|
return UpdateSource(this.rootState, path, value, this.keyFunc);
|
|
131
130
|
}
|
|
132
131
|
GetPath(path) {
|
|
133
|
-
path = (path && `root.${path}`) || "root";
|
|
134
132
|
return GetPathValue(this.rootState, path);
|
|
135
133
|
}
|
|
136
134
|
}
|
|
@@ -9,11 +9,11 @@ class WorkerQueue {
|
|
|
9
9
|
this.worker = worker;
|
|
10
10
|
this.callbacks = list_1.List.Create();
|
|
11
11
|
this.worker.onerror = (err) => {
|
|
12
|
-
|
|
12
|
+
const cb = list_1.List.Pop(this.callbacks);
|
|
13
13
|
cb && cb(null, err);
|
|
14
14
|
};
|
|
15
15
|
this.worker.onmessage = (message) => {
|
|
16
|
-
|
|
16
|
+
const cb = list_1.List.Pop(this.callbacks);
|
|
17
17
|
cb && cb(message.data);
|
|
18
18
|
};
|
|
19
19
|
}
|
package/Store/Store/store.d.ts
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { JsonDiffResult } from "../../Utils/json";
|
|
2
|
+
export declare class Store {
|
|
3
|
+
protected keyFunc: (value: unknown) => string;
|
|
4
|
+
private rootMap;
|
|
5
|
+
private createNode;
|
|
6
|
+
constructor(keyFunc: (value: unknown) => string);
|
|
7
|
+
Get<O>(id: string, defaultValue?: O): O | undefined;
|
|
8
|
+
protected UpdateRootMap(results: JsonDiffResult): void;
|
|
9
|
+
private UpdateRootObject;
|
|
10
|
+
}
|
package/Store/Store/store.js
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Store = void 0;
|
|
4
|
+
const json_1 = require("../../Utils/json");
|
|
5
|
+
const observableNode_1 = require("../Tree/observableNode");
|
|
6
|
+
class Store {
|
|
7
|
+
keyFunc;
|
|
8
|
+
rootMap = new Map();
|
|
9
|
+
createNode;
|
|
10
|
+
constructor(keyFunc) {
|
|
11
|
+
this.keyFunc = keyFunc;
|
|
12
|
+
const aliasFunc = (value) => {
|
|
13
|
+
const key = keyFunc(value);
|
|
14
|
+
if (key === undefined)
|
|
15
|
+
return undefined;
|
|
16
|
+
const rootObject = this.rootMap.get(key);
|
|
17
|
+
if (rootObject === undefined)
|
|
18
|
+
throw `No root object found for key: ${key}`;
|
|
19
|
+
const rootValue = rootObject[observableNode_1.GET_OBSERVABLE_VALUE];
|
|
20
|
+
const alias = rootValue[key];
|
|
21
|
+
return alias;
|
|
22
|
+
};
|
|
23
|
+
this.createNode = observableNode_1.ObservableNode.CreateFactory(aliasFunc);
|
|
24
|
+
}
|
|
25
|
+
Get(id, defaultValue) {
|
|
26
|
+
let result = this.rootMap.get(id);
|
|
27
|
+
if (result === undefined) {
|
|
28
|
+
result = this.createNode({ [id]: defaultValue });
|
|
29
|
+
this.rootMap.set(id, result);
|
|
30
|
+
}
|
|
31
|
+
return result[id];
|
|
32
|
+
}
|
|
33
|
+
UpdateRootMap(results) {
|
|
34
|
+
for (let x = 0; x < results.length;) {
|
|
35
|
+
const root = results[x].path[0];
|
|
36
|
+
const startIndex = x;
|
|
37
|
+
while (x < results.length && results[x].path[0] === root)
|
|
38
|
+
x++;
|
|
39
|
+
const rootGroup = results.slice(startIndex, x);
|
|
40
|
+
this.UpdateRootObject(rootGroup[0].path[0], rootGroup);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
UpdateRootObject(rootPath, results) {
|
|
44
|
+
const rootObject = this.rootMap.get(rootPath);
|
|
45
|
+
if (rootObject === undefined) {
|
|
46
|
+
if (results.length > 1 || results[0].path.length > 1)
|
|
47
|
+
throw `Unable to initialize root path ${rootPath} with ${results.length} results and initial path ${results[0].path}`;
|
|
48
|
+
const newRootObject = this.createNode({ [rootPath]: results[0].value });
|
|
49
|
+
this.rootMap.set(rootPath, newRootObject);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
observableNode_1.ObservableNode.EnableDiff(true);
|
|
53
|
+
(0, json_1.ApplyDiff)(rootObject, results);
|
|
54
|
+
observableNode_1.ObservableNode.EnableDiff(false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Store = Store;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Store } from "./store";
|
|
2
|
+
export declare class StoreAsync extends Store {
|
|
3
|
+
private diff;
|
|
4
|
+
private queue;
|
|
5
|
+
constructor(keyFunc: (value: unknown) => string);
|
|
6
|
+
Write(data: unknown, key?: string): Promise<void>;
|
|
7
|
+
Patch(key: string, patch: unknown): Promise<void>;
|
|
8
|
+
Push(key: string, ...data: unknown[]): Promise<void>;
|
|
9
|
+
Splice(key: string, start: number, deleteCount?: number, ...items: unknown[]): Promise<any[]>;
|
|
10
|
+
Destroy(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StoreAsync = void 0;
|
|
4
|
+
const asyncQueue_1 = require("../../Utils/asyncQueue");
|
|
5
|
+
const jsonMerge_1 = require("../../Utils/jsonMerge");
|
|
6
|
+
const diffAsync_1 = require("../Diff/diffAsync");
|
|
7
|
+
const observableNode_1 = require("../Tree/observableNode");
|
|
8
|
+
const store_1 = require("./store");
|
|
9
|
+
class StoreAsync extends store_1.Store {
|
|
10
|
+
diff;
|
|
11
|
+
queue = new asyncQueue_1.AsyncQueue();
|
|
12
|
+
constructor(keyFunc) {
|
|
13
|
+
super(keyFunc);
|
|
14
|
+
this.diff = new diffAsync_1.DiffAsync(keyFunc);
|
|
15
|
+
}
|
|
16
|
+
async Write(data, key) {
|
|
17
|
+
await this.queue.Next(async () => {
|
|
18
|
+
key = key || this.keyFunc(data);
|
|
19
|
+
if (!key)
|
|
20
|
+
throw "No key provided for data";
|
|
21
|
+
const diffResult = await this.diff.DiffPath(key, data);
|
|
22
|
+
this.UpdateRootMap(diffResult);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async Patch(key, patch) {
|
|
26
|
+
await this.queue.Next(async () => {
|
|
27
|
+
const value = this.Get(key);
|
|
28
|
+
if (value === undefined)
|
|
29
|
+
throw "Unable to patch undefined value";
|
|
30
|
+
const json = value.toJSON();
|
|
31
|
+
const mergedJson = (0, jsonMerge_1.JsonMerge)(json, patch);
|
|
32
|
+
const diffResult = await this.diff.DiffPath(key, mergedJson);
|
|
33
|
+
this.UpdateRootMap(diffResult);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async Push(key, ...data) {
|
|
37
|
+
await this.queue.Next(async () => {
|
|
38
|
+
const arr = this.Get(key);
|
|
39
|
+
const batch = data.map(function (d, i) {
|
|
40
|
+
return {
|
|
41
|
+
path: `${key}.${arr.length + i}`,
|
|
42
|
+
value: d
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
const diffResult = await this.diff.DiffBatch(batch);
|
|
46
|
+
this.UpdateRootMap(diffResult);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async Splice(key, start, deleteCount, ...items) {
|
|
50
|
+
return await this.queue.Next(async () => {
|
|
51
|
+
const arr = this.Get(key);
|
|
52
|
+
const arrValue = arr[observableNode_1.GET_OBSERVABLE_VALUE];
|
|
53
|
+
const arrCopy = arrValue.slice();
|
|
54
|
+
const spliceResult = arrCopy.splice(start, deleteCount, ...items);
|
|
55
|
+
const diffResult = await this.diff.DiffPath(key, arrCopy);
|
|
56
|
+
this.UpdateRootMap(diffResult);
|
|
57
|
+
return spliceResult;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
Destroy() {
|
|
61
|
+
this.queue.Stop();
|
|
62
|
+
this.diff.Destroy();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.StoreAsync = StoreAsync;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Store } from "./store";
|
|
2
|
+
export declare class StoreSync extends Store {
|
|
3
|
+
private diff;
|
|
4
|
+
constructor(keyFunc: (value: unknown) => string);
|
|
5
|
+
Write(data: unknown, key?: string): void;
|
|
6
|
+
Patch(key: string, patch: unknown): void;
|
|
7
|
+
Push(key: string, ...data: unknown[]): void;
|
|
8
|
+
Splice(key: string, start: number, deleteCount?: number, ...items: unknown[]): any[];
|
|
9
|
+
}
|
package/Store/Store/storeSync.js
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StoreSync = void 0;
|
|
4
|
+
const jsonMerge_1 = require("../../Utils/jsonMerge");
|
|
5
|
+
const diffSync_1 = require("../Diff/diffSync");
|
|
6
|
+
const observableNode_1 = require("../Tree/observableNode");
|
|
7
|
+
const store_1 = require("./store");
|
|
8
|
+
class StoreSync extends store_1.Store {
|
|
9
|
+
diff;
|
|
10
|
+
constructor(keyFunc) {
|
|
11
|
+
super(keyFunc);
|
|
12
|
+
this.diff = new diffSync_1.DiffSync(keyFunc);
|
|
13
|
+
}
|
|
14
|
+
Write(data, key) {
|
|
15
|
+
key = key || this.keyFunc(data);
|
|
16
|
+
if (!key)
|
|
17
|
+
throw "No key provided for data";
|
|
18
|
+
const diffResult = this.diff.DiffPath(key, data);
|
|
19
|
+
this.UpdateRootMap(diffResult);
|
|
20
|
+
}
|
|
21
|
+
Patch(key, patch) {
|
|
22
|
+
const value = this.Get(key);
|
|
23
|
+
if (value === undefined)
|
|
24
|
+
throw "Unable to patch undefined value";
|
|
25
|
+
const json = value.toJSON();
|
|
26
|
+
const mergedJson = (0, jsonMerge_1.JsonMerge)(json, patch);
|
|
27
|
+
const diffResult = this.diff.DiffPath(key, mergedJson);
|
|
28
|
+
this.UpdateRootMap(diffResult);
|
|
29
|
+
}
|
|
30
|
+
Push(key, ...data) {
|
|
31
|
+
const arr = this.Get(key);
|
|
32
|
+
const batch = data.map(function (d, i) {
|
|
33
|
+
return {
|
|
34
|
+
path: `${key}.${arr.length + i}`,
|
|
35
|
+
value: d
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
const diffResult = this.diff.DiffBatch(batch);
|
|
39
|
+
this.UpdateRootMap(diffResult);
|
|
40
|
+
}
|
|
41
|
+
Splice(key, start, deleteCount, ...items) {
|
|
42
|
+
const arr = this.Get(key);
|
|
43
|
+
const arrValue = arr[observableNode_1.GET_OBSERVABLE_VALUE];
|
|
44
|
+
const arrCopy = arrValue.slice();
|
|
45
|
+
const spliceResult = arrCopy.splice(start, deleteCount, ...items);
|
|
46
|
+
const diffResult = this.diff.DiffPath(key, arrCopy);
|
|
47
|
+
this.UpdateRootMap(diffResult);
|
|
48
|
+
return spliceResult;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.StoreSync = StoreSync;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export declare const IS_OBSERVABLE_NODE = "____isObservableNode";
|
|
2
|
+
export declare const GET_OBSERVABLE_VALUE = "____getObservableValue";
|
|
3
|
+
export declare const GET_TO_JSON = "toJSON";
|
|
1
4
|
export declare namespace ObservableNode {
|
|
2
5
|
function Create<T>(value: T): T;
|
|
3
6
|
function EnableDiff(value: boolean): void;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ObservableNode = void 0;
|
|
3
|
+
exports.ObservableNode = exports.GET_TO_JSON = exports.GET_OBSERVABLE_VALUE = exports.IS_OBSERVABLE_NODE = void 0;
|
|
4
4
|
const json_1 = require("../../Utils/json");
|
|
5
|
+
const jsonType_1 = require("../../Utils/jsonType");
|
|
5
6
|
const observableScope_1 = require("./observableScope");
|
|
6
|
-
|
|
7
|
+
exports.IS_OBSERVABLE_NODE = "____isObservableNode";
|
|
8
|
+
exports.GET_OBSERVABLE_VALUE = "____getObservableValue";
|
|
9
|
+
exports.GET_TO_JSON = "toJSON";
|
|
7
10
|
const proxyCache = new WeakMap();
|
|
8
11
|
const scopeCache = new WeakMap();
|
|
9
12
|
function getOwnPropertyDescriptor(target, prop) {
|
|
@@ -33,11 +36,11 @@ function ownKeysArray(value) {
|
|
|
33
36
|
return Object.keys(value);
|
|
34
37
|
}
|
|
35
38
|
function UnwrapProxy(value) {
|
|
36
|
-
const type = (0,
|
|
39
|
+
const type = (0, jsonType_1.JsonType)(value);
|
|
37
40
|
if (type === "value")
|
|
38
41
|
return value;
|
|
39
|
-
if (value[IS_OBSERVABLE_NODE] === true)
|
|
40
|
-
return value.
|
|
42
|
+
if (value[exports.IS_OBSERVABLE_NODE] === true)
|
|
43
|
+
return value[exports.GET_OBSERVABLE_VALUE];
|
|
41
44
|
switch (type) {
|
|
42
45
|
case "object": {
|
|
43
46
|
const keys = Object.keys(value);
|
|
@@ -53,17 +56,23 @@ function UnwrapProxy(value) {
|
|
|
53
56
|
}
|
|
54
57
|
let applyingDiff = false;
|
|
55
58
|
function CreateProxyFactory(alias) {
|
|
59
|
+
function CreateProxy(value) {
|
|
60
|
+
value = UnwrapProxy(value);
|
|
61
|
+
return CreateProxyFromValue(value);
|
|
62
|
+
}
|
|
56
63
|
function ToJsonCopy(value) {
|
|
57
|
-
const type = (0,
|
|
64
|
+
const type = (0, jsonType_1.JsonType)(value);
|
|
58
65
|
switch (type) {
|
|
59
66
|
case "array": {
|
|
60
67
|
const typedValue = value;
|
|
61
|
-
|
|
68
|
+
const proxy = CreateProxy(typedValue);
|
|
69
|
+
return proxy.map(ToJsonCopy);
|
|
62
70
|
break;
|
|
63
71
|
}
|
|
64
72
|
case "object": {
|
|
65
73
|
const typedValue = alias(value) ?? value;
|
|
66
|
-
const
|
|
74
|
+
const proxy = CreateProxy(typedValue);
|
|
75
|
+
const keys = Object.keys(proxy);
|
|
67
76
|
const copy = {};
|
|
68
77
|
for (let x = 0; x < keys.length; x++)
|
|
69
78
|
copy[keys[x]] = ToJsonCopy(typedValue[keys[x]]);
|
|
@@ -77,6 +86,7 @@ function CreateProxyFactory(alias) {
|
|
|
77
86
|
return value;
|
|
78
87
|
}
|
|
79
88
|
const ToJson = alias !== undefined ? ToJsonCopy : ToJsonDefault;
|
|
89
|
+
const readOnly = alias !== undefined;
|
|
80
90
|
function CreateArrayProxy(value) {
|
|
81
91
|
const scope = observableScope_1.ObservableScope.Create(() => value);
|
|
82
92
|
const proxy = new Proxy(value, {
|
|
@@ -104,8 +114,10 @@ function CreateProxyFactory(alias) {
|
|
|
104
114
|
return proxy;
|
|
105
115
|
}
|
|
106
116
|
function ArrayProxySetter(array, prop, value) {
|
|
107
|
-
if (
|
|
108
|
-
throw `
|
|
117
|
+
if (readOnly && !applyingDiff)
|
|
118
|
+
throw `Object is readonly`;
|
|
119
|
+
if (prop === exports.IS_OBSERVABLE_NODE)
|
|
120
|
+
throw `Cannot assign read-only property: ${exports.IS_OBSERVABLE_NODE}`;
|
|
109
121
|
value = UnwrapProxy(value);
|
|
110
122
|
array[prop] = value;
|
|
111
123
|
const scope = scopeCache.get(array);
|
|
@@ -113,15 +125,29 @@ function CreateProxyFactory(alias) {
|
|
|
113
125
|
return true;
|
|
114
126
|
}
|
|
115
127
|
function ArrayProxyGetter(array, prop) {
|
|
116
|
-
if (prop === IS_OBSERVABLE_NODE)
|
|
128
|
+
if (prop === exports.IS_OBSERVABLE_NODE)
|
|
117
129
|
return true;
|
|
130
|
+
if (readOnly && !applyingDiff)
|
|
131
|
+
switch (prop) {
|
|
132
|
+
case "push":
|
|
133
|
+
case "unshift":
|
|
134
|
+
case "splice":
|
|
135
|
+
case "pop":
|
|
136
|
+
case "shift":
|
|
137
|
+
case "sort":
|
|
138
|
+
case "reverse":
|
|
139
|
+
if (readOnly && !applyingDiff)
|
|
140
|
+
throw `Object is readonly`;
|
|
141
|
+
}
|
|
118
142
|
const scope = scopeCache.get(array);
|
|
119
143
|
array = observableScope_1.ObservableScope.Value(scope);
|
|
120
144
|
switch (prop) {
|
|
121
|
-
case
|
|
145
|
+
case exports.GET_TO_JSON:
|
|
122
146
|
return function () {
|
|
123
147
|
return ToJson(array);
|
|
124
148
|
};
|
|
149
|
+
case exports.GET_OBSERVABLE_VALUE:
|
|
150
|
+
return array;
|
|
125
151
|
default: {
|
|
126
152
|
const arrayValue = array[prop];
|
|
127
153
|
if (typeof prop === "symbol")
|
|
@@ -156,8 +182,10 @@ function CreateProxyFactory(alias) {
|
|
|
156
182
|
}
|
|
157
183
|
}
|
|
158
184
|
function ObjectProxySetter(object, prop, value) {
|
|
159
|
-
if (
|
|
160
|
-
throw `
|
|
185
|
+
if (readOnly && !applyingDiff)
|
|
186
|
+
throw `Object is readonly`;
|
|
187
|
+
if (prop === exports.IS_OBSERVABLE_NODE)
|
|
188
|
+
throw `Cannot assign read-only property: ${exports.IS_OBSERVABLE_NODE}`;
|
|
161
189
|
const scope = scopeCache.get(object);
|
|
162
190
|
value = UnwrapProxy(value);
|
|
163
191
|
if (applyingDiff) {
|
|
@@ -187,15 +215,17 @@ function CreateProxyFactory(alias) {
|
|
|
187
215
|
return true;
|
|
188
216
|
}
|
|
189
217
|
function ObjectProxyGetter(object, prop) {
|
|
190
|
-
if (prop === IS_OBSERVABLE_NODE)
|
|
218
|
+
if (prop === exports.IS_OBSERVABLE_NODE)
|
|
191
219
|
return true;
|
|
192
220
|
const scope = scopeCache.get(object);
|
|
193
221
|
object = observableScope_1.ObservableScope.Value(scope);
|
|
194
222
|
switch (prop) {
|
|
195
|
-
case
|
|
223
|
+
case exports.GET_TO_JSON:
|
|
196
224
|
return function () {
|
|
197
225
|
return ToJson(object);
|
|
198
226
|
};
|
|
227
|
+
case exports.GET_OBSERVABLE_VALUE:
|
|
228
|
+
return object;
|
|
199
229
|
default: {
|
|
200
230
|
const proxyValue = object[prop];
|
|
201
231
|
if (typeof prop === "symbol")
|
|
@@ -206,7 +236,7 @@ function CreateProxyFactory(alias) {
|
|
|
206
236
|
}
|
|
207
237
|
}
|
|
208
238
|
function CreateProxyFromValue(value) {
|
|
209
|
-
const type = (0,
|
|
239
|
+
const type = (0, jsonType_1.JsonType)(value);
|
|
210
240
|
switch (type) {
|
|
211
241
|
case "object": {
|
|
212
242
|
let proxy = proxyCache.get(value) ?? CreateObjectProxy(value);
|
|
@@ -226,10 +256,6 @@ function CreateProxyFactory(alias) {
|
|
|
226
256
|
return value;
|
|
227
257
|
}
|
|
228
258
|
}
|
|
229
|
-
function CreateProxy(value) {
|
|
230
|
-
value = UnwrapProxy(value);
|
|
231
|
-
return CreateProxyFromValue(value);
|
|
232
|
-
}
|
|
233
259
|
return CreateProxy;
|
|
234
260
|
}
|
|
235
261
|
const DefaultCreateProxy = CreateProxyFactory();
|
|
@@ -32,15 +32,14 @@ export interface IObservableScope<T> extends IDestroyable {
|
|
|
32
32
|
value: T;
|
|
33
33
|
dirty: boolean;
|
|
34
34
|
emitter: Emitter;
|
|
35
|
-
emitters:
|
|
35
|
+
emitters: (Emitter | null)[];
|
|
36
36
|
setCallback: EmitterCallback;
|
|
37
37
|
destroyed: boolean;
|
|
38
|
-
dependencies?: IObservableScope<unknown>[];
|
|
39
38
|
}
|
|
40
39
|
export declare namespace ObservableScope {
|
|
41
40
|
function Create<T>(valueFunction: {
|
|
42
41
|
(): T | Promise<T>;
|
|
43
|
-
} | T
|
|
42
|
+
} | T): IObservableScope<T>;
|
|
44
43
|
function Register(emitter: Emitter): void;
|
|
45
44
|
function Value<T>(scope: IObservableScope<T>): T;
|
|
46
45
|
function Watching(): boolean;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObservableScope = exports.ObservableScopeWrapper = exports.ObservableScopeValue = void 0;
|
|
4
|
+
const array_1 = require("../../Utils/array");
|
|
4
5
|
const emitter_1 = require("../../Utils/emitter");
|
|
5
6
|
class ObservableScopeValue {
|
|
6
7
|
scope;
|
|
@@ -61,7 +62,7 @@ function WatchAction(action) {
|
|
|
61
62
|
return lastSet;
|
|
62
63
|
}
|
|
63
64
|
(function (ObservableScope) {
|
|
64
|
-
function Create(valueFunction
|
|
65
|
+
function Create(valueFunction) {
|
|
65
66
|
const hasFunction = typeof valueFunction === "function";
|
|
66
67
|
const scope = {
|
|
67
68
|
getFunction: hasFunction ? valueFunction : null,
|
|
@@ -71,14 +72,13 @@ function WatchAction(action) {
|
|
|
71
72
|
: false,
|
|
72
73
|
dirty: hasFunction,
|
|
73
74
|
emitter: hasFunction ? emitter_1.Emitter.Create() : null,
|
|
74
|
-
emitters:
|
|
75
|
+
emitters: [],
|
|
75
76
|
destroyed: false,
|
|
76
77
|
setCallback: hasFunction
|
|
77
78
|
? function () {
|
|
78
79
|
return OnSet(scope);
|
|
79
80
|
}
|
|
80
81
|
: null,
|
|
81
|
-
dependencies,
|
|
82
82
|
};
|
|
83
83
|
return scope;
|
|
84
84
|
}
|
|
@@ -154,23 +154,30 @@ function UpdateValue(scope) {
|
|
|
154
154
|
function DestroyScope(scope) {
|
|
155
155
|
if (!scope)
|
|
156
156
|
return;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
DestroyScope(scope.dependencies[x]);
|
|
160
|
-
scope.emitters && scope.emitters.clear();
|
|
161
|
-
scope.emitter && emitter_1.Emitter.Clear(scope.emitter);
|
|
157
|
+
scope.emitters = null;
|
|
158
|
+
scope.emitter = null;
|
|
162
159
|
scope.getFunction = null;
|
|
163
160
|
scope.setCallback = null;
|
|
164
161
|
scope.destroyed = true;
|
|
165
162
|
}
|
|
166
163
|
function UpdateEmitters(scope, newEmitters) {
|
|
167
|
-
if (newEmitters) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
164
|
+
if (newEmitters === null) {
|
|
165
|
+
for (let x = 0; x < scope.emitters.length; x++)
|
|
166
|
+
emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
|
|
167
|
+
scope.emitters = [];
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
let removed = false;
|
|
171
|
+
for (let x = 0; x < scope.emitters.length; x++) {
|
|
172
|
+
if (!newEmitters.delete(scope.emitters[x])) {
|
|
173
|
+
emitter_1.Emitter.Remove(scope.emitters[x], scope.setCallback);
|
|
174
|
+
scope.emitters[x] = null;
|
|
175
|
+
removed = true;
|
|
176
|
+
}
|
|
172
177
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
scope.emitters
|
|
178
|
+
const initLength = scope.emitters.length;
|
|
179
|
+
scope.emitters.push(...newEmitters);
|
|
180
|
+
for (let x = initLength; x < scope.emitters.length; x++)
|
|
181
|
+
emitter_1.Emitter.On(scope.emitters[x], scope.setCallback);
|
|
182
|
+
removed && (0, array_1.RemoveNulls)(scope.emitters);
|
|
176
183
|
}
|
package/Store/index.d.ts
CHANGED
package/Store/index.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ObservableNode = exports.ObservableScope = void 0;
|
|
3
|
+
exports.ObservableNode = exports.ObservableScope = exports.StoreAsync = exports.StoreSync = void 0;
|
|
4
|
+
var storeSync_1 = require("./Store/storeSync");
|
|
5
|
+
Object.defineProperty(exports, "StoreSync", { enumerable: true, get: function () { return storeSync_1.StoreSync; } });
|
|
6
|
+
var storeAsync_1 = require("./Store/storeAsync");
|
|
7
|
+
Object.defineProperty(exports, "StoreAsync", { enumerable: true, get: function () { return storeAsync_1.StoreAsync; } });
|
|
4
8
|
var observableScope_1 = require("./Tree/observableScope");
|
|
5
9
|
Object.defineProperty(exports, "ObservableScope", { enumerable: true, get: function () { return observableScope_1.ObservableScope; } });
|
|
6
10
|
var observableNode_1 = require("./Tree/observableNode");
|
package/Utils/array.js
CHANGED
|
@@ -5,11 +5,12 @@ function RemoveNulls(array, startIndex = 0) {
|
|
|
5
5
|
let nullIndex = startIndex;
|
|
6
6
|
for (; nullIndex < array.length && array[nullIndex] !== null; nullIndex++) { }
|
|
7
7
|
let notNullIndex = nullIndex + 1;
|
|
8
|
+
for (; notNullIndex < array.length && array[notNullIndex] === null; notNullIndex++) { }
|
|
8
9
|
while (notNullIndex < array.length) {
|
|
9
|
-
for (; notNullIndex < array.length && array[notNullIndex] === null; notNullIndex++) { }
|
|
10
10
|
array[nullIndex] = array[notNullIndex];
|
|
11
11
|
nullIndex++;
|
|
12
12
|
notNullIndex++;
|
|
13
|
+
for (; notNullIndex < array.length && array[notNullIndex] === null; notNullIndex++) { }
|
|
13
14
|
}
|
|
14
15
|
array.splice(nullIndex);
|
|
15
16
|
}
|
package/Utils/decorators.js
CHANGED
|
@@ -11,8 +11,7 @@ exports.PreReqTemplate = PreReqTemplate;
|
|
|
11
11
|
exports.PreReq = PreReq;
|
|
12
12
|
const observableScope_1 = require("../Store/Tree/observableScope");
|
|
13
13
|
const observableNode_1 = require("../Store/Tree/observableNode");
|
|
14
|
-
const
|
|
15
|
-
const json_1 = require("./json");
|
|
14
|
+
const Store_1 = require("../Store");
|
|
16
15
|
const decoratorInstanceMap = new WeakMap();
|
|
17
16
|
const valueInstanceMap = new WeakMap();
|
|
18
17
|
function GetDecoratorMapForInstance(instance) {
|
|
@@ -66,8 +65,6 @@ function ValueDecorator(target, propertyKey) {
|
|
|
66
65
|
},
|
|
67
66
|
set: function (val) {
|
|
68
67
|
const valueMap = GetValueMapForInstance(this);
|
|
69
|
-
if (valueMap.get(propKey) === val)
|
|
70
|
-
return;
|
|
71
68
|
valueMap.set(propKey, val);
|
|
72
69
|
const map = GetDecoratorMapForInstance(this);
|
|
73
70
|
let scope = map.get(propKey);
|
|
@@ -82,42 +79,28 @@ function ValueDecorator(target, propertyKey) {
|
|
|
82
79
|
},
|
|
83
80
|
};
|
|
84
81
|
}
|
|
82
|
+
function defaultKeyFunc() { }
|
|
85
83
|
function StateAsync(defaultValue, keyFunc) {
|
|
86
|
-
return StateAsyncDecorator.bind(null, defaultValue, keyFunc);
|
|
84
|
+
return StateAsyncDecorator.bind(null, defaultValue, keyFunc ?? defaultKeyFunc);
|
|
87
85
|
}
|
|
88
86
|
function StateAsyncDecorator(defaultValue, keyFunc, target, propertyKey) {
|
|
89
87
|
const propKey = `ValueDecorator_${propertyKey}`;
|
|
90
|
-
const treeKey = `ValueDecoratorTree_${propertyKey}`;
|
|
91
88
|
return {
|
|
92
89
|
configurable: false,
|
|
93
90
|
enumerable: true,
|
|
94
91
|
get: function () {
|
|
95
92
|
const map = GetDecoratorMapForInstance(this);
|
|
96
|
-
const
|
|
97
|
-
return
|
|
93
|
+
const store = map.get(propKey);
|
|
94
|
+
return store?.Get('root', defaultValue) ?? defaultValue;
|
|
98
95
|
},
|
|
99
96
|
set: function (val) {
|
|
100
97
|
const map = GetDecoratorMapForInstance(this);
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
map.set(propKey,
|
|
105
|
-
const newTree = new diffAsync_1.DiffAsync(keyFunc);
|
|
106
|
-
map.set(treeKey, newTree);
|
|
107
|
-
newTree.DiffPath("", val).then((result) => {
|
|
108
|
-
observableNode_1.ObservableNode.EnableDiff(false);
|
|
109
|
-
(0, json_1.ApplyDiff)(newNode, result);
|
|
110
|
-
observableNode_1.ObservableNode.EnableDiff(true);
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
const tree = map.get(treeKey);
|
|
115
|
-
tree.DiffPath("", val).then((result) => {
|
|
116
|
-
observableNode_1.ObservableNode.EnableDiff(false);
|
|
117
|
-
(0, json_1.ApplyDiff)(node, result);
|
|
118
|
-
observableNode_1.ObservableNode.EnableDiff(true);
|
|
119
|
-
});
|
|
98
|
+
let store = map.get(propKey);
|
|
99
|
+
if (store === undefined) {
|
|
100
|
+
store = new Store_1.StoreAsync(keyFunc);
|
|
101
|
+
map.set(propKey, store);
|
|
120
102
|
}
|
|
103
|
+
store.Write(val, "root");
|
|
121
104
|
},
|
|
122
105
|
};
|
|
123
106
|
}
|
package/Utils/json.d.ts
CHANGED
|
@@ -5,8 +5,7 @@ export type JsonDiffResult<T = unknown> = {
|
|
|
5
5
|
export type JsonDiffFactoryResult = ReturnType<typeof JsonDiffFactory>;
|
|
6
6
|
export declare function ApplyDiff(root: any, diffResult: JsonDiffResult): void;
|
|
7
7
|
export declare function JsonDiffFactory(): {
|
|
8
|
-
JsonType: (value: any) => "value" | "array" | "object";
|
|
9
8
|
JsonDiff: <T>(newValue: T, oldValue: T, rootPath?: string, initResult?: JsonDiffResult<T>) => JsonDiffResult<T>;
|
|
9
|
+
JsonType: (value: any) => "value" | "array" | "object";
|
|
10
10
|
};
|
|
11
|
-
export declare const JsonDiff: <T>(newValue: T, oldValue: T, rootPath?: string, initResult?: JsonDiffResult<T>) => JsonDiffResult<T
|
|
12
|
-
export declare const JsonType: (value: any) => "value" | "array" | "object";
|
|
11
|
+
export declare const JsonDiff: <T>(newValue: T, oldValue: T, rootPath?: string, initResult?: JsonDiffResult<T>) => JsonDiffResult<T>, JsonType: (value: any) => "value" | "array" | "object";
|