annil 1.3.0-dev.202312100 → 1.4.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 +26 -0
- package/README.md +28 -16
- 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/CustomEvents/test/GetCustomEventDoc.test.js +0 -1
- package/dist/api/RootComponent/CustomEvents/test/GetCustomEventDoc.test.js.map +1 -1
- package/dist/api/RootComponent/CustomEvents/test/GetFullEventDoc.test.js +0 -1
- package/dist/api/RootComponent/CustomEvents/test/GetFullEventDoc.test.js.map +1 -1
- package/dist/api/RootComponent/CustomEvents/test/GetShortEventDoc.test.js +0 -1
- package/dist/api/RootComponent/CustomEvents/test/GetShortEventDoc.test.js.map +1 -1
- package/dist/api/RootComponent/Lifetimes/test/error.test.js +1 -1
- package/dist/api/RootComponent/Lifetimes/test/error.test.js.map +1 -1
- package/dist/api/RootComponent/Lifetimes/test/normal.test.js +1 -1
- package/dist/api/RootComponent/Lifetimes/test/normal.test.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/api/navigateTo.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 +3 -0
- 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 +2 -2
- 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 +36 -39
- 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/isEqual.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/types.d.ts +8 -5
- 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/thirdLib/index.js +1 -2
- package/dist/thirdLib/index.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 +7 -4
- package/src/api/DefineComponent/collectOptionsForComponent.ts +57 -58
- package/src/api/DefineComponent/index.ts +1 -1
- package/src/api/RootComponent/Lifetimes/test/error.test.ts +1 -1
- package/src/api/RootComponent/Lifetimes/test/normal.test.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/api/navigateTo.ts +1 -0
- package/src/behaviors/BComputedAndWatch/computedUpdater.ts +2 -20
- package/src/behaviors/BComputedAndWatch/data-tracer.ts +6 -1
- package/src/behaviors/BComputedAndWatch/getPathsValue.ts +2 -4
- package/src/behaviors/BComputedAndWatch/getPropertiesValue.ts +57 -0
- package/src/behaviors/BComputedAndWatch/index.ts +55 -59
- package/src/behaviors/BComputedAndWatch/initComputed.ts +36 -90
- package/src/behaviors/BComputedAndWatch/isEqual.ts +1 -0
- package/src/behaviors/BComputedAndWatch/types.ts +13 -11
- 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/tsconfig.json +26 -0
- 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
|
@@ -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,45 @@ 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
|
+
// @ts-ignore 隐式索引
|
|
117
|
+
initAllData[key] = itemCache.value;
|
|
118
|
+
|
|
119
|
+
computedCache[key] = itemCache;
|
|
151
120
|
}
|
|
152
121
|
}
|
|
153
|
-
// 看uninitedkey是否未空,空表示所有依赖收集完毕直接返回
|
|
122
|
+
// 看uninitedkey(2处的新值)是否未空,空表示所有依赖收集完毕直接返回
|
|
154
123
|
if (uninitedkeys.length === 0) {
|
|
155
124
|
return computedCache;
|
|
156
125
|
}
|
|
157
126
|
|
|
158
127
|
// 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
|
-
}
|
|
128
|
+
return initComputed(options, computedConfig, initAllData, uninitedkeys, computedCache);
|
|
183
129
|
}
|
|
@@ -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
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
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"lib": ["ES2022", "DOM"], // jest中用到了DOM类型
|
|
4
|
+
"target": "ES2017",
|
|
5
|
+
"module": "ES2022",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"strictFunctionTypes": false, // e: Dataset<{ id: string }>会报错。
|
|
8
|
+
"exactOptionalPropertyTypes": true, // 带有?标志的属性类型不在包含undefined
|
|
9
|
+
"skipLibCheck": true, // 跳过声明文件的类型检查(.d.ts),除了你引用的, 默认false(不跳过)。
|
|
10
|
+
"moduleResolution": "Bundler", // 模块解析策略 //5.0 新值 bundler
|
|
11
|
+
"noImplicitAny": true, // 不允许隐式的any类型
|
|
12
|
+
"esModuleInterop": true, // 默认false 将 CommonJS/AMD/UMD 模块视为类似于 ES6 模块,jest需要。
|
|
13
|
+
"removeComments": true, // 去除注释
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"noImplicitReturns": true, // 隐式函数返回
|
|
16
|
+
"forceConsistentCasingInFileNames": true, // 区分文件名的大小写
|
|
17
|
+
// "noUnusedLocals": true, // 报告未使用局部变量的错误
|
|
18
|
+
// "noUnusedParameters": false, // 报告函数中未使用参数的错误
|
|
19
|
+
// "suppressImplicitAnyIndexErrors": true, // 为true关闭隐式索引错误,但很激进,建议使用@ts-ignore
|
|
20
|
+
"isolatedModules": true, // 模块化(.ts文件必须有导入或导出) 默认false
|
|
21
|
+
// "verbatimModuleSyntax": true,不能开启.jset会报错,在vscode中设置代替它要求ts版本>5.3
|
|
22
|
+
// 默认加载所有@types下的包 types指定具体的包,其他拒绝。包含上级目录
|
|
23
|
+
"types": ["hry-types", "mobx", "jest", "miniprogram-api-typings"]
|
|
24
|
+
},
|
|
25
|
+
"include": ["**/*.ts"]
|
|
26
|
+
}
|
|
@@ -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"}
|