annil 1.2.2-dev.202312100 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/dist/api/DefineComponent/collectOptionsForComponent.d.ts +11 -6
- package/dist/api/DefineComponent/collectOptionsForComponent.js +30 -35
- package/dist/api/DefineComponent/collectOptionsForComponent.js.map +1 -1
- package/dist/api/DefineComponent/index.js.map +1 -1
- package/dist/api/RootComponent/PageLifetimes/PageLifetimesOption.d.ts +1 -1
- package/dist/api/RootComponent/index.js +1 -1
- package/dist/api/RootComponent/index.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/computedUpdater.js +2 -11
- package/dist/behaviors/BComputedAndWatch/computedUpdater.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/data-tracer.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/getPathsValue.d.ts +1 -2
- package/dist/behaviors/BComputedAndWatch/getPathsValue.js +14 -9
- package/dist/behaviors/BComputedAndWatch/getPathsValue.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/getPropertiesValue.d.ts +2 -0
- package/dist/behaviors/BComputedAndWatch/getPropertiesValue.js +41 -0
- package/dist/behaviors/BComputedAndWatch/getPropertiesValue.js.map +1 -0
- package/dist/behaviors/BComputedAndWatch/index.js +39 -42
- package/dist/behaviors/BComputedAndWatch/index.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/initComputed.d.ts +4 -8
- package/dist/behaviors/BComputedAndWatch/initComputed.js +16 -40
- package/dist/behaviors/BComputedAndWatch/initComputed.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/types.d.ts +9 -6
- package/dist/behaviors/BStore.js +1 -4
- package/dist/behaviors/BStore.js.map +1 -1
- package/dist/behaviors/BbeforeCreated.js.map +1 -1
- package/dist/utils/InnerMarker.js.map +1 -1
- package/dist/utils/isEmptyObject.d.ts +1 -1
- package/dist/utils/isEmptyObject.js.map +1 -1
- package/package.json +1 -1
- package/src/api/DefineComponent/collectOptionsForComponent.ts +49 -58
- package/src/api/DefineComponent/index.ts +1 -1
- package/src/api/RootComponent/PageLifetimes/PageLifetimesOption.ts +1 -1
- package/src/api/RootComponent/Watch/test/WatchProperties.test.ts +1 -1
- package/src/api/RootComponent/index.ts +1 -1
- package/src/behaviors/BComputedAndWatch/computedUpdater.ts +3 -20
- package/src/behaviors/BComputedAndWatch/data-tracer.ts +2 -0
- package/src/behaviors/BComputedAndWatch/getPathsValue.ts +24 -10
- package/src/behaviors/BComputedAndWatch/getPropertiesValue.ts +54 -0
- package/src/behaviors/BComputedAndWatch/index.ts +58 -63
- package/src/behaviors/BComputedAndWatch/initComputed.ts +35 -90
- package/src/behaviors/BComputedAndWatch/types.ts +14 -12
- package/src/behaviors/BStore.ts +3 -7
- package/src/behaviors/BbeforeCreated.ts +5 -2
- package/src/utils/InnerMarker.ts +1 -2
- package/src/utils/isEmptyObject.ts +1 -1
- package/dist/behaviors/BComputedAndWatch/IsPage.d.ts +0 -2
- package/dist/behaviors/BComputedAndWatch/IsPage.js +0 -4
- package/dist/behaviors/BComputedAndWatch/IsPage.js.map +0 -1
- package/src/behaviors/BComputedAndWatch/IsPage.ts +0 -5
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import type { Func } from "hry-types/src/Misc/
|
|
2
|
-
|
|
1
|
+
import type { Func } from "hry-types/src/Misc/_api";
|
|
3
2
|
import type { FinalOptionsOfComponent } from "../../api/DefineComponent/collectOptionsForComponent";
|
|
4
3
|
import { deepClone } from "../../utils/deepClone";
|
|
5
4
|
import { deleteProtoField } from "../../utils/deleteProtoField";
|
|
6
5
|
import { isEmptyObject } from "../../utils/isEmptyObject";
|
|
6
|
+
import { computedUpdater } from "./computedUpdater";
|
|
7
7
|
import { getPathsValue } from "./getPathsValue";
|
|
8
|
+
import { getPropertiesValue } from "./getPropertiesValue";
|
|
8
9
|
import { initComputed } from "./initComputed";
|
|
9
10
|
import { isEqual } from "./isEqual";
|
|
10
|
-
import { isPage } from "./IsPage";
|
|
11
11
|
import type { Instance, WatchOldValue } from "./types";
|
|
12
12
|
function initWatchOldValue(this: Instance, watchConfig: object): WatchOldValue {
|
|
13
13
|
const watchOldValue = {};
|
|
14
14
|
for (const key in watchConfig) {
|
|
15
|
-
watchOldValue[key] = deepClone(getPathsValue
|
|
15
|
+
watchOldValue[key] = deepClone(getPathsValue(this.data, key));
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
return watchOldValue;
|
|
19
19
|
}
|
|
20
|
+
|
|
20
21
|
/**
|
|
21
22
|
* 实现
|
|
22
23
|
* 1. 计算属性初始化在attached之后(前版本在beforeCreate)
|
|
@@ -32,60 +33,50 @@ function initWatchOldValue(this: Instance, watchConfig: object): WatchOldValue {
|
|
|
32
33
|
*/
|
|
33
34
|
|
|
34
35
|
export const BComputedAndWatch = Behavior({
|
|
36
|
+
// @ts-ignore
|
|
35
37
|
definitionFilter(options: FinalOptionsOfComponent) {
|
|
38
|
+
// 计算属性初始化
|
|
39
|
+
const rawPropertiesValue = getPropertiesValue(options.properties);
|
|
40
|
+
const methodOpt = options.methods;
|
|
36
41
|
const computedConfig = options.computed;
|
|
37
42
|
|
|
38
|
-
// computed handle
|
|
39
43
|
if (computedConfig && !isEmptyObject(computedConfig)) {
|
|
40
|
-
|
|
41
|
-
options.methods ||= {};
|
|
42
|
-
|
|
43
|
-
const methodsConfig = options.methods;
|
|
44
|
-
|
|
45
|
-
// 把计算属性配置保留在methods的__computedConfig__字段下带入到组件实例中。
|
|
46
|
-
methodsConfig.__computedConfig__ = () => computedConfig;
|
|
47
|
-
|
|
48
|
-
const observersConfig = options.observers ||= {};
|
|
49
|
-
// 通过observers加入`**`字段来触发计算属性更新
|
|
50
|
-
const originalFunc = observersConfig["**"] as Func | undefined;
|
|
51
|
-
|
|
52
|
-
observersConfig["**"] = function(this: Instance): undefined {
|
|
53
|
-
const computedStatus = this.__computedStatus__;
|
|
54
|
-
switch (computedStatus) {
|
|
55
|
-
case undefined:
|
|
56
|
-
// 1 触发来自attach时或没有计算属性时
|
|
57
|
-
originalFunc && /* istanbul ignore next */ originalFunc.call(this);
|
|
58
|
-
|
|
59
|
-
break;
|
|
60
|
-
case "初始化中":
|
|
61
|
-
// 2 触发来自计算属性初始化时的setData,什么都不需要做。 初始化后__computedStatus__变为待更新
|
|
62
|
-
|
|
63
|
-
break;
|
|
64
|
-
case "待更新":
|
|
65
|
-
// 3. 触发来自attached后的setData或properties更新
|
|
66
|
-
{
|
|
67
|
-
const isUpdated = this.__computedUpdater__();
|
|
68
|
-
if (isUpdated) {
|
|
69
|
-
// 更新了会再次触发自身转到 4
|
|
70
|
-
this.__computedStatus__ = "更新完毕";
|
|
71
|
-
} else {
|
|
72
|
-
// 无需更新计算属性
|
|
73
|
-
originalFunc && /* istanbul ignore next */ originalFunc.call(this);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
break;
|
|
77
|
-
case "更新完毕":
|
|
78
|
-
{
|
|
79
|
-
// 4 来自计算属性更新后的自身回调
|
|
80
|
-
// console.log("来自计算属性更新后的自身回调");
|
|
81
|
-
this.__computedStatus__ = "待更新";
|
|
44
|
+
const computedCache = initComputed(options, computedConfig, { ...options.data, ...rawPropertiesValue });
|
|
82
45
|
|
|
46
|
+
// 把计算属性缓存从方法中带入到实例中,在created周期加入实例后删除
|
|
47
|
+
methodOpt.__computedInitCache__ = () => computedCache;
|
|
48
|
+
}
|
|
49
|
+
const observersConfig = options.observers;
|
|
50
|
+
// 通过observers加入`**`字段来触发计算属性更新
|
|
51
|
+
const originalFunc = observersConfig["**"] as Func | undefined;
|
|
52
|
+
|
|
53
|
+
observersConfig["**"] = function(this: Instance): undefined {
|
|
54
|
+
const computedStatus = this.__computedStatus__;
|
|
55
|
+
switch (computedStatus) {
|
|
56
|
+
case "待更新":
|
|
57
|
+
// 3. 触发来自attached后的setData或properties更新
|
|
58
|
+
{
|
|
59
|
+
const isUpdated = this.__computedUpdater__();
|
|
60
|
+
if (isUpdated) {
|
|
61
|
+
// 更新了会再次触发自身转到 4
|
|
62
|
+
this.__computedStatus__ = "更新完毕";
|
|
63
|
+
} else {
|
|
64
|
+
// 无需更新计算属性
|
|
83
65
|
originalFunc && /* istanbul ignore next */ originalFunc.call(this);
|
|
84
66
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
case "更新完毕":
|
|
70
|
+
{
|
|
71
|
+
// 4 来自计算属性更新后的自身回调
|
|
72
|
+
// console.log("来自计算属性更新后的自身回调");
|
|
73
|
+
this.__computedStatus__ = "待更新";
|
|
74
|
+
|
|
75
|
+
originalFunc && /* istanbul ignore next */ originalFunc.call(this);
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
89
80
|
|
|
90
81
|
// watch handle
|
|
91
82
|
const watchConfig = options.watch;
|
|
@@ -96,40 +87,44 @@ export const BComputedAndWatch = Behavior({
|
|
|
96
87
|
// 把watch配置保留在methods的__watchConfig__字段下带入到组件实例中。
|
|
97
88
|
methodsConfig.__watchConfig__ = () => watchConfig;
|
|
98
89
|
|
|
90
|
+
/* istanbul ignore next */
|
|
99
91
|
const observersConfig = options.observers ||= {};
|
|
100
92
|
for (const key in watchConfig) {
|
|
101
93
|
const watchHadle = watchConfig[key];
|
|
102
94
|
|
|
103
|
-
|
|
95
|
+
// 在监控多个数据时,参数是多个值
|
|
96
|
+
observersConfig[key] = function(this: Instance, ...newValue: unknown[]) {
|
|
104
97
|
const watchOldValue = this.__watchOldValue__!;
|
|
105
98
|
const oldValue = watchOldValue[key];
|
|
106
|
-
|
|
107
|
-
if ((newValue === undefined) || isEqual(newValue, oldValue)) return;
|
|
99
|
+
if (isEqual(newValue, oldValue)) return;
|
|
108
100
|
watchOldValue[key] = deepClone(newValue);
|
|
109
101
|
|
|
110
|
-
watchHadle.call(this, newValue, oldValue);
|
|
102
|
+
watchHadle.call(this, ...newValue, ...oldValue);
|
|
111
103
|
};
|
|
112
104
|
}
|
|
113
105
|
}
|
|
114
106
|
},
|
|
115
107
|
lifetimes: {
|
|
116
108
|
created(this: Instance) {
|
|
109
|
+
// 把计算属性的初始缓存放入实例
|
|
110
|
+
const __computedInitCache__ = this.__computedInitCache__?.();
|
|
111
|
+
|
|
112
|
+
this.__computedCache__ = __computedInitCache__;
|
|
113
|
+
|
|
114
|
+
deleteProtoField(this, "__computedInitCache__");
|
|
115
|
+
|
|
116
|
+
this.__computedStatus__ = "待更新";
|
|
117
|
+
|
|
118
|
+
this.__computedUpdater__ = computedUpdater.bind(this);
|
|
119
|
+
|
|
117
120
|
// 初始化watch 生成OldValue
|
|
118
121
|
const watchConfig = this.__watchConfig__?.();
|
|
119
122
|
|
|
120
123
|
deleteProtoField(this, "__watchConfig__");
|
|
121
124
|
|
|
122
125
|
if (watchConfig) {
|
|
123
|
-
// 此时计算属性还未初始化,properties还未第一次传入
|
|
124
126
|
this.__watchOldValue__ = initWatchOldValue.call(this, watchConfig);
|
|
125
127
|
}
|
|
126
128
|
},
|
|
127
|
-
attached(this: Instance) {
|
|
128
|
-
if (!isPage(this)) {
|
|
129
|
-
// 组件此时properties已传入
|
|
130
|
-
initComputed.call(this);
|
|
131
|
-
}
|
|
132
|
-
// else 页面在onLoad时传入properties 计算属性初始化逻辑通过onLoadHijack函数完成
|
|
133
|
-
},
|
|
134
129
|
},
|
|
135
130
|
});
|
|
@@ -2,17 +2,11 @@
|
|
|
2
2
|
// import type { IsDependOnOthers } from "./data-tracer";
|
|
3
3
|
|
|
4
4
|
import type { Func } from "hry-types/src/Misc/Func";
|
|
5
|
-
import {
|
|
6
|
-
import { computedUpdater } from "./computedUpdater";
|
|
5
|
+
import type { FinalOptionsOfComponent } from "../../api/DefineComponent/collectOptionsForComponent";
|
|
7
6
|
import { deepProxy, unwrap } from "./data-tracer";
|
|
8
|
-
import type { ComputedCache,
|
|
7
|
+
import type { ComputedCache, ItemCache } from "./types";
|
|
9
8
|
export type ComputedDependence = { paths: string[]; val: unknown };
|
|
10
9
|
|
|
11
|
-
type ComputedInfo = {
|
|
12
|
-
dependences: ComputedDependence[];
|
|
13
|
-
value: unknown;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
10
|
/**
|
|
17
11
|
* 如果依赖列表某项的首个字段值为undefined并且字段为其他计算属性字段 返回false(即被依赖的计算字段写在了依赖他的计算字段后面), 否则返回ComputedInfo。
|
|
18
12
|
*/
|
|
@@ -20,7 +14,7 @@ function isValidDependences(dependences: ComputedDependence[], computedKeys: str
|
|
|
20
14
|
for (const { paths: path, val } of dependences) {
|
|
21
15
|
// 引用值为undefined时
|
|
22
16
|
// console.log(path[0]);
|
|
23
|
-
if (val === undefined && computedKeys.includes(path[0])) {
|
|
17
|
+
if ((val === undefined) && computedKeys.includes(path[0])) {
|
|
24
18
|
return false;
|
|
25
19
|
}
|
|
26
20
|
}
|
|
@@ -28,49 +22,22 @@ function isValidDependences(dependences: ComputedDependence[], computedKeys: str
|
|
|
28
22
|
return true; // 依赖有效
|
|
29
23
|
}
|
|
30
24
|
|
|
31
|
-
export function
|
|
32
|
-
|
|
25
|
+
export function getItemCache(
|
|
26
|
+
initAllData: object,
|
|
33
27
|
computedKeys: string[],
|
|
34
28
|
computedFunc: Func,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// 建立当前计算字段的依赖
|
|
29
|
+
): false | ItemCache {
|
|
30
|
+
// 当前计算字段的依赖列表
|
|
38
31
|
const dependences: ComputedDependence[] = [];
|
|
39
|
-
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
41
|
-
const _this = this;
|
|
42
|
-
const computedThis = new Proxy({ data: deepProxy(this.data, dependences) }, {
|
|
43
|
-
get(target, key) {
|
|
44
|
-
if (key === "data") {
|
|
45
|
-
return Reflect.get(target, key);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return Reflect.get(_this, key);
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
initValue = computedFunc.call(computedThis);
|
|
53
|
-
|
|
54
|
-
// } catch (error) {
|
|
55
|
-
// // 为js开发考虑使用了 this.data.xxx.age 当xxx为undefined时
|
|
56
|
-
// if (!isValidDependences(dependences, computedKeys)) {
|
|
57
|
-
// console.log(222);
|
|
32
|
+
const initValue = computedFunc.call({ data: deepProxy(initAllData, dependences) });
|
|
58
33
|
|
|
59
|
-
// // 情形2 依赖了未初始化的计算属性的子属性(会报错) 对应测试文件[NoOptionalProperties.ts](../../../jest/computed/NoOptionalProperties/NoOptionalProperties.test.ts)的age字段
|
|
60
|
-
// return false;
|
|
61
|
-
// }
|
|
62
|
-
// // 其他错误正常报错
|
|
63
|
-
// throw error;
|
|
64
|
-
// }
|
|
65
34
|
if (isValidDependences(dependences, computedKeys)) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return { dependences, value: initValue };
|
|
35
|
+
return {
|
|
36
|
+
// 去重 避免 如: ` age(){ return this.data.xxx.num + this.data.xxx.str + this.data.xxx} ` 造成的多个依赖相同情况。应就要一个 xxx
|
|
37
|
+
dependences: uniqueDependences(dependences),
|
|
38
|
+
method: computedFunc,
|
|
39
|
+
value: unwrap(initValue),
|
|
40
|
+
};
|
|
74
41
|
} else {
|
|
75
42
|
/**
|
|
76
43
|
* 情形1 依赖了其他未初始化的计算属性(不报错) 对应测试文件[NoOptionalProperties.ts](../../../jest/computed/NoOptionalProperties/NoOptionalProperties.test.ts)的copyPropUser字段
|
|
@@ -118,66 +85,44 @@ function uniqueDependences(dependences: ComputedDependence[]): ComputedDependenc
|
|
|
118
85
|
|
|
119
86
|
return dependences;
|
|
120
87
|
}
|
|
88
|
+
|
|
121
89
|
/**
|
|
122
|
-
*
|
|
123
|
-
* @param computedConfig - 计算属性配置
|
|
124
|
-
* @param uninitedkeys - 未初始的计算属性keys
|
|
125
|
-
* @param computedCache - 计算属性缓存默认=`{}`
|
|
126
|
-
* @returns 计算属性缓存
|
|
90
|
+
* 把计算属性初始值加入到options.data中返回缓存
|
|
127
91
|
*/
|
|
128
|
-
function
|
|
129
|
-
|
|
92
|
+
export function initComputed(
|
|
93
|
+
options: FinalOptionsOfComponent,
|
|
130
94
|
computedConfig: Record<string, Func>,
|
|
131
|
-
|
|
95
|
+
initAllData: object,
|
|
96
|
+
uninitedkeys: string[] = Object.keys(computedConfig),
|
|
132
97
|
computedCache: ComputedCache = {},
|
|
133
98
|
): ComputedCache {
|
|
134
|
-
// 收集依赖
|
|
99
|
+
// 收集依赖 uninitedkeys不受内部2处重新赋值的影响
|
|
135
100
|
for (const key of uninitedkeys) {
|
|
136
|
-
|
|
101
|
+
// uninitedkeys 受2处重新赋值的影响
|
|
102
|
+
const itemCache = getItemCache(initAllData, uninitedkeys, computedConfig[key]);
|
|
137
103
|
|
|
138
|
-
//
|
|
104
|
+
// 去除当前的key 2
|
|
139
105
|
uninitedkeys = uninitedkeys.filter(ele => ele !== key);
|
|
140
106
|
|
|
141
|
-
if (
|
|
107
|
+
if (itemCache === false) {
|
|
142
108
|
// 把当前依赖不正确的key放到后面去
|
|
143
109
|
uninitedkeys.push(key);
|
|
144
110
|
} else {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
111
|
+
/* istanbul ignore next */
|
|
112
|
+
const dataOpt = options.data ||= {};
|
|
113
|
+
|
|
114
|
+
dataOpt[key] = itemCache.value;
|
|
115
|
+
|
|
116
|
+
initAllData[key] = itemCache.value;
|
|
117
|
+
|
|
118
|
+
computedCache[key] = itemCache;
|
|
151
119
|
}
|
|
152
120
|
}
|
|
153
|
-
// 看uninitedkey是否未空,空表示所有依赖收集完毕直接返回
|
|
121
|
+
// 看uninitedkey(2处的新值)是否未空,空表示所有依赖收集完毕直接返回
|
|
154
122
|
if (uninitedkeys.length === 0) {
|
|
155
123
|
return computedCache;
|
|
156
124
|
}
|
|
157
125
|
|
|
158
126
|
// uninitedkey不为空,递归
|
|
159
|
-
return
|
|
160
|
-
}
|
|
161
|
-
export function initComputed(this: Instance) {
|
|
162
|
-
const computedConfig = this.__computedConfig__?.();
|
|
163
|
-
|
|
164
|
-
deleteProtoField(this, "__computedConfig__");
|
|
165
|
-
|
|
166
|
-
if (computedConfig) {
|
|
167
|
-
// 实例加入 __computedUpdateStatus__ 提高性能
|
|
168
|
-
this.__computedStatus__ = "初始化中";
|
|
169
|
-
|
|
170
|
-
// 初始化计算属性(返回缓存)
|
|
171
|
-
this.__computedCache__ = _initComputed.call(
|
|
172
|
-
this,
|
|
173
|
-
computedConfig,
|
|
174
|
-
Object.keys(computedConfig),
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
// 给原型上加入__computedUpdater__方法 在observers下的'**'配置中触发
|
|
178
|
-
|
|
179
|
-
this.__computedUpdater__ = computedUpdater.bind(this);
|
|
180
|
-
|
|
181
|
-
this.__computedStatus__ = "待更新";
|
|
182
|
-
}
|
|
127
|
+
return initComputed(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
183
128
|
}
|
|
@@ -5,25 +5,27 @@ import type { ComputedDependence } from "./initComputed";
|
|
|
5
5
|
export type InstanceCustomFields = {
|
|
6
6
|
__pendingSetData__?: object | null;
|
|
7
7
|
__storeConfig__?: () => Record<string, () => unknown>;
|
|
8
|
-
__computedStatus__?: "
|
|
8
|
+
__computedStatus__?: "待更新" | "更新完毕";
|
|
9
9
|
__computedUpdater__: Func;
|
|
10
|
-
|
|
11
|
-
__computedCache__
|
|
10
|
+
__computedInitCache__?: () => ComputedCache;
|
|
11
|
+
__computedCache__: ComputedCache | undefined;
|
|
12
12
|
__watchOldValue__?: WatchOldValue;
|
|
13
13
|
__watchConfig__?: () => Record<string, Func>;
|
|
14
14
|
__compLoadList__?: Func[];
|
|
15
|
+
|
|
16
|
+
__prePropertiesValue__: object | undefined;
|
|
17
|
+
__setDataFromInner__?: boolean;
|
|
15
18
|
disposer: Record<string, Func>;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
21
|
export type Instance = (ComponentInstance | PageInstance) & InstanceCustomFields;
|
|
19
22
|
|
|
20
|
-
export type
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
>;
|
|
23
|
+
export type ItemCache = {
|
|
24
|
+
dependences: ComputedDependence[];
|
|
25
|
+
method: Func;
|
|
26
|
+
value: unknown;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type ComputedCache = Record<string, ItemCache>;
|
|
28
30
|
|
|
29
|
-
export type WatchOldValue = Record<string, unknown>;
|
|
31
|
+
export type WatchOldValue = Record<string, unknown[]>;
|
package/src/behaviors/BStore.ts
CHANGED
|
@@ -5,22 +5,17 @@ import { deleteProtoField } from "../utils/deleteProtoField";
|
|
|
5
5
|
import type { Instance } from "./BComputedAndWatch/types";
|
|
6
6
|
|
|
7
7
|
export const BStore = Behavior({
|
|
8
|
+
// @ts-ignore
|
|
8
9
|
definitionFilter(options: FinalOptionsOfComponent) {
|
|
9
10
|
// 初始化store
|
|
10
11
|
const storeConfig = options.store;
|
|
11
12
|
if (!storeConfig) return;
|
|
12
13
|
const { toJS } = require("mobx") as typeof mobx;
|
|
13
14
|
for (const key in storeConfig) {
|
|
14
|
-
options.data ||= {};
|
|
15
|
-
|
|
16
15
|
options.data[key] = toJS(storeConfig[key]());
|
|
17
16
|
|
|
18
17
|
// 把响应式数据配置保留在methods的__storeConfig__字段下带入到组件实例中(不用函数返回方式也可以,但不符合methods字段类型),后续再从原型上删除。
|
|
19
|
-
options.methods ||= {};
|
|
20
|
-
|
|
21
18
|
options.methods.__storeConfig__ = () => storeConfig;
|
|
22
|
-
|
|
23
|
-
delete options.store;
|
|
24
19
|
}
|
|
25
20
|
},
|
|
26
21
|
lifetimes: {
|
|
@@ -28,6 +23,8 @@ export const BStore = Behavior({
|
|
|
28
23
|
// 取出通过addStoreConfigToMethods函数带入的storeConfig
|
|
29
24
|
const storeConfig = this.__storeConfig__?.();
|
|
30
25
|
if (!storeConfig) return;
|
|
26
|
+
deleteProtoField(this, "__storeConfig__");
|
|
27
|
+
|
|
31
28
|
const { comparer, reaction, toJS } = require("mobx") as typeof mobx;
|
|
32
29
|
|
|
33
30
|
this.disposer = {};
|
|
@@ -47,7 +44,6 @@ export const BStore = Behavior({
|
|
|
47
44
|
},
|
|
48
45
|
);
|
|
49
46
|
}
|
|
50
|
-
deleteProtoField(this, "__storeConfig__");
|
|
51
47
|
},
|
|
52
48
|
/* istanbul ignore next */
|
|
53
49
|
detached(this: Instance) {
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { FinalOptionsOfComponent } from "../api/DefineComponent/collectOptionsForComponent";
|
|
2
2
|
|
|
3
3
|
export const BBeforeCreate = Behavior({
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
definitionFilter(
|
|
6
|
+
options: Omit<FinalOptionsOfComponent, "customEvents" | "store" | "events" | "computed">,
|
|
7
|
+
) {
|
|
8
|
+
// 触发beforeCreate生命周期函数 options.lifetimes在之前被赋值过默认{}所以!
|
|
6
9
|
const beforeCreateFunc = options.lifetimes!.beforeCreate;
|
|
7
10
|
|
|
8
11
|
beforeCreateFunc && beforeCreateFunc(options);
|
package/src/utils/InnerMarker.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 判断一个值是否为空对象 `{}`
|
|
3
3
|
*/
|
|
4
|
-
export function isEmptyObject(obj:
|
|
4
|
+
export function isEmptyObject(obj: object) {
|
|
5
5
|
if (
|
|
6
6
|
typeof obj !== "object" || obj === null || Array.isArray(obj) || obj instanceof Set || obj instanceof Map
|
|
7
7
|
|| obj instanceof WeakSet || obj instanceof WeakMap
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"IsPage.js","sourceRoot":"","sources":["../../../src/behaviors/BComputedAndWatch/IsPage.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,MAAM,CAAC,GAAqC;IAC1D,OAAO,OAAQ,GAAoB,CAAC,KAAK,KAAK,QAAQ,CAAC;AACzD,CAAC"}
|