annil 1.6.2 → 1.6.4
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/CHANGELOG.md +14 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.js +6 -5
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.js.map +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.d.ts +3 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.js +17 -9
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.js.map +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/index.js +2 -2
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/index.js.map +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.d.ts +12 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.js +36 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/uniqueDependences.d.ts +2 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/uniqueDependences.js +20 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/uniqueDependences.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.ts +8 -6
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.ts +21 -17
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/index.ts +9 -5
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.ts +84 -0
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/uniqueDependences.ts +34 -0
- package/src/api/DefineComponent/assignOptions/index.ts +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.d.ts +0 -12
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.js +0 -63
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.js.map +0 -1
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.ts +0 -135
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,20 @@
|
|
|
4
4
|
|
|
5
5
|
* 解决DetailedType不接收接口类型的错误 ([ae8acbf](https://github.com/missannil/annil/commit/ae8acbfc2e62f99db565c448ad9253aa549e78bb))
|
|
6
6
|
|
|
7
|
+
## [1.6.4](https://github.com/missannil/annil/compare/v1.6.3...v1.6.4) (2024-02-12)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Bug Fixes
|
|
11
|
+
|
|
12
|
+
* comouted属性初始化依赖去重bug,并添加更新时去重 ([f228e55](https://github.com/missannil/annil/commit/f228e551dce65d97450b9a1aa3c7b3d3eb3e8199))
|
|
13
|
+
|
|
14
|
+
## [1.6.3](https://github.com/missannil/annil/compare/v1.6.2...v1.6.3) (2024-02-05)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* 计算属性优化 ([2479d08](https://github.com/missannil/annil/commit/2479d08720b2e69782c063b5fde394f0fa35d28a))
|
|
20
|
+
|
|
7
21
|
## [1.6.2](https://github.com/missannil/annil/compare/v1.6.1...v1.6.2) (2024-02-03)
|
|
8
22
|
|
|
9
23
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { deepProxy } from "./data-tracer";
|
|
1
|
+
import { deepProxy, getProxyOriginalValue } from "./data-tracer";
|
|
3
2
|
import { getPathsValue } from "./getPathsValue";
|
|
4
3
|
import { isEqual } from "./isEqual";
|
|
4
|
+
import { uniqueDependences } from "./uniqueDependences";
|
|
5
5
|
export function computedUpdater(isUpdated = false) {
|
|
6
6
|
for (const key in this.data.__computedCache__) {
|
|
7
7
|
const itemCache = this.data.__computedCache__[key];
|
|
@@ -15,12 +15,13 @@ export function computedUpdater(isUpdated = false) {
|
|
|
15
15
|
}
|
|
16
16
|
if (changed) {
|
|
17
17
|
const newDependences = [];
|
|
18
|
-
|
|
18
|
+
let newValue = itemCache.method.call({ data: deepProxy(this.data, newDependences) });
|
|
19
|
+
newValue = getProxyOriginalValue(newValue);
|
|
19
20
|
this.setData({
|
|
20
|
-
[key]:
|
|
21
|
+
[key]: newValue,
|
|
21
22
|
});
|
|
22
23
|
isUpdated = true;
|
|
23
|
-
this.data.__computedCache__[key].dependences = newDependences;
|
|
24
|
+
this.data.__computedCache__[key].dependences = uniqueDependences(newDependences);
|
|
24
25
|
return computedUpdater.call(this, isUpdated);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computedUpdater.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"computedUpdater.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAGxD,MAAM,UAAU,eAAe,CAAiB,SAAS,GAAG,KAAK;IAC/D,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC;QACnD,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,KAAK,MAAM,GAAG,IAAI,SAAS,CAAC,WAAW,EAAE,CAAC;YAExC,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAGhE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC9B,OAAO,GAAG,IAAI,CAAC;gBAEf,MAAM;YACR,CAAC;QACH,CAAC;QACD,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,cAAc,GAAyB,EAAE,CAAC;YAChD,IAAI,QAAQ,GAAG,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,EAAE,CAAC,CAAC;YAGrF,QAAQ,GAAG,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YAE3C,IAAI,CAAC,OAAO,CAAC;gBACX,CAAC,GAAG,CAAC,EAAE,QAAQ;aAChB,CAAC,CAAC;YAEH,SAAS,GAAG,IAAI,CAAC;YAGjB,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,WAAW,GAAG,iBAAiB,CAAC,cAAc,CAAC,CAAC;YAGjF,OAAO,eAAe,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { ComputedDependence } from "./computedUpdater";
|
|
2
2
|
export declare function deepProxy(data: object, dependences: ComputedDependence[], basePath?: string[]): object;
|
|
3
|
+
export declare function getProxyOriginalValue<T extends {
|
|
4
|
+
__original__?: string;
|
|
5
|
+
}>(value: T): unknown;
|
|
@@ -2,18 +2,18 @@ import { deepClone } from "../../../../utils/deepClone";
|
|
|
2
2
|
export function deepProxy(data, dependences, basePath = []) {
|
|
3
3
|
const handler = {
|
|
4
4
|
get(target, prop) {
|
|
5
|
+
if (prop === "__original__") {
|
|
6
|
+
return target;
|
|
7
|
+
}
|
|
5
8
|
const val = target[prop];
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
if (lastDependences.paths.toString() === basePath.toString()) {
|
|
9
|
-
dependences.pop();
|
|
10
|
-
}
|
|
9
|
+
if ((val !== undefined) && (!Object.prototype.hasOwnProperty.call(target, prop) || typeof val === "function")) {
|
|
10
|
+
return val.bind(target);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (!Object.prototype.hasOwnProperty.call(target, prop) || typeof val === "function") {
|
|
15
|
-
return typeof val === "function" ? val.bind(target) : val;
|
|
12
|
+
if (basePath.length !== 0 && dependences[dependences.length - 1].paths.toString() === basePath.toString()) {
|
|
13
|
+
dependences.pop();
|
|
16
14
|
}
|
|
15
|
+
const curPath = basePath.concat(prop);
|
|
16
|
+
dependences.push({ paths: curPath, val });
|
|
17
17
|
if (typeof val !== "object" || val === null)
|
|
18
18
|
return val;
|
|
19
19
|
return deepProxy(val, dependences, curPath);
|
|
@@ -24,4 +24,12 @@ export function deepProxy(data, dependences, basePath = []) {
|
|
|
24
24
|
};
|
|
25
25
|
return new Proxy(data, handler);
|
|
26
26
|
}
|
|
27
|
+
export function getProxyOriginalValue(value) {
|
|
28
|
+
if (typeof value !== "object" || value === null || !value.__original__) {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return deepClone(value.__original__);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
27
35
|
//# sourceMappingURL=data-tracer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-tracer.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAGxD,MAAM,UAAU,SAAS,CACvB,IAAY,EACZ,WAAiC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;YAExD,OAAO,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,GAAG,CAAC,OAAe,EAAE,IAAY;YAC/B,MAAM,KAAK,CAAC,GAAG,IAAI,QAAQ,CAAC,CAAC;QAC/B,CAAC;KACF,CAAC;IAEF,OAAO,IAAI,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAsC,KAAQ;IACjF,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC;QACvE,OAAO,KAAK,CAAC;IACf,CAAC;SAAM,CAAC;QACN,OAAO,SAAS,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACvC,CAAC;AACH,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { computedUpdater } from "./computedUpdater";
|
|
2
2
|
import { getPathsValue } from "./getPathsValue";
|
|
3
3
|
import { getPropertiesValue } from "./getPropertiesValue";
|
|
4
|
-
import {
|
|
4
|
+
import { initComputedAndGetCache } from "./initComputedAndGetCache";
|
|
5
5
|
import { isEqual } from "./isEqual";
|
|
6
6
|
import { deepClone } from "../../../../utils/deepClone";
|
|
7
7
|
import { isEmptyObject } from "../../../../utils/isEmptyObject";
|
|
@@ -16,7 +16,7 @@ export function computedWatchHandle(options) {
|
|
|
16
16
|
const computedConfig = options.computed;
|
|
17
17
|
const rawPropertiesValue = getPropertiesValue(options.properties);
|
|
18
18
|
if (computedConfig && !isEmptyObject(computedConfig)) {
|
|
19
|
-
const __computedInitCache__ =
|
|
19
|
+
const __computedInitCache__ = initComputedAndGetCache(options, computedConfig, Object.assign(Object.assign({}, options.data), rawPropertiesValue));
|
|
20
20
|
options.data.__computedCache__ = __computedInitCache__;
|
|
21
21
|
options.methods.__computedUpdater__ = computedUpdater;
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAKhE,SAAS,iBAAiB,CAAC,IAAqC,EAAE,WAAmB;IACnF,MAAM,aAAa,GAAG,EAAE,CAAC;IACzB,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAM9B,aAAa,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AACD,MAAM,UAAU,mBAAmB,CAAC,OAAgC;IAElE,MAAM,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC;IACxC,MAAM,kBAAkB,GAAG,kBAAkB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAClE,IAAI,cAAc,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,EA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AC,GAAG,CAAC,CAAC;gBAEpC,IAAI,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAC;oBAAE,OAAO;gBACxC,aAAa,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;gBAEzC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,QAAQ,EAAE,GAAG,QAAQ,CAAC,CAAC;YAClD,CAAC,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Func } from "hry-types/src/Misc/Func";
|
|
2
|
+
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
+
import type { ComputedDependence } from "./computedUpdater";
|
|
4
|
+
type ItemCache = {
|
|
5
|
+
dependences: ComputedDependence[];
|
|
6
|
+
method: Func;
|
|
7
|
+
value: unknown;
|
|
8
|
+
};
|
|
9
|
+
type ComputedId = string;
|
|
10
|
+
export type ComputedCache = Record<ComputedId, ItemCache>;
|
|
11
|
+
export declare function initComputedAndGetCache(options: FinalOptionsOfComponent, computedConfig: Record<string, Func>, initAllData: Record<string, unknown>, uninitedkeys?: string[], computedCache?: ComputedCache): ComputedCache;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { deepProxy, getProxyOriginalValue } from "./data-tracer";
|
|
2
|
+
import { uniqueDependences } from "./uniqueDependences";
|
|
3
|
+
function isValidDependences(dependences, computedKeys) {
|
|
4
|
+
for (const { paths: path, val } of dependences) {
|
|
5
|
+
if ((val === undefined) && computedKeys.includes(path[0])) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
export function initComputedAndGetCache(options, computedConfig, initAllData, uninitedkeys = Object.keys(computedConfig), computedCache = {}) {
|
|
12
|
+
for (const key of uninitedkeys) {
|
|
13
|
+
const computedFunc = computedConfig[key];
|
|
14
|
+
const dependences = [];
|
|
15
|
+
let initValue = computedFunc.call({ data: deepProxy(initAllData, dependences) });
|
|
16
|
+
uninitedkeys = uninitedkeys.filter(ele => ele !== key);
|
|
17
|
+
if (isValidDependences(dependences, uninitedkeys)) {
|
|
18
|
+
initValue = getProxyOriginalValue(initValue);
|
|
19
|
+
options.data[key] = initValue;
|
|
20
|
+
initAllData[key] = initValue;
|
|
21
|
+
computedCache[key] = {
|
|
22
|
+
dependences: uniqueDependences(dependences),
|
|
23
|
+
method: computedFunc,
|
|
24
|
+
value: initValue,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
uninitedkeys.push(key);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (uninitedkeys.length === 0) {
|
|
32
|
+
return computedCache;
|
|
33
|
+
}
|
|
34
|
+
return initComputedAndGetCache(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=initComputedAndGetCache.js.map
|
package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"initComputedAndGetCache.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/initComputedAndGetCache.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAexD,SAAS,kBAAkB,CAAC,WAAiC,EAAE,YAAsB;IACnF,KAAK,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,WAAW,EAAE,CAAC;QAC/C,IAAI,CAAC,GAAG,KAAK,SAAS,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC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pG,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function uniqueDependences(dependences) {
|
|
2
|
+
if (dependences.length === 1)
|
|
3
|
+
return dependences;
|
|
4
|
+
for (let f = 0; f < dependences.length; f++) {
|
|
5
|
+
const firstPath = dependences[f].paths.join(".") + ".";
|
|
6
|
+
for (let i = f + 1; i < dependences.length; i++) {
|
|
7
|
+
const curPath = dependences[i].paths.join(".") + ".";
|
|
8
|
+
if (firstPath.startsWith(curPath)) {
|
|
9
|
+
dependences.splice(f, 1);
|
|
10
|
+
return uniqueDependences(dependences);
|
|
11
|
+
}
|
|
12
|
+
if (curPath.startsWith(firstPath)) {
|
|
13
|
+
dependences.splice(i, 1);
|
|
14
|
+
return uniqueDependences(dependences);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return dependences;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=uniqueDependences.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uniqueDependences.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/uniqueDependences.ts"],"names":[],"mappings":"AAEA,MAAM,UASG,iBAAiB,CAAC,WAAiC;IAC1D,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,WAAW,CAAC;IACjD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5C,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;QACvD,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAChD,MAAM,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;YACrD,IAAI,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAElC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;gBAEzB,OAAO,iBAAiB,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC;YACD,IAAI,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;gBAElC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;gBAEzB,OAAO,iBAAiB,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -9,7 +9,7 @@ import type { PageLifetimesOption } from "../../RootComponent/PageLifetimes/Page
|
|
|
9
9
|
import type { PropertiesConstraint } from "../../RootComponent/Properties/PropertiesConstraint";
|
|
10
10
|
import type { StoreConstraint } from "../../RootComponent/Store/StoreConstraint";
|
|
11
11
|
import type { DefineComponentOption } from "..";
|
|
12
|
-
import type { ComputedCache } from "./computedWatchHandle/
|
|
12
|
+
import type { ComputedCache } from "./computedWatchHandle/initComputedAndGetCache";
|
|
13
13
|
type InstanceInnerFields = {
|
|
14
14
|
data: OptionsInnerFields["data"];
|
|
15
15
|
disposer: Record<string, Func>;
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { deepClone } from "../../../../utils/deepClone";
|
|
2
1
|
import type { Instance } from "..";
|
|
3
|
-
import { deepProxy } from "./data-tracer";
|
|
2
|
+
import { deepProxy, getProxyOriginalValue } from "./data-tracer";
|
|
4
3
|
import { getPathsValue } from "./getPathsValue";
|
|
5
4
|
|
|
6
5
|
import { isEqual } from "./isEqual";
|
|
6
|
+
import { uniqueDependences } from "./uniqueDependences";
|
|
7
7
|
export type ComputedDependence = { paths: string[]; val: unknown };
|
|
8
8
|
|
|
9
9
|
export function computedUpdater(this: Instance, isUpdated = false): boolean {
|
|
@@ -23,17 +23,19 @@ export function computedUpdater(this: Instance, isUpdated = false): boolean {
|
|
|
23
23
|
}
|
|
24
24
|
if (changed) {
|
|
25
25
|
const newDependences: ComputedDependence[] = [];
|
|
26
|
-
|
|
26
|
+
let newValue = itemCache.method.call({ data: deepProxy(this.data, newDependences) });
|
|
27
27
|
|
|
28
28
|
// 更新值不会立即再次进入**函数,而是当前**函数运行完毕后触发**函数,
|
|
29
|
+
newValue = getProxyOriginalValue(newValue);
|
|
30
|
+
|
|
29
31
|
this.setData({
|
|
30
|
-
[key]:
|
|
32
|
+
[key]: newValue,
|
|
31
33
|
});
|
|
32
34
|
|
|
33
35
|
isUpdated = true;
|
|
34
36
|
|
|
35
|
-
//
|
|
36
|
-
this.data.__computedCache__[key].dependences = newDependences;
|
|
37
|
+
// 更新依赖,去重
|
|
38
|
+
this.data.__computedCache__[key].dependences = uniqueDependences(newDependences);
|
|
37
39
|
|
|
38
40
|
// 有一个计算属性更新就重新更新所有计算互相,避免后置依赖导致前置依赖错误
|
|
39
41
|
return computedUpdater.call(this, isUpdated);
|
|
@@ -8,28 +8,24 @@ export function deepProxy(
|
|
|
8
8
|
): object {
|
|
9
9
|
const handler = {
|
|
10
10
|
get<T extends object>(target: T, prop: keyof T & string) {
|
|
11
|
+
if (prop === "__original__") {
|
|
12
|
+
return target;
|
|
13
|
+
}
|
|
11
14
|
const val = target[prop];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
|
|
16
|
+
// val不为undefined(依赖还为初始化的其他计算属性)且非自身属性或函数不再返回代理。 (如 this.data.arr.slice(),slice不属于自身属性,小程序允许data子字段为函数的情况,也不代理)
|
|
17
|
+
if ((val !== undefined) && (!Object.prototype.hasOwnProperty.call(target, prop) || typeof val === "function")) {
|
|
18
|
+
// val有不是函数的情况么?
|
|
19
|
+
return (val as Function).bind(target);
|
|
20
|
+
}
|
|
21
|
+
// 依赖长度不为0时径依赖去重(只留最后一个路径),比如 return this.data.obj.user.name 去重得到的是最后1个路径 ['obj','user','name'] , 不去重则3个依赖路径了 ['obj'], ['obj','user'],['obj','user','name'],在这里去重效率高些,外部去重时还要再次遍历,加"dependences[dependences.length - 1].paths.toString() === basePath.toString()"这判断是有可能遇到` this.data.obj[this.data.id].list`的情况,这种情况下,第一个依赖是obj,第二个依赖是id,因为依赖不是一个路径连续向下的,所以不能去重。
|
|
22
|
+
if (basePath.length !== 0 && dependences[dependences.length - 1].paths.toString() === basePath.toString()) {
|
|
23
|
+
dependences.pop();
|
|
21
24
|
}
|
|
22
25
|
const curPath = basePath.concat(prop);
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
dependences.push({ paths: curPath, val: deepClone(val) });
|
|
27
|
+
dependences.push({ paths: curPath, val });
|
|
27
28
|
|
|
28
|
-
// 自身方法或原型属性不代理
|
|
29
|
-
if (!Object.prototype.hasOwnProperty.call(target, prop) || typeof val === "function") {
|
|
30
|
-
// 原型链上的属性不代理,函数加bind。如 this.data.arr.slice() this.data.arr.forEach(...)
|
|
31
|
-
return typeof val === "function" ? val.bind(target) : val;
|
|
32
|
-
}
|
|
33
29
|
// 非对象不代理
|
|
34
30
|
if (typeof val !== "object" || val === null) return val;
|
|
35
31
|
|
|
@@ -42,3 +38,11 @@ export function deepProxy(
|
|
|
42
38
|
|
|
43
39
|
return new Proxy(data, handler);
|
|
44
40
|
}
|
|
41
|
+
|
|
42
|
+
export function getProxyOriginalValue<T extends { __original__?: string }>(value: T): unknown {
|
|
43
|
+
if (typeof value !== "object" || value === null || !value.__original__) {
|
|
44
|
+
return value;
|
|
45
|
+
} else {
|
|
46
|
+
return deepClone(value.__original__);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -2,7 +2,7 @@ import type { Func } from "hry-types/src/Misc/Func";
|
|
|
2
2
|
import { computedUpdater } from "./computedUpdater";
|
|
3
3
|
import { getPathsValue } from "./getPathsValue";
|
|
4
4
|
import { getPropertiesValue } from "./getPropertiesValue";
|
|
5
|
-
import {
|
|
5
|
+
import { initComputedAndGetCache } from "./initComputedAndGetCache";
|
|
6
6
|
import { isEqual } from "./isEqual";
|
|
7
7
|
|
|
8
8
|
import { deepClone } from "../../../../utils/deepClone";
|
|
@@ -25,25 +25,29 @@ function initWatchOldValue(data: FinalOptionsOfComponent["data"], watchConfig: o
|
|
|
25
25
|
return watchOldValue;
|
|
26
26
|
}
|
|
27
27
|
export function computedWatchHandle(options: FinalOptionsOfComponent) {
|
|
28
|
-
//
|
|
28
|
+
// 计算属性初始化和首次依赖收集
|
|
29
29
|
const computedConfig = options.computed;
|
|
30
30
|
const rawPropertiesValue = getPropertiesValue(options.properties);
|
|
31
31
|
if (computedConfig && !isEmptyObject(computedConfig)) {
|
|
32
32
|
// 此时store已经初始化数据到data了
|
|
33
33
|
|
|
34
|
-
const __computedInitCache__ =
|
|
34
|
+
const __computedInitCache__ = initComputedAndGetCache(options, computedConfig, {
|
|
35
|
+
...options.data,
|
|
36
|
+
...rawPropertiesValue,
|
|
37
|
+
});
|
|
35
38
|
|
|
39
|
+
// 缓存放入data中
|
|
36
40
|
options.data.__computedCache__ = __computedInitCache__;
|
|
37
41
|
|
|
42
|
+
// 计算属性更新函数放入methods中 要做冲突判断,避免用户定义了同名methods字段
|
|
38
43
|
options.methods.__computedUpdater__ = computedUpdater;
|
|
39
|
-
// // 把计算属性缓存从方法中带入到实例中,在created周期加入实例后删除
|
|
40
|
-
// methodOpt.__computedInitCache__ = () => computedCache;
|
|
41
44
|
}
|
|
42
45
|
const observersConfig = options.observers;
|
|
43
46
|
// 通过observers加入`**`字段来触发计算属性更新
|
|
44
47
|
const originalFunc = observersConfig["**"] as Func | undefined;
|
|
45
48
|
|
|
46
49
|
observersConfig["**"] = function(this: Instance): undefined {
|
|
50
|
+
// 任何setData都会触发计算属性更新(可能依赖数据并没变化)
|
|
47
51
|
this.__computedUpdater__?.();
|
|
48
52
|
|
|
49
53
|
originalFunc && originalFunc.call(this);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Func } from "hry-types/src/Misc/Func";
|
|
2
|
+
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
+
|
|
4
|
+
import type { ComputedDependence } from "./computedUpdater";
|
|
5
|
+
import { deepProxy, getProxyOriginalValue } from "./data-tracer";
|
|
6
|
+
import { uniqueDependences } from "./uniqueDependences";
|
|
7
|
+
|
|
8
|
+
type ItemCache = {
|
|
9
|
+
dependences: ComputedDependence[];
|
|
10
|
+
method: Func;
|
|
11
|
+
value: unknown;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ComputedId = string;
|
|
15
|
+
|
|
16
|
+
export type ComputedCache = Record<ComputedId, ItemCache>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 如果依赖列表某项的首个字段值为undefined并且字段为其他计算属性字段 返回false(即被依赖的计算字段写在了依赖他的计算字段后面), 否则返回true表示依赖有效。
|
|
20
|
+
*/
|
|
21
|
+
function isValidDependences(dependences: ComputedDependence[], computedKeys: string[]): boolean {
|
|
22
|
+
for (const { paths: path, val } of dependences) {
|
|
23
|
+
if ((val === undefined) && computedKeys.includes(path[0])) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 依赖有效
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 把计算属性初始值加入到options.data中并返回缓存(递归函数)
|
|
34
|
+
* @param options - 配置选项
|
|
35
|
+
* @param computedConfig - 计算字段配置
|
|
36
|
+
* @param initAllData - 初始化时全部的data包含(properties,data,和已经初始化后的computed字段)
|
|
37
|
+
* @param uninitedkeys - 未初始化的key (默认为所有computedConfig的key)
|
|
38
|
+
* @param computedCache - 返回的所有计算字段缓存(默认为空对象)
|
|
39
|
+
* @returns `computedCache` 计算字段缓存
|
|
40
|
+
*/
|
|
41
|
+
export function initComputedAndGetCache(
|
|
42
|
+
options: FinalOptionsOfComponent,
|
|
43
|
+
computedConfig: Record<string, Func>,
|
|
44
|
+
initAllData: Record<string, unknown>,
|
|
45
|
+
uninitedkeys: string[] = Object.keys(computedConfig),
|
|
46
|
+
computedCache: ComputedCache = {},
|
|
47
|
+
): ComputedCache {
|
|
48
|
+
for (const key of uninitedkeys) {
|
|
49
|
+
const computedFunc = computedConfig[key];
|
|
50
|
+
const dependences: ComputedDependence[] = [];
|
|
51
|
+
// 通过代理data获取计算字段的初始值和依赖
|
|
52
|
+
let initValue = computedFunc.call({ data: deepProxy(initAllData, dependences) });
|
|
53
|
+
|
|
54
|
+
// 去除当前已初始的计算属性key
|
|
55
|
+
uninitedkeys = uninitedkeys.filter(ele => ele !== key);
|
|
56
|
+
|
|
57
|
+
// 验证依赖是否有效
|
|
58
|
+
if (isValidDependences(dependences, uninitedkeys)) {
|
|
59
|
+
initValue = getProxyOriginalValue(initValue);
|
|
60
|
+
|
|
61
|
+
// 把计算属性初始值加入到options.data中
|
|
62
|
+
options.data[key] = initValue;
|
|
63
|
+
|
|
64
|
+
// 把计算属性初始值加入到initAllData中,后续其他计算属性依赖时会可能会用到
|
|
65
|
+
initAllData[key] = initValue;
|
|
66
|
+
|
|
67
|
+
computedCache[key] = {
|
|
68
|
+
dependences: uniqueDependences(dependences),
|
|
69
|
+
method: computedFunc,
|
|
70
|
+
value: initValue,
|
|
71
|
+
};
|
|
72
|
+
} else {
|
|
73
|
+
// 把当前依赖不正确的key放到后面去
|
|
74
|
+
uninitedkeys.push(key);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// 看uninitedkey是否未空,空表示所有依赖收集完毕直接返回
|
|
78
|
+
if (uninitedkeys.length === 0) {
|
|
79
|
+
return computedCache;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// uninitedkey不为空,递归
|
|
83
|
+
return initComputedAndGetCache(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
84
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ComputedDependence } from "./computedUpdater";
|
|
2
|
+
|
|
3
|
+
export /**
|
|
4
|
+
* 依赖去重
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const dependences = [{path:['a','b']}, {path:['a','b','c']}, {path:['d','e']}, {path:['d']}]
|
|
8
|
+
* const result = uniqueDependences(dependences)
|
|
9
|
+
* // result = [{path:['a','b'] } ,{ path:['d'] }]
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function uniqueDependences(dependences: ComputedDependence[]): ComputedDependence[] {
|
|
13
|
+
if (dependences.length === 1) return dependences;
|
|
14
|
+
for (let f = 0; f < dependences.length; f++) {
|
|
15
|
+
const firstPath = dependences[f].paths.join(".") + ".";
|
|
16
|
+
for (let i = f + 1; i < dependences.length; i++) {
|
|
17
|
+
const curPath = dependences[i].paths.join(".") + ".";
|
|
18
|
+
if (firstPath.startsWith(curPath)) {
|
|
19
|
+
// 例如 path[0] = 'a.b.c.',curPath = 'a.b.'
|
|
20
|
+
dependences.splice(f, 1);
|
|
21
|
+
|
|
22
|
+
return uniqueDependences(dependences);
|
|
23
|
+
}
|
|
24
|
+
if (curPath.startsWith(firstPath)) {
|
|
25
|
+
// 例如 curPath = 'a.b.' path[0] = 'a.b.c.',
|
|
26
|
+
dependences.splice(i, 1);
|
|
27
|
+
|
|
28
|
+
return uniqueDependences(dependences);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return dependences;
|
|
34
|
+
}
|
|
@@ -27,7 +27,7 @@ import type { DefineComponentOption } from "..";
|
|
|
27
27
|
import { BStore } from "../../../behaviors/BStore";
|
|
28
28
|
import { computedWatchHandle } from "./computedWatchHandle";
|
|
29
29
|
|
|
30
|
-
import type { ComputedCache } from "./computedWatchHandle/
|
|
30
|
+
import type { ComputedCache } from "./computedWatchHandle/initComputedAndGetCache";
|
|
31
31
|
import { initStore } from "./initStore";
|
|
32
32
|
|
|
33
33
|
type InstanceInnerFields = {
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Func } from "hry-types/src/Misc/Func";
|
|
2
|
-
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
-
import type { ComputedDependence } from "./computedUpdater";
|
|
4
|
-
type ItemCache = {
|
|
5
|
-
dependences: ComputedDependence[];
|
|
6
|
-
method: Func;
|
|
7
|
-
value: unknown;
|
|
8
|
-
};
|
|
9
|
-
export type ComputedCache = Record<string, ItemCache>;
|
|
10
|
-
export declare function getItemCache(initAllData: object, computedKeys: string[], computedFunc: Func): false | ItemCache;
|
|
11
|
-
export declare function initComputed(options: FinalOptionsOfComponent, computedConfig: Record<string, Func>, initAllData: object, uninitedkeys?: string[], computedCache?: ComputedCache): ComputedCache;
|
|
12
|
-
export {};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { deepClone } from "../../../../utils/deepClone";
|
|
2
|
-
import { deepProxy } from "./data-tracer";
|
|
3
|
-
function isValidDependences(dependences, computedKeys) {
|
|
4
|
-
for (const { paths: path, val } of dependences) {
|
|
5
|
-
if ((val === undefined) && computedKeys.includes(path[0])) {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
return true;
|
|
10
|
-
}
|
|
11
|
-
export function getItemCache(initAllData, computedKeys, computedFunc) {
|
|
12
|
-
const dependences = [];
|
|
13
|
-
const initValue = computedFunc.call({ data: deepProxy(initAllData, dependences) });
|
|
14
|
-
if (isValidDependences(dependences, computedKeys)) {
|
|
15
|
-
return {
|
|
16
|
-
dependences: uniqueDependences(dependences),
|
|
17
|
-
method: computedFunc,
|
|
18
|
-
value: deepClone(initValue),
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function uniqueDependences(dependences) {
|
|
26
|
-
if (dependences.length === 1)
|
|
27
|
-
return dependences;
|
|
28
|
-
for (let f = 0; f < dependences.length; f++) {
|
|
29
|
-
const firstPath = dependences[f].paths.join(".") + ".";
|
|
30
|
-
for (let i = f + 1; i < dependences.length; i++) {
|
|
31
|
-
const curPath = dependences[i].paths.join(".") + ".";
|
|
32
|
-
if (firstPath.startsWith(curPath)) {
|
|
33
|
-
dependences.splice(f, 1);
|
|
34
|
-
return uniqueDependences(dependences);
|
|
35
|
-
}
|
|
36
|
-
if (curPath.startsWith(firstPath)) {
|
|
37
|
-
dependences.splice(i, 1);
|
|
38
|
-
return uniqueDependences(dependences);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return dependences;
|
|
43
|
-
}
|
|
44
|
-
export function initComputed(options, computedConfig, initAllData, uninitedkeys = Object.keys(computedConfig), computedCache = {}) {
|
|
45
|
-
for (const key of uninitedkeys) {
|
|
46
|
-
const itemCache = getItemCache(initAllData, uninitedkeys, computedConfig[key]);
|
|
47
|
-
uninitedkeys = uninitedkeys.filter(ele => ele !== key);
|
|
48
|
-
if (itemCache === false) {
|
|
49
|
-
uninitedkeys.push(key);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
const dataOpt = options.data;
|
|
53
|
-
dataOpt[key] = itemCache.value;
|
|
54
|
-
initAllData[key] = itemCache.value;
|
|
55
|
-
computedCache[key] = itemCache;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (uninitedkeys.length === 0) {
|
|
59
|
-
return computedCache;
|
|
60
|
-
}
|
|
61
|
-
return initComputed(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
62
|
-
}
|
|
63
|
-
//# sourceMappingURL=initComputed.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"initComputed.js","sourceRoot":"","sources":["../../../../../src/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAA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C,GAAG,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC;YAEnC,aAAa,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC;QACjC,CAAC;IACH,CAAC;IAED,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,OAAO,aAAa,CAAC;IACvB,CAAC;IAGD,OAAO,YAAY,CAAC,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,CAAC,CAAC;AACzF,CAAC"}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import type { Func } from "hry-types/src/Misc/Func";
|
|
2
|
-
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
-
|
|
4
|
-
import { deepClone } from "../../../../utils/deepClone";
|
|
5
|
-
import type { ComputedDependence } from "./computedUpdater";
|
|
6
|
-
import { deepProxy } from "./data-tracer";
|
|
7
|
-
|
|
8
|
-
type ItemCache = {
|
|
9
|
-
dependences: ComputedDependence[];
|
|
10
|
-
method: Func;
|
|
11
|
-
value: unknown;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type ComputedCache = Record<string, ItemCache>;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 如果依赖列表某项的首个字段值为undefined并且字段为其他计算属性字段 返回false(即被依赖的计算字段写在了依赖他的计算字段后面), 否则返回ComputedInfo。
|
|
18
|
-
*/
|
|
19
|
-
function isValidDependences(dependences: ComputedDependence[], computedKeys: string[]): boolean {
|
|
20
|
-
for (const { paths: path, val } of dependences) {
|
|
21
|
-
// 引用值为undefined时
|
|
22
|
-
// console.log(path[0]);
|
|
23
|
-
if ((val === undefined) && computedKeys.includes(path[0])) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return true; // 依赖有效
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function getItemCache(
|
|
32
|
-
initAllData: object,
|
|
33
|
-
computedKeys: string[],
|
|
34
|
-
computedFunc: Func,
|
|
35
|
-
): false | ItemCache {
|
|
36
|
-
// 当前计算字段的依赖列表
|
|
37
|
-
const dependences: ComputedDependence[] = [];
|
|
38
|
-
const initValue = computedFunc.call({ data: deepProxy(initAllData, dependences) });
|
|
39
|
-
|
|
40
|
-
if (isValidDependences(dependences, computedKeys)) {
|
|
41
|
-
return {
|
|
42
|
-
// 去重 避免 如: ` age(){ return this.data.xxx.num + this.data.xxx.str + this.data.xxx} ` 造成的多个依赖相同情况。应就要一个 xxx
|
|
43
|
-
dependences: uniqueDependences(dependences),
|
|
44
|
-
method: computedFunc,
|
|
45
|
-
value: deepClone(initValue),
|
|
46
|
-
};
|
|
47
|
-
} else {
|
|
48
|
-
/**
|
|
49
|
-
* 情形1 依赖了其他未初始化的计算属性(不报错) 对应测试文件[NoOptionalProperties.ts](../../../jest/computed/NoOptionalProperties/NoOptionalProperties.test.ts)的copyPropUser字段
|
|
50
|
-
*/
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 依赖去重
|
|
57
|
-
* @example
|
|
58
|
-
* ```ts
|
|
59
|
-
* const dependences = [{path:['a','b']}, {path:['a','b','c']}, {path:['d','e']}, {path:['d']}]
|
|
60
|
-
* const result = uniqueDependences(dependences)
|
|
61
|
-
* // result = [{path:['a','b'] } ,{ path:['d'] }]
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
function uniqueDependences(dependences: ComputedDependence[]): ComputedDependence[] {
|
|
65
|
-
if (dependences.length === 1) return dependences;
|
|
66
|
-
// console.log(dependences);
|
|
67
|
-
|
|
68
|
-
for (let f = 0; f < dependences.length; f++) {
|
|
69
|
-
const firstPath = dependences[f].paths.join(".") + ".";
|
|
70
|
-
for (let i = f + 1; i < dependences.length; i++) {
|
|
71
|
-
const curPath = dependences[i].paths.join(".") + ".";
|
|
72
|
-
// console.log(firstPath,curPath)
|
|
73
|
-
if (firstPath.startsWith(curPath)) {
|
|
74
|
-
// console.log("删除:", curPath, f);
|
|
75
|
-
|
|
76
|
-
// 例如 path[0] = 'a.b.c.',curPath = 'a.b.'
|
|
77
|
-
dependences.splice(f, 1);
|
|
78
|
-
|
|
79
|
-
return uniqueDependences(dependences);
|
|
80
|
-
}
|
|
81
|
-
if (curPath.startsWith(firstPath)) {
|
|
82
|
-
// console.log("删除:", firstPath, i);
|
|
83
|
-
|
|
84
|
-
// 例如 curPath = 'a.b.' path[0] = 'a.b.c.',
|
|
85
|
-
dependences.splice(i, 1);
|
|
86
|
-
|
|
87
|
-
return uniqueDependences(dependences);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return dependences;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 把计算属性初始值加入到options.data中返回缓存
|
|
97
|
-
*/
|
|
98
|
-
export function initComputed(
|
|
99
|
-
options: FinalOptionsOfComponent,
|
|
100
|
-
computedConfig: Record<string, Func>,
|
|
101
|
-
initAllData: object,
|
|
102
|
-
uninitedkeys: string[] = Object.keys(computedConfig),
|
|
103
|
-
computedCache: ComputedCache = {},
|
|
104
|
-
): ComputedCache {
|
|
105
|
-
// 收集依赖 uninitedkeys不受内部2处重新赋值的影响
|
|
106
|
-
|
|
107
|
-
for (const key of uninitedkeys) {
|
|
108
|
-
// uninitedkeys 受2处重新赋值的影响
|
|
109
|
-
const itemCache = getItemCache(initAllData, uninitedkeys, computedConfig[key]);
|
|
110
|
-
|
|
111
|
-
// 去除当前的key 2
|
|
112
|
-
uninitedkeys = uninitedkeys.filter(ele => ele !== key);
|
|
113
|
-
|
|
114
|
-
if (itemCache === false) {
|
|
115
|
-
// 把当前依赖不正确的key放到后面去
|
|
116
|
-
uninitedkeys.push(key);
|
|
117
|
-
} else {
|
|
118
|
-
const dataOpt = options.data;
|
|
119
|
-
|
|
120
|
-
dataOpt[key] = itemCache.value;
|
|
121
|
-
|
|
122
|
-
// @ts-ignore 隐式索引
|
|
123
|
-
initAllData[key] = itemCache.value;
|
|
124
|
-
|
|
125
|
-
computedCache[key] = itemCache;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
// 看uninitedkey(2处的新值)是否未空,空表示所有依赖收集完毕直接返回
|
|
129
|
-
if (uninitedkeys.length === 0) {
|
|
130
|
-
return computedCache;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// uninitedkey不为空,递归
|
|
134
|
-
return initComputed(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
135
|
-
}
|