annil 1.5.8 → 1.5.10
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/README.md +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.d.ts +6 -0
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/computedUpdater.js +3 -3
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/computedUpdater.js.map +1 -0
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/data-tracer.d.ts +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/data-tracer.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/getPathsValue.js.map +1 -0
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPropertiesValue.d.ts +1 -1
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPropertiesValue.js +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/getPropertiesValue.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/index.d.ts +2 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/index.js +69 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/index.js.map +1 -0
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/initComputed.d.ts +8 -5
- package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/initComputed.js +1 -1
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/initComputed.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/computedWatchHandle/isEqual.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/index.d.ts +49 -0
- package/dist/api/DefineComponent/{collectOptionsForComponent.js → assignOptions/index.js} +35 -23
- package/dist/api/DefineComponent/assignOptions/index.js.map +1 -0
- package/dist/api/DefineComponent/assignOptions/initStore/index.d.ts +2 -0
- package/dist/api/DefineComponent/assignOptions/initStore/index.js +12 -0
- package/dist/api/DefineComponent/assignOptions/initStore/index.js.map +1 -0
- package/dist/api/DefineComponent/index.d.ts +1 -1
- package/dist/api/DefineComponent/index.js +2 -2
- package/dist/api/DefineComponent/index.js.map +1 -1
- package/dist/api/InstanceInject/instanceConfig.d.ts +1 -1
- package/dist/api/InstanceInject/instanceConfig.js.map +1 -1
- package/dist/api/RootComponent/Lifetimes/LifetimesConstraint.d.ts +1 -1
- package/dist/api/RootComponent/Observers/ObserversOption.d.ts +1 -1
- package/dist/api/SubComponent/index.d.ts +2 -1
- package/dist/api/SubComponent/index.js.map +1 -1
- package/dist/behaviors/BStore.js +1 -14
- package/dist/behaviors/BStore.js.map +1 -1
- package/dist/types/AssignValues.d.ts +9 -0
- package/dist/types/AssignValues.js +2 -0
- package/dist/types/AssignValues.js.map +1 -0
- package/dist/utils/deepClone.js.map +1 -1
- package/package.json +1 -1
- package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/computedUpdater.ts +6 -5
- package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/data-tracer.ts +1 -1
- package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPropertiesValue.ts +3 -4
- package/src/api/DefineComponent/assignOptions/computedWatchHandle/index.ts +110 -0
- package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/initComputed.ts +13 -8
- package/src/api/DefineComponent/{collectOptionsForComponent.ts → assignOptions/index.ts} +108 -61
- package/src/api/DefineComponent/assignOptions/initStore/index.ts +16 -0
- package/src/api/DefineComponent/index.ts +3 -6
- package/src/api/DefineComponent/storeDataHandle/index.ts +30 -0
- package/src/api/InstanceInject/instanceConfig.ts +2 -1
- package/src/api/RootComponent/Lifetimes/LifetimesConstraint.ts +1 -1
- package/src/api/RootComponent/Observers/ObserversOption.ts +2 -2
- package/src/api/SubComponent/SubComputed/test/normal.test.ts +5 -5
- package/src/api/SubComponent/SubData/test/normal.test.ts +3 -3
- package/src/api/SubComponent/SubInstance/test/normal.test.ts +2 -2
- package/src/api/SubComponent/index.ts +3 -2
- package/src/behaviors/BStore.ts +4 -22
- package/src/behaviors/BbeforeCreated.ts +1 -1
- package/src/types/AssignValues.ts +26 -0
- package/src/utils/deepClone.ts +1 -2
- package/dist/api/DefineComponent/collectOptionsForComponent.d.ts +0 -31
- package/dist/api/DefineComponent/collectOptionsForComponent.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/computedUpdater.d.ts +0 -2
- package/dist/behaviors/BComputedAndWatch/computedUpdater.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/data-tracer.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/getPathsValue.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/getPropertiesValue.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/index.d.ts +0 -1
- package/dist/behaviors/BComputedAndWatch/index.js +0 -85
- package/dist/behaviors/BComputedAndWatch/index.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/initComputed.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/isEqual.js.map +0 -1
- package/dist/behaviors/BComputedAndWatch/types.d.ts +0 -24
- package/dist/behaviors/BComputedAndWatch/types.js +0 -2
- package/dist/behaviors/BComputedAndWatch/types.js.map +0 -1
- package/dist/utils/deleteProtoField.d.ts +0 -1
- package/dist/utils/deleteProtoField.js +0 -4
- package/dist/utils/deleteProtoField.js.map +0 -1
- package/src/behaviors/BComputedAndWatch/index.ts +0 -135
- package/src/behaviors/BComputedAndWatch/types.ts +0 -30
- package/src/utils/deleteProtoField.ts +0 -6
- /package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/data-tracer.js +0 -0
- /package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPathsValue.d.ts +0 -0
- /package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPathsValue.js +0 -0
- /package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/isEqual.d.ts +0 -0
- /package/dist/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/isEqual.js +0 -0
- /package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/getPathsValue.ts +0 -0
- /package/src/{behaviors/BComputedAndWatch → api/DefineComponent/assignOptions/computedWatchHandle}/isEqual.ts +0 -0
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import type { Instance } from "..";
|
|
2
|
+
|
|
1
3
|
import { deepProxy, unwrap } from "./data-tracer";
|
|
2
4
|
import { getPathsValue } from "./getPathsValue";
|
|
3
5
|
|
|
4
|
-
import type { ComputedDependence } from "./initComputed";
|
|
5
6
|
import { isEqual } from "./isEqual";
|
|
6
|
-
|
|
7
|
+
export type ComputedDependence = { paths: string[]; val: unknown };
|
|
7
8
|
|
|
8
9
|
export function computedUpdater(this: Instance, isUpdated = false): boolean {
|
|
9
|
-
for (const key in this.__computedCache__) {
|
|
10
|
-
const itemCache = this.__computedCache__[key];
|
|
10
|
+
for (const key in this.data.__computedCache__) {
|
|
11
|
+
const itemCache = this.data.__computedCache__[key];
|
|
11
12
|
let changed = false;
|
|
12
13
|
for (const dep of itemCache.dependences) {
|
|
13
14
|
// getPathsValue返回的是数组
|
|
@@ -32,7 +33,7 @@ export function computedUpdater(this: Instance, isUpdated = false): boolean {
|
|
|
32
33
|
isUpdated = true;
|
|
33
34
|
|
|
34
35
|
// 更新依赖
|
|
35
|
-
this.__computedCache__[key].dependences = newDependences;
|
|
36
|
+
this.data.__computedCache__[key].dependences = newDependences;
|
|
36
37
|
|
|
37
38
|
// 有一个计算属性更新就重新更新所有计算互相,避免后置依赖导致前置依赖错误
|
|
38
39
|
return computedUpdater.call(this, isUpdated);
|
|
@@ -3,11 +3,10 @@ import type {
|
|
|
3
3
|
PropertiesTypes,
|
|
4
4
|
RequiredSingle,
|
|
5
5
|
RequiredUnion,
|
|
6
|
-
} from "
|
|
6
|
+
} from "../../../RootComponent/Properties/PropertiesConstraint";
|
|
7
7
|
|
|
8
8
|
type PropertiesDefaultValue = "" | 0 | [] | null | false;
|
|
9
9
|
|
|
10
|
-
/* istanbul ignore next */
|
|
11
10
|
function getRequiredSingleValue(
|
|
12
11
|
PropType: RequiredSingle,
|
|
13
12
|
): PropertiesDefaultValue {
|
|
@@ -25,7 +24,7 @@ function getRequiredSingleValue(
|
|
|
25
24
|
default:
|
|
26
25
|
/* istanbul ignore next */
|
|
27
26
|
throw Error(
|
|
28
|
-
"properties字段类型只能为 String | Number | Array | Object | Boolean",
|
|
27
|
+
"properties字段类型只能为 String | Number | Array | Object | Boolean ",
|
|
29
28
|
);
|
|
30
29
|
}
|
|
31
30
|
}
|
|
@@ -40,7 +39,7 @@ export function getPropertiesValue(propertiesOpt: PropertiesConstraint | undefin
|
|
|
40
39
|
const result = {};
|
|
41
40
|
for (const key in propertiesOpt) {
|
|
42
41
|
const config = propertiesOpt[key];
|
|
43
|
-
|
|
42
|
+
|
|
44
43
|
if (IsRequiredSingle(config)) {
|
|
45
44
|
// @ts-ignore 隐式索引
|
|
46
45
|
result[key] = getRequiredSingleValue(config);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Func } from "hry-types/src/Misc/Func";
|
|
2
|
+
import { computedUpdater } from "./computedUpdater";
|
|
3
|
+
import { getPathsValue } from "./getPathsValue";
|
|
4
|
+
import { getPropertiesValue } from "./getPropertiesValue";
|
|
5
|
+
import { initComputed } from "./initComputed";
|
|
6
|
+
import { isEqual } from "./isEqual";
|
|
7
|
+
|
|
8
|
+
import { deepClone } from "../../../../utils/deepClone";
|
|
9
|
+
import { isEmptyObject } from "../../../../utils/isEmptyObject";
|
|
10
|
+
import type { FinalOptionsOfComponent, Instance } from "..";
|
|
11
|
+
|
|
12
|
+
type WatchOldValue = Record<string, unknown[]>;
|
|
13
|
+
|
|
14
|
+
function initWatchOldValue(data: FinalOptionsOfComponent["data"], watchConfig: object): WatchOldValue {
|
|
15
|
+
const watchOldValue = {};
|
|
16
|
+
for (const key in watchConfig) {
|
|
17
|
+
// watchOldValue[key] = deepClone(getPathsValue(data, key));
|
|
18
|
+
|
|
19
|
+
// watchOldValue[key] = getPathsValue(data, key);
|
|
20
|
+
|
|
21
|
+
// @ts-ignore 隐式索引
|
|
22
|
+
watchOldValue[key] = deepClone(getPathsValue(data, key));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return watchOldValue;
|
|
26
|
+
}
|
|
27
|
+
export function computedWatchHandle(options: FinalOptionsOfComponent) {
|
|
28
|
+
// 计算属性初始化
|
|
29
|
+
const computedConfig = options.computed;
|
|
30
|
+
const rawPropertiesValue = getPropertiesValue(options.properties);
|
|
31
|
+
if (computedConfig && !isEmptyObject(computedConfig)) {
|
|
32
|
+
// 此时store已经初始化数据到data了
|
|
33
|
+
|
|
34
|
+
const __computedInitCache__ = initComputed(options, computedConfig, { ...options.data, ...rawPropertiesValue });
|
|
35
|
+
|
|
36
|
+
options.data.__computedCache__ = __computedInitCache__;
|
|
37
|
+
|
|
38
|
+
options.data.__computedStatus__ = "待更新";
|
|
39
|
+
|
|
40
|
+
options.methods.__computedUpdater__ = computedUpdater;
|
|
41
|
+
// // 把计算属性缓存从方法中带入到实例中,在created周期加入实例后删除
|
|
42
|
+
// methodOpt.__computedInitCache__ = () => computedCache;
|
|
43
|
+
}
|
|
44
|
+
const observersConfig = options.observers;
|
|
45
|
+
// 通过observers加入`**`字段来触发计算属性更新
|
|
46
|
+
const originalFunc = observersConfig["**"] as Func | undefined;
|
|
47
|
+
|
|
48
|
+
observersConfig["**"] = function(this: Instance): undefined {
|
|
49
|
+
const computedStatus = this.data.__computedStatus__;
|
|
50
|
+
|
|
51
|
+
// __computedStatus__为undefined表示无计算属性,不处理
|
|
52
|
+
|
|
53
|
+
switch (computedStatus) {
|
|
54
|
+
case "待更新":
|
|
55
|
+
// 3. 触发来自attached后的setData或properties更新
|
|
56
|
+
{
|
|
57
|
+
const isUpdated = this.__computedUpdater__!();
|
|
58
|
+
|
|
59
|
+
if (isUpdated) {
|
|
60
|
+
// 更新了会再次触发自身转到 4
|
|
61
|
+
this.data.__computedStatus__ = "更新完毕";
|
|
62
|
+
} else {
|
|
63
|
+
// 无需更新计算属性
|
|
64
|
+
|
|
65
|
+
originalFunc && originalFunc.call(this);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "更新完毕":
|
|
70
|
+
{
|
|
71
|
+
// 4 来自计算属性更新后的自身回调
|
|
72
|
+
// console.log("来自计算属性更新后的自身回调");
|
|
73
|
+
this.data.__computedStatus__ = "待更新";
|
|
74
|
+
|
|
75
|
+
originalFunc && originalFunc.call(this);
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
// 没有计算属性时
|
|
80
|
+
originalFunc && originalFunc.call(this);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// watch handle
|
|
84
|
+
const watchConfig = options.watch;
|
|
85
|
+
|
|
86
|
+
if (watchConfig && !isEmptyObject(watchConfig)) {
|
|
87
|
+
const data = options.data;
|
|
88
|
+
|
|
89
|
+
data.__watchOldValue__ = initWatchOldValue({ ...data, ...rawPropertiesValue }, watchConfig);
|
|
90
|
+
|
|
91
|
+
const observersConfig = options.observers;
|
|
92
|
+
for (const key in watchConfig) {
|
|
93
|
+
const watchHadle = watchConfig[key];
|
|
94
|
+
const originObserversHandle = observersConfig[key] as Func | undefined;
|
|
95
|
+
|
|
96
|
+
// 在监控多个数据时,参数是多个值
|
|
97
|
+
observersConfig[key] = function(this: Instance, ...newValue: unknown[]) {
|
|
98
|
+
originObserversHandle && originObserversHandle.call(this, ...newValue);
|
|
99
|
+
|
|
100
|
+
const watchOldValue = this.data.__watchOldValue__!;
|
|
101
|
+
const oldValue = watchOldValue[key];
|
|
102
|
+
|
|
103
|
+
if (isEqual(newValue, oldValue)) return;
|
|
104
|
+
watchOldValue[key] = deepClone(newValue);
|
|
105
|
+
|
|
106
|
+
watchHadle.call(this, ...newValue, ...oldValue);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
// import type { ComponentInstance } from "../../api/RootComponent/Instance/RootComponentInstance";
|
|
2
|
-
// import type { IsDependOnOthers } from "./data-tracer";
|
|
3
|
-
|
|
4
1
|
import type { Func } from "hry-types/src/Misc/Func";
|
|
5
|
-
import type { FinalOptionsOfComponent } from "
|
|
2
|
+
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
+
|
|
4
|
+
import type { ComputedDependence } from "./computedUpdater";
|
|
6
5
|
import { deepProxy, unwrap } from "./data-tracer";
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
type ItemCache = {
|
|
8
|
+
dependences: ComputedDependence[];
|
|
9
|
+
method: Func;
|
|
10
|
+
value: unknown;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type ComputedCache = Record<string, ItemCache>;
|
|
9
14
|
|
|
10
15
|
/**
|
|
11
16
|
* 如果依赖列表某项的首个字段值为undefined并且字段为其他计算属性字段 返回false(即被依赖的计算字段写在了依赖他的计算字段后面), 否则返回ComputedInfo。
|
|
@@ -97,6 +102,7 @@ export function initComputed(
|
|
|
97
102
|
computedCache: ComputedCache = {},
|
|
98
103
|
): ComputedCache {
|
|
99
104
|
// 收集依赖 uninitedkeys不受内部2处重新赋值的影响
|
|
105
|
+
|
|
100
106
|
for (const key of uninitedkeys) {
|
|
101
107
|
// uninitedkeys 受2处重新赋值的影响
|
|
102
108
|
const itemCache = getItemCache(initAllData, uninitedkeys, computedConfig[key]);
|
|
@@ -108,8 +114,7 @@ export function initComputed(
|
|
|
108
114
|
// 把当前依赖不正确的key放到后面去
|
|
109
115
|
uninitedkeys.push(key);
|
|
110
116
|
} else {
|
|
111
|
-
|
|
112
|
-
const dataOpt = options.data ||= {};
|
|
117
|
+
const dataOpt = options.data;
|
|
113
118
|
|
|
114
119
|
dataOpt[key] = itemCache.value;
|
|
115
120
|
|
|
@@ -1,47 +1,75 @@
|
|
|
1
1
|
import type { Func } from "hry-types/src/Misc/_api";
|
|
2
|
-
import { BBeforeCreate } from "
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import type { RootComponentTrueOptions } from "../RootComponent";
|
|
13
|
-
import type { ComputedConstraint } from "../RootComponent/Computed/ComputedConstraint";
|
|
2
|
+
import { BBeforeCreate } from "../../../behaviors/BbeforeCreated";
|
|
3
|
+
|
|
4
|
+
import type { Assign } from "../../../types/Assign";
|
|
5
|
+
import type { WMComponent } from "../../../types/OfficialTypeAlias";
|
|
6
|
+
import { deepClone } from "../../../utils/deepClone";
|
|
7
|
+
import { INNERMARKER } from "../../../utils/InnerMarker";
|
|
8
|
+
import { isEmptyObject } from "../../../utils/isEmptyObject";
|
|
9
|
+
import { instanceConfig } from "../../InstanceInject/instanceConfig";
|
|
10
|
+
import type { RootComponentTrueOptions } from "../../RootComponent";
|
|
11
|
+
import type { ComputedConstraint } from "../../RootComponent/Computed/ComputedConstraint";
|
|
14
12
|
import type {
|
|
15
13
|
CustomEventConstraint,
|
|
16
14
|
CustomEvents,
|
|
17
15
|
FullCustomEvents,
|
|
18
|
-
} from "
|
|
19
|
-
import type { DataConstraint } from "
|
|
20
|
-
import type { EventsConstraint } from "
|
|
21
|
-
import type { PageInstance } from "
|
|
22
|
-
import type { LifetimesConstraint } from "
|
|
23
|
-
import type { MethodsConstraint } from "
|
|
24
|
-
import type { PageLifetimesOption } from "
|
|
25
|
-
import type { PropertiesConstraint } from "
|
|
26
|
-
import type { StoreConstraint } from "
|
|
27
|
-
import type { SubComponentTrueOptions } from "
|
|
28
|
-
import type { DefineComponentOption } from "
|
|
16
|
+
} from "../../RootComponent/CustomEvents/CustomEventConstraint";
|
|
17
|
+
import type { DataConstraint } from "../../RootComponent/Data/DataConstraint";
|
|
18
|
+
import type { EventsConstraint } from "../../RootComponent/Events/EventsConstraint";
|
|
19
|
+
import type { ComponentInstance, PageInstance } from "../../RootComponent/Instance/RootComponentInstance";
|
|
20
|
+
import type { LifetimesConstraint } from "../../RootComponent/Lifetimes/LifetimesConstraint";
|
|
21
|
+
import type { MethodsConstraint } from "../../RootComponent/Methods/MethodsConstraint";
|
|
22
|
+
import type { PageLifetimesOption } from "../../RootComponent/PageLifetimes/PageLifetimesOption";
|
|
23
|
+
import type { PropertiesConstraint } from "../../RootComponent/Properties/PropertiesConstraint";
|
|
24
|
+
import type { StoreConstraint } from "../../RootComponent/Store/StoreConstraint";
|
|
25
|
+
import type { SubComponentTrueOptions } from "../../SubComponent";
|
|
26
|
+
import type { DefineComponentOption } from "..";
|
|
27
|
+
|
|
28
|
+
import { BStore } from "../../../behaviors/BStore";
|
|
29
|
+
import { computedWatchHandle } from "./computedWatchHandle";
|
|
30
|
+
|
|
31
|
+
import type { ComputedCache } from "./computedWatchHandle/initComputed";
|
|
32
|
+
import { initStore } from "./initStore";
|
|
33
|
+
|
|
34
|
+
type InstanceInnerFields = {
|
|
35
|
+
data: OptionsInnerFields["data"];
|
|
36
|
+
disposer: Record<string, Func>;
|
|
37
|
+
} & OptionsInnerFields["methods"];
|
|
38
|
+
|
|
39
|
+
export type Instance =
|
|
40
|
+
& (ComponentInstance | PageInstance)
|
|
41
|
+
& InstanceInnerFields;
|
|
42
|
+
|
|
29
43
|
/**
|
|
30
44
|
* 临时的函数配置项
|
|
31
45
|
* 把根组件与子组件中配置类型为函数的相同字段配置收集在一起(数组)
|
|
32
46
|
*/
|
|
33
|
-
export type FuncOptions = Record<"pageLifetimes" | "lifetimes" | "watch", Record<string, Func[]>>;
|
|
47
|
+
export type FuncOptions = Record<"pageLifetimes" | "lifetimes" | "watch" | "observers", Record<string, Func[]>>;
|
|
48
|
+
|
|
49
|
+
export type WatchOldValue = Record<string, unknown[]>;
|
|
50
|
+
|
|
51
|
+
export type OptionsInnerFields = {
|
|
52
|
+
data: {
|
|
53
|
+
__computedStatus__?: "待更新" | "更新完毕";
|
|
54
|
+
__computedCache__?: ComputedCache;
|
|
55
|
+
__storeConfig__?: StoreConstraint;
|
|
56
|
+
__watchOldValue__?: WatchOldValue;
|
|
57
|
+
};
|
|
58
|
+
methods: {
|
|
59
|
+
disposer?: Record<string, Func>;
|
|
60
|
+
// computedHandle加入
|
|
61
|
+
__computedUpdater__?: Func;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
34
64
|
|
|
35
65
|
/**
|
|
36
66
|
* 最终传入原生Component的配置项
|
|
37
67
|
*/
|
|
38
68
|
export type FinalOptionsOfComponent = {
|
|
39
|
-
data: DataConstraint;
|
|
69
|
+
data: DataConstraint & OptionsInnerFields["data"];
|
|
40
70
|
observers: Record<string, Func>;
|
|
41
71
|
behaviors: string[];
|
|
42
|
-
methods: MethodsConstraint &
|
|
43
|
-
__computedInitCache__?: () => ComputedCache;
|
|
44
|
-
};
|
|
72
|
+
methods: MethodsConstraint & OptionsInnerFields["methods"];
|
|
45
73
|
externalClasses: string[];
|
|
46
74
|
pageLifetimes: PageLifetimesOption<false, object>["pageLifetimes"] & {};
|
|
47
75
|
isPage?: boolean;
|
|
@@ -81,7 +109,7 @@ export function isPageCheck(isPage: boolean | undefined) {
|
|
|
81
109
|
* 1. 使用wx.navigateTo传值的。这种情况无内置字段 option[INNERMARKER.url] 等于 undefined
|
|
82
110
|
* 2. 使用插件提供的navigateTo传值。这种情况 INNERMARKER.url被load周期劫持函数解码后赋值INNERMARKER.url字段为本身,即option[INNERMARKER.url] 等于 INNERMARKER.url
|
|
83
111
|
*/
|
|
84
|
-
/* istanbul ignore next
|
|
112
|
+
/* istanbul ignore next miniprogram-simulate(当前版本 1.6.1) 无法测试页面生命周期 */
|
|
85
113
|
function onLoadReceivedDataHandle(
|
|
86
114
|
this: PageInstance,
|
|
87
115
|
option: Record<typeof INNERMARKER.url, string>,
|
|
@@ -107,7 +135,7 @@ function onLoadReceivedDataHandle(
|
|
|
107
135
|
* 针对通过 navigateTo传过来的数据对组件load周期传入数据解析
|
|
108
136
|
* @param option - option中的url是拼接了encodeURIComponent转码的data对象的,key为INNERMARKER.url
|
|
109
137
|
*/
|
|
110
|
-
/* istanbul ignore next
|
|
138
|
+
/* istanbul ignore next miniprogram-simulate(当前版本 1.6.1) 无法测试load */
|
|
111
139
|
function loadReceivedDataHandle(
|
|
112
140
|
this: PageInstance,
|
|
113
141
|
option: Record<typeof INNERMARKER.url, string>,
|
|
@@ -173,7 +201,7 @@ function _funcOptionsHandle(config: object, configList: Record<string, Func[]>)
|
|
|
173
201
|
/**
|
|
174
202
|
* 把函数列表配置放入一个配置中循环一次运行
|
|
175
203
|
*/
|
|
176
|
-
function
|
|
204
|
+
function assignFuncOptions(
|
|
177
205
|
finalOptionsForComponent: FinalOptionsOfComponent,
|
|
178
206
|
isPage: boolean | undefined,
|
|
179
207
|
funcOptions: FuncOptions,
|
|
@@ -198,14 +226,13 @@ function funcFieldsCollect(
|
|
|
198
226
|
options: SubComponentTrueOptions | RootComponentTrueOptions,
|
|
199
227
|
funcOptions: FuncOptions,
|
|
200
228
|
) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
(funcOptions[key][_key] ||= []).push(options[key][_key]);
|
|
229
|
+
let key: keyof FuncOptions;
|
|
230
|
+
for (key in funcOptions) {
|
|
231
|
+
const optionsKeyConfig = options[key];
|
|
232
|
+
if (optionsKeyConfig) {
|
|
233
|
+
for (const _key in optionsKeyConfig) {
|
|
234
|
+
// @ts-ignore
|
|
235
|
+
(funcOptions[key][_key] ||= []).push(optionsKeyConfig[_key]);
|
|
209
236
|
}
|
|
210
237
|
}
|
|
211
238
|
}
|
|
@@ -241,7 +268,7 @@ function otherFieldsHandle(
|
|
|
241
268
|
function eventsHandle(methods: FinalOptionsOfComponent["methods"], eventsConfig: EventsConstraint) {
|
|
242
269
|
Object.assign(methods, eventsConfig);
|
|
243
270
|
}
|
|
244
|
-
function
|
|
271
|
+
function assignSubComponentsOption(
|
|
245
272
|
componentOptions: FinalOptionsOfComponent,
|
|
246
273
|
subComponents: SubComponentTrueOptions[],
|
|
247
274
|
funcOptions: FuncOptions,
|
|
@@ -289,7 +316,7 @@ function customEventsHandle(
|
|
|
289
316
|
* @param funcOptions - 收集特殊配置对象字段
|
|
290
317
|
* @param rootComponentOptions - 被收集的源配置对象
|
|
291
318
|
*/
|
|
292
|
-
function
|
|
319
|
+
function assignRootComponentOption(
|
|
293
320
|
finalOptions: FinalOptionsOfComponent,
|
|
294
321
|
funcOptions: FuncOptions,
|
|
295
322
|
rootComponentOptions: RootComponentTrueOptions,
|
|
@@ -347,20 +374,23 @@ function merge<Target extends object, Source extends object>(target: Target, sou
|
|
|
347
374
|
* @param subComponentsList -
|
|
348
375
|
* @returns FinalOptionsForComponent
|
|
349
376
|
*/
|
|
350
|
-
export function
|
|
377
|
+
export function assignOptions(
|
|
351
378
|
defineComponentOption: DefineComponentOption,
|
|
352
379
|
): FinalOptionsOfComponent {
|
|
353
380
|
const rootComponentOption = defineComponentOption.rootComponent;
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
const finalOptionsForComponent: FinalOptionsOfComponent = merge(
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
381
|
+
const subComponentsOption = defineComponentOption.subComponents;
|
|
382
|
+
// 注入injectInfo和给默认配置(便于后续判断)
|
|
383
|
+
const finalOptionsForComponent: FinalOptionsOfComponent = merge(
|
|
384
|
+
{ ...deepClone(instanceConfig.injectInfo) },
|
|
385
|
+
{
|
|
386
|
+
observers: {},
|
|
387
|
+
data: {},
|
|
388
|
+
methods: {},
|
|
389
|
+
behaviors: [BStore],
|
|
390
|
+
externalClasses: [],
|
|
391
|
+
pageLifetimes: {},
|
|
392
|
+
},
|
|
393
|
+
);
|
|
364
394
|
|
|
365
395
|
/**
|
|
366
396
|
* 有些字段配置同时存在根组件和子组件当中(如pageLifetimes,lifetimes,watch字段), 且key相同值类型为函数。funcConfig对象用于收集这些配置为数组形式,最终再一起整合进finalOptionsForComponent配置中。即funcConfig是一个临时中介对象。
|
|
@@ -369,20 +399,30 @@ export function collectOptionsForComponent(
|
|
|
369
399
|
pageLifetimes: {},
|
|
370
400
|
lifetimes: {},
|
|
371
401
|
watch: {},
|
|
402
|
+
observers: {},
|
|
372
403
|
};
|
|
373
404
|
|
|
374
405
|
if (rootComponentOption && !isEmptyObject(rootComponentOption)) {
|
|
375
|
-
|
|
406
|
+
// 把rootComponentOption配置并入finalOptionsForComponent,其中FuncOptions字段配置收集到funcOptions中
|
|
407
|
+
assignRootComponentOption(finalOptionsForComponent, funcOptions, rootComponentOption);
|
|
376
408
|
}
|
|
377
409
|
|
|
378
|
-
if (
|
|
379
|
-
|
|
410
|
+
if (subComponentsOption && !isEmptyObject(subComponentsOption)) {
|
|
411
|
+
// 把subComponentsList配置并入finalOptionsForComponent,其中FuncOptions字段配置收集到funcOptions中
|
|
412
|
+
assignSubComponentsOption(finalOptionsForComponent, subComponentsOption, funcOptions);
|
|
380
413
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
414
|
+
// 把收集好的funcOptions并入finalOptionsForComponent
|
|
415
|
+
assignFuncOptions(finalOptionsForComponent, rootComponentOption?.isPage, funcOptions);
|
|
416
|
+
|
|
417
|
+
// 配置与内部字段冲突验证
|
|
418
|
+
InternalFieldProtection(finalOptionsForComponent.methods, ["disposer", "__computedUpdater__"]);
|
|
419
|
+
|
|
420
|
+
InternalFieldProtection(finalOptionsForComponent.data, [
|
|
421
|
+
"__computedStatus__",
|
|
422
|
+
"__computedCache__",
|
|
423
|
+
"__storeConfig__",
|
|
424
|
+
"__watchOldValue__",
|
|
425
|
+
]);
|
|
386
426
|
// 对页面传入参数进行处理 老框架劫持页面methods.onLoad,新框架劫持页面pageLifetimes.load
|
|
387
427
|
|
|
388
428
|
finalOptionsForComponent.isPage
|
|
@@ -394,16 +434,23 @@ export function collectOptionsForComponent(
|
|
|
394
434
|
[onLoadReceivedDataHandle],
|
|
395
435
|
);
|
|
396
436
|
|
|
437
|
+
// 验证isPage字段是否配置正确
|
|
397
438
|
hijack(
|
|
398
439
|
finalOptionsForComponent.lifetimes!,
|
|
399
440
|
"attached",
|
|
400
441
|
[isPageCheck(rootComponentOption?.isPage)],
|
|
401
442
|
);
|
|
402
443
|
|
|
444
|
+
// 页面时删除预设的虚拟组件字段
|
|
403
445
|
finalOptionsForComponent.isPage
|
|
404
|
-
// 页面时删除预设的虚拟组件字段
|
|
405
446
|
&& finalOptionsForComponent.options && Reflect.deleteProperty(finalOptionsForComponent.options, "virtualHost");
|
|
406
447
|
|
|
448
|
+
// 初始化store数据到data并把store配置放入到data的__storeConfig__下
|
|
449
|
+
initStore(finalOptionsForComponent);
|
|
450
|
+
|
|
451
|
+
// 处理computed和watch配置
|
|
452
|
+
computedWatchHandle(finalOptionsForComponent);
|
|
453
|
+
|
|
407
454
|
// BBeforeCreate在最后面,让BeforeCreate生命周期运行在最终建立组件时。
|
|
408
455
|
finalOptionsForComponent.behaviors!.push(BBeforeCreate);
|
|
409
456
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type mobx from "mobx";
|
|
2
|
+
import type { FinalOptionsOfComponent } from "..";
|
|
3
|
+
export function initStore(finalOptionsForComponent: FinalOptionsOfComponent) {
|
|
4
|
+
const storeConfig = finalOptionsForComponent.store;
|
|
5
|
+
if (storeConfig) {
|
|
6
|
+
const { toJS } = require("mobx") as typeof mobx;
|
|
7
|
+
for (const key in storeConfig) {
|
|
8
|
+
finalOptionsForComponent.data[key] = toJS(storeConfig[key]());
|
|
9
|
+
|
|
10
|
+
// 把响应式数据配置保留在methods的__storeConfig__字段下带入到组件实例中(不用函数返回方式也可以,但不符合methods字段类型),后续再从原型上删除。
|
|
11
|
+
}
|
|
12
|
+
finalOptionsForComponent.data.__storeConfig__ = storeConfig;
|
|
13
|
+
|
|
14
|
+
delete finalOptionsForComponent.store;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -9,9 +9,8 @@ import type { RootComponentOption } from "./RootComponent/RootComponentOption";
|
|
|
9
9
|
import type { SubComponentsOption } from "./SubComponents/SubComponentsOption";
|
|
10
10
|
|
|
11
11
|
import type { RootComponentTrueOptions } from "../RootComponent";
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
type Path = `/${string}`;
|
|
12
|
+
import { assignOptions } from "./assignOptions";
|
|
13
|
+
export type Path = `/${string}`;
|
|
15
14
|
|
|
16
15
|
type RootOptions<
|
|
17
16
|
TRootComponentDoc extends RootComponentDoc,
|
|
@@ -53,9 +52,7 @@ export type DefineComponentOption = {
|
|
|
53
52
|
* 把根组件选项和子组件选项转化为原生Component API选项并执行
|
|
54
53
|
*/
|
|
55
54
|
export const DefineComponent: DefineComponentConstructor = function(options): any {
|
|
56
|
-
// console.log("---------------------------");
|
|
57
|
-
|
|
58
55
|
Component(
|
|
59
|
-
|
|
56
|
+
assignOptions(options as DefineComponentOption) as any,
|
|
60
57
|
);
|
|
61
58
|
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// import type mobx from "mobx";
|
|
2
|
+
// import type { FinalOptionsOfComponent } from "../collectOptionsForComponent";
|
|
3
|
+
// export function storeDataHandle(options: FinalOptionsOfComponent) {
|
|
4
|
+
// // 初始化store
|
|
5
|
+
// const storeConfig = options.store;
|
|
6
|
+
// if (!storeConfig) return;
|
|
7
|
+
// const { toJS, comparer, reaction } = require("mobx") as typeof mobx;
|
|
8
|
+
|
|
9
|
+
// options.data.__disposer__ = {};
|
|
10
|
+
|
|
11
|
+
// for (const key in storeConfig) {
|
|
12
|
+
// options.data[key] = toJS(storeConfig[key]());
|
|
13
|
+
|
|
14
|
+
// options.data.__disposer__[key] = reaction(storeConfig[key], (value: unknown) => {
|
|
15
|
+
|
|
16
|
+
// // @ts-ignore 加入到待setData对象中
|
|
17
|
+
// this.setData({
|
|
18
|
+
// [key]: toJS(value),
|
|
19
|
+
// });
|
|
20
|
+
// }, {
|
|
21
|
+
// equals: comparer.structural,
|
|
22
|
+
// });
|
|
23
|
+
|
|
24
|
+
// options.methods.disposer = function(key: keyof typeof storeConfig) {
|
|
25
|
+
|
|
26
|
+
// this.data.__disposer__[key].call(this);
|
|
27
|
+
// };
|
|
28
|
+
// }
|
|
29
|
+
// delete options.store;
|
|
30
|
+
// }
|
|
@@ -21,7 +21,8 @@ export interface IInjectInfo extends BaseInjectInfo {
|
|
|
21
21
|
|
|
22
22
|
export type IInjectData = IInjectInfo["data"] & ReturnTypeInObject<IInjectInfo["store"]>;
|
|
23
23
|
|
|
24
|
-
export type IInjectStore =
|
|
24
|
+
export type IInjectStore = StoreConstraint extends IInjectInfo["store"] ? unknown
|
|
25
|
+
: ReturnTypeInObject<IInjectInfo["store"]>;
|
|
25
26
|
|
|
26
27
|
export type IInjectMethods = IInjectInfo["methods"];
|
|
27
28
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ComputeIntersection } from "hry-types/src/Object/ComputeIntersection";
|
|
2
2
|
import type { WMCompLifetimes } from "../../../types/OfficialTypeAlias";
|
|
3
|
-
import type { FinalOptionsOfComponent } from "../../DefineComponent/
|
|
3
|
+
import type { FinalOptionsOfComponent } from "../../DefineComponent/assignOptions";
|
|
4
4
|
|
|
5
5
|
export type LifetimesConstraint = // 官方组件生命周期
|
|
6
6
|
ComputeIntersection<
|
|
@@ -45,8 +45,8 @@ export type ObserversOption<TWatchData extends object, _WatchKeys extends keyof
|
|
|
45
45
|
// 不加NoInfer会导致只监控计算字段时错误
|
|
46
46
|
NoInfer<
|
|
47
47
|
& {
|
|
48
|
-
[k in _WatchKeys]?: (
|
|
49
|
-
newValue: ReadonlyDeep<TWatchData[k]
|
|
48
|
+
[k in _WatchKeys | "**"]?: (
|
|
49
|
+
newValue: k extends keyof TWatchData ? ReadonlyDeep<TWatchData[k]> : unknown,
|
|
50
50
|
) => void;
|
|
51
51
|
}
|
|
52
52
|
// 解决单独书写计算书写字段的报错(或许是:ts字面量约束检测提前计算属性key引起的错误提示)
|
|
@@ -67,7 +67,7 @@ SubComponent<Root, OnlyPropsCompDoc>()({
|
|
|
67
67
|
SubComponent<Root, OnlyPropsCompDoc>()({
|
|
68
68
|
computed: {
|
|
69
69
|
aaa_num() {
|
|
70
|
-
return 123
|
|
70
|
+
return 123;
|
|
71
71
|
},
|
|
72
72
|
aaa_num123(): 123 {
|
|
73
73
|
return 123;
|
|
@@ -90,8 +90,8 @@ SubComponent<Root, OnlyPropsCompDoc>()({
|
|
|
90
90
|
bool: boolean;
|
|
91
91
|
aaa_num: number;
|
|
92
92
|
aaa_num123: 123;
|
|
93
|
-
aaa_str: "a";
|
|
94
|
-
aaa_obj: Mock_User;
|
|
93
|
+
aaa_str: "a" | "b";
|
|
94
|
+
aaa_obj: Mock_User | null;
|
|
95
95
|
} & IInjectData
|
|
96
96
|
>,
|
|
97
97
|
Test.Pass
|
|
@@ -114,8 +114,8 @@ SubComponent<Root, OnlyPropsCompDoc>()({
|
|
|
114
114
|
bool: boolean;
|
|
115
115
|
aaa_num: number;
|
|
116
116
|
aaa_num123: 123;
|
|
117
|
-
aaa_str: "a";
|
|
118
|
-
aaa_obj: Mock_User;
|
|
117
|
+
aaa_str: "a" | "b";
|
|
118
|
+
aaa_obj: Mock_User | null;
|
|
119
119
|
} & IInjectData
|
|
120
120
|
>,
|
|
121
121
|
Test.Pass
|
|
@@ -54,10 +54,10 @@ SubComponent<{}, CompDoc>()({
|
|
|
54
54
|
typeof this.data,
|
|
55
55
|
ReadonlyDeep<
|
|
56
56
|
{
|
|
57
|
-
aaa_str: "a";
|
|
58
|
-
aaa_num: 123;
|
|
59
|
-
aaa_obj: null;
|
|
60
57
|
_aaa_str: string;
|
|
58
|
+
aaa_str: "a" | "b";
|
|
59
|
+
aaa_num: number;
|
|
60
|
+
aaa_obj: Mock_User | null;
|
|
61
61
|
} & IInjectData
|
|
62
62
|
>,
|
|
63
63
|
Test.Pass
|