annil 1.0.1 → 1.0.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 +12 -0
- package/README.md +36 -6
- package/dist/api/DefineComponent/collectOptionsForComponent.d.ts +4 -0
- package/dist/api/DefineComponent/collectOptionsForComponent.js +152 -0
- package/dist/api/DefineComponent/collectOptionsForComponent.js.map +1 -0
- package/dist/api/DefineComponent/index.d.ts +4 -28
- package/dist/api/DefineComponent/index.js +2 -37
- package/dist/api/DefineComponent/index.js.map +1 -1
- package/dist/api/RootComponent/CustomEvents/CustomEventConstraint.d.ts +2 -1
- package/dist/api/RootComponent/Data/DataConstraint.d.ts +1 -0
- package/dist/api/RootComponent/Data/DataConstraint.js +2 -0
- package/dist/api/RootComponent/Data/DataConstraint.js.map +1 -0
- package/dist/api/RootComponent/Lifetimes/LifetimesConstraint.d.ts +2 -2
- package/dist/api/RootComponent/Lifetimes/test/error.test.js +1 -0
- package/dist/api/RootComponent/Lifetimes/test/error.test.js.map +1 -1
- package/dist/api/RootComponent/Lifetimes/test/normal.test.js +3 -0
- package/dist/api/RootComponent/Lifetimes/test/normal.test.js.map +1 -1
- package/dist/api/RootComponent/Properties/test/normalOptional.test.js +15 -0
- package/dist/api/RootComponent/Properties/test/normalOptional.test.js.map +1 -1
- package/dist/api/RootComponent/index.d.ts +19 -2
- package/dist/api/RootComponent/index.js +1 -1
- package/dist/api/RootComponent/index.js.map +1 -1
- package/dist/api/SubComponent/index.d.ts +1 -1
- package/dist/api/SubComponent/index.js.map +1 -1
- package/dist/{utils → behaviors/BComputedAndWatch}/computedUpdater.d.ts +1 -1
- package/dist/{utils → behaviors/BComputedAndWatch}/computedUpdater.js +2 -2
- package/dist/behaviors/BComputedAndWatch/computedUpdater.js.map +1 -0
- package/dist/{utils → behaviors/BComputedAndWatch}/getPathsValue.d.ts +1 -1
- package/dist/behaviors/BComputedAndWatch/getPathsValue.js.map +1 -0
- package/dist/behaviors/BComputedAndWatch/index.js +8 -1
- package/dist/behaviors/BComputedAndWatch/index.js.map +1 -1
- package/dist/behaviors/BComputedAndWatch/initComputed.js +1 -1
- package/dist/behaviors/BComputedAndWatch/initComputed.js.map +1 -1
- package/dist/behaviors/BStore.js.map +1 -1
- package/dist/behaviors/BbeforeCreated.js +1 -2
- package/dist/behaviors/BbeforeCreated.js.map +1 -1
- package/dist/types/OfficialTypeAlias.d.ts +2 -2
- package/package.json +3 -3
- package/src/api/DefineComponent/collectOptionsForComponent.ts +290 -0
- package/src/api/DefineComponent/index.ts +12 -85
- package/src/api/RootComponent/CustomEvents/CustomEventConstraint.ts +3 -1
- package/src/api/RootComponent/Data/DataConstraint.ts +1 -0
- package/src/api/RootComponent/IsPage/IsPageOption.ts +6 -1
- package/src/api/RootComponent/Lifetimes/LifetimesConstraint.ts +2 -2
- package/src/api/RootComponent/Lifetimes/test/error.test.ts +1 -1
- package/src/api/RootComponent/Lifetimes/test/normal.test.ts +3 -3
- package/src/api/RootComponent/Properties/test/normalOptional.test.ts +20 -0
- package/src/api/RootComponent/index.ts +19 -3
- package/src/api/SubComponent/index.ts +2 -3
- package/src/{utils → behaviors/BComputedAndWatch}/computedUpdater.ts +5 -4
- package/src/{utils → behaviors/BComputedAndWatch}/getPathsValue.ts +1 -1
- package/src/behaviors/BComputedAndWatch/index.ts +13 -4
- package/src/behaviors/BComputedAndWatch/initComputed.ts +1 -1
- package/src/behaviors/BStore.ts +2 -2
- package/src/behaviors/BbeforeCreated.ts +3 -3
- package/src/types/OfficialTypeAlias.ts +2 -2
- package/src/types/SpecificType.ts +0 -4
- package/tsconfig.json +6 -15
- package/dist/utils/InternalFieldProtection.d.ts +0 -2
- package/dist/utils/InternalFieldProtection.js +0 -11
- package/dist/utils/InternalFieldProtection.js.map +0 -1
- package/dist/utils/collector.d.ts +0 -5
- package/dist/utils/collector.js +0 -21
- package/dist/utils/collector.js.map +0 -1
- package/dist/utils/computedUpdater.js.map +0 -1
- package/dist/utils/createdHijack.d.ts +0 -0
- package/dist/utils/createdHijack.js +0 -2
- package/dist/utils/createdHijack.js.map +0 -1
- package/dist/utils/customEventsHandle.d.ts +0 -2
- package/dist/utils/customEventsHandle.js +0 -20
- package/dist/utils/customEventsHandle.js.map +0 -1
- package/dist/utils/eventsHandle.d.ts +0 -2
- package/dist/utils/eventsHandle.js +0 -5
- package/dist/utils/eventsHandle.js.map +0 -1
- package/dist/utils/funcConfigHandle.d.ts +0 -2
- package/dist/utils/funcConfigHandle.js +0 -18
- package/dist/utils/funcConfigHandle.js.map +0 -1
- package/dist/utils/getPathsValue.js.map +0 -1
- package/dist/utils/initWatchOldValue.d.ts +0 -2
- package/dist/utils/initWatchOldValue.js +0 -10
- package/dist/utils/initWatchOldValue.js.map +0 -1
- package/dist/utils/onLoadHijack.d.ts +0 -3
- package/dist/utils/onLoadHijack.js +0 -16
- package/dist/utils/onLoadHijack.js.map +0 -1
- package/dist/utils/onLoadReceivedDataHandle.d.ts +0 -3
- package/dist/utils/onLoadReceivedDataHandle.js +0 -14
- package/dist/utils/onLoadReceivedDataHandle.js.map +0 -1
- package/dist/utils/rootComponentFieldHandle.d.ts +0 -3
- package/dist/utils/rootComponentFieldHandle.js +0 -13
- package/dist/utils/rootComponentFieldHandle.js.map +0 -1
- package/dist/utils/subComponentsHandle.d.ts +0 -3
- package/dist/utils/subComponentsHandle.js +0 -12
- package/dist/utils/subComponentsHandle.js.map +0 -1
- package/src/utils/InternalFieldProtection.ts +0 -18
- package/src/utils/collector.ts +0 -41
- package/src/utils/createdHijack.ts +0 -21
- package/src/utils/customEventsHandle.ts +0 -31
- package/src/utils/eventsHandle.ts +0 -10
- package/src/utils/funcConfigHandle.ts +0 -30
- package/src/utils/initWatchOldValue.ts +0 -12
- package/src/utils/onLoadHijack.ts +0 -30
- package/src/utils/onLoadReceivedDataHandle.ts +0 -31
- package/src/utils/rootComponentFieldHandle.ts +0 -27
- package/src/utils/subComponentsHandle.ts +0 -25
- /package/dist/{utils → behaviors/BComputedAndWatch}/getPathsValue.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BStore.js","sourceRoot":"","sources":["../../src/behaviors/BStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAG7D,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC;IAC7B,gBAAgB,CAAC,
|
|
1
|
+
{"version":3,"file":"BStore.js","sourceRoot":"","sources":["../../src/behaviors/BStore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAG7D,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC;IAC7B,gBAAgB,CAAC,OAAiC;QAEhD,MAAM,WAAW,GAAG,OAAO,CAAC,KAAK,CAAC;QAClC,IAAI,CAAC,WAAW;YAAE,OAAO;QACzB,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAgB,CAAC;QAChD,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;YAC9B,OAAO,CAAC,IAAI,KAAZ,OAAO,CAAC,IAAI,GAAK,EAAE,EAAC;YAEpB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAI7C,OAAO,CAAC,OAAO,KAAf,OAAO,CAAC,OAAO,GAAK,EAAE,EAAC;YAEvB,OAAO,CAAC,OAAO,CAAC,eAAe,GAAG,GAAG,EAAE,CAAC,WAAW,CAAC;YAEpD,OAAO,OAAO,CAAC,KAAK,CAAC;QACvB,CAAC;IACH,CAAC;IAED,SAAS,EAAE;QACT,OAAO;;YAEL,MAAM,WAAW,GAAG,MAAA,IAAI,CAAC,eAAe,oDAAI,CAAC;YAC7C,IAAI,CAAC,WAAW;gBAAE,OAAO;YACzB,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,CAAgB,CAAC;YAEpE,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;YAEnB,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;gBAE9B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,QAAQ,CAC3B,WAAW,CAAC,GAAG,CAAC,EAChB,CAAC,KAAc,EAAE,EAAE;oBAEjB,IAAI,CAAC,OAAO,CAAC;wBACX,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC;qBACnB,CAAC,CAAC;gBACL,CAAC,EACD;oBACE,MAAM,EAAE,QAAQ,CAAC,UAAU;iBAC5B,CACF,CAAC;YACJ,CAAC;YACD,gBAAgB,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC;QAC5C,CAAC;QACD,QAAQ;YAGN,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBAChC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACvB,CAAC;QACH,CAAC;KACF;CACF,CAAC,CAAC"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export const BBeforeCreate = Behavior({
|
|
2
2
|
definitionFilter(options) {
|
|
3
|
-
|
|
4
|
-
const beforeCreateFunc = (_a = options.lifetimes) === null || _a === void 0 ? void 0 : _a.beforeCreate;
|
|
3
|
+
const beforeCreateFunc = options.lifetimes.beforeCreate;
|
|
5
4
|
beforeCreateFunc && beforeCreateFunc(options);
|
|
6
5
|
},
|
|
7
6
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BbeforeCreated.js","sourceRoot":"","sources":["../../src/behaviors/BbeforeCreated.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;IACpC,gBAAgB,CAAC,
|
|
1
|
+
{"version":3,"file":"BbeforeCreated.js","sourceRoot":"","sources":["../../src/behaviors/BbeforeCreated.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC;IACpC,gBAAgB,CAAC,OAAiC;QAEhD,MAAM,gBAAgB,GAAG,OAAO,CAAC,SAAU,CAAC,YAAY,CAAC;QAEzD,gBAAgB,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAChD,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
/// <reference types="./node_modules/miniprogram-api-typings/" />
|
|
4
4
|
/// <reference types="./node_modules/miniprogram-api-typings/" />
|
|
5
5
|
export type WMTriggerEventOption = WechatMiniprogram.Component.TriggerEventOption;
|
|
6
|
-
export type WMCustomEvent<Detail =
|
|
6
|
+
export type WMCustomEvent<Detail = undefined, Mark extends object = object, CurrentTargetDataset extends object = object, TargetDataset extends object = CurrentTargetDataset> = WechatMiniprogram.CustomEvent<Detail, Mark, CurrentTargetDataset, TargetDataset>;
|
|
7
7
|
export type WMBaseEvent<Mark extends object = object, CurrentTargetDataset extends object = object, TargetDataset extends object = CurrentTargetDataset> = WechatMiniprogram.BaseEvent<Mark, CurrentTargetDataset, TargetDataset>;
|
|
8
|
-
export type Dataset<TCurrentTargetDataset extends object, TargetDataset extends object = TCurrentTargetDataset, Detail =
|
|
8
|
+
export type Dataset<TCurrentTargetDataset extends object, TargetDataset extends object = TCurrentTargetDataset, Detail = undefined> = WMCustomEvent<Detail, object, TCurrentTargetDataset, TargetDataset>;
|
|
9
9
|
export type Mark<TMark extends object> = WechatMiniprogram.CustomEvent<{}, TMark, {}, {}>;
|
|
10
10
|
export type Detail<T> = WMCustomEvent<T>;
|
|
11
11
|
export type CurrentTargetDataset<T extends object> = WMCustomEvent<{}, {}, T>;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "annil",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "微信小程序(原生开发)插件",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest --watch",
|
|
8
|
-
"coverage": "jest --
|
|
8
|
+
"coverage": "jest --coverage",
|
|
9
9
|
"prepare": "husky install",
|
|
10
10
|
"lint": "eslint . --fix && npm run fmt",
|
|
11
11
|
"fmt": "dprint fmt",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"program",
|
|
45
45
|
"wmp"
|
|
46
46
|
],
|
|
47
|
-
"author": "",
|
|
47
|
+
"author": "missannil",
|
|
48
48
|
"license": "MIT",
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"hry-types": "^0.16.12",
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import type { Func } from "hry-types/src/Misc/_api";
|
|
2
|
+
import { BBeforeCreate } from "../../behaviors/BbeforeCreated";
|
|
3
|
+
import { BComputedAndWatch } from "../../behaviors/BComputedAndWatch";
|
|
4
|
+
import { initComputed } from "../../behaviors/BComputedAndWatch/initComputed";
|
|
5
|
+
import type { Instance } from "../../behaviors/BComputedAndWatch/types";
|
|
6
|
+
import { BStore } from "../../behaviors/BStore";
|
|
7
|
+
import { deepClone } from "../../utils/deepClone";
|
|
8
|
+
import { INNERMARKER } from "../../utils/InnerMarker";
|
|
9
|
+
import { isEmptyObject } from "../../utils/isEmptyObject";
|
|
10
|
+
import type { RootComponentTrueOptions } from "../RootComponent";
|
|
11
|
+
import type {
|
|
12
|
+
CustomEventConstraint,
|
|
13
|
+
CustomEvents,
|
|
14
|
+
FullCustomEvents,
|
|
15
|
+
} from "../RootComponent/CustomEvents/CustomEventConstraint";
|
|
16
|
+
import type { EventsConstraint } from "../RootComponent/Events/EventsConstraint";
|
|
17
|
+
import type { PageInstance } from "../RootComponent/Instance/RootComponentInstance";
|
|
18
|
+
import type { SubComponentTrueOptions } from "../SubComponent";
|
|
19
|
+
import type { FinalOptionsForComponent, FuncOptions } from ".";
|
|
20
|
+
/**
|
|
21
|
+
* 针对通过 navigateTo传过来的数据解析
|
|
22
|
+
* @param option - option中的url是拼接了encodeURIComponent转码的data对象的,key为INNERMARKER.url
|
|
23
|
+
*/
|
|
24
|
+
/* istanbul ignore next */
|
|
25
|
+
function onLoadReceivedDataHandle(
|
|
26
|
+
this: PageInstance,
|
|
27
|
+
option: Record<typeof INNERMARKER.url, string>,
|
|
28
|
+
) {
|
|
29
|
+
const innerData: string | undefined = option[INNERMARKER.url];
|
|
30
|
+
// 未使用自定义的navigateTo
|
|
31
|
+
if (innerData === undefined) return;
|
|
32
|
+
const optionData = JSON.parse(decodeURIComponent(innerData));
|
|
33
|
+
|
|
34
|
+
// 使用navigateTo
|
|
35
|
+
for (const key in optionData) {
|
|
36
|
+
option[key] = optionData[key];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// setData会触发计算属性 继承属性等 。
|
|
40
|
+
this.setData(option as any);
|
|
41
|
+
|
|
42
|
+
delete option[INNERMARKER.url];
|
|
43
|
+
|
|
44
|
+
Reflect.deleteProperty(this.data, INNERMARKER.url);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* onLoad生命周期劫持函数
|
|
49
|
+
*/
|
|
50
|
+
/* istanbul ignore next */
|
|
51
|
+
function onLoadHijack(
|
|
52
|
+
options: FinalOptionsForComponent,
|
|
53
|
+
before: Func[],
|
|
54
|
+
after: Func[] = [],
|
|
55
|
+
) {
|
|
56
|
+
options.methods ||= {};
|
|
57
|
+
|
|
58
|
+
const cloneOpt = deepClone(options);
|
|
59
|
+
const originalOnLoad: Func | undefined = options.methods.onLoad;
|
|
60
|
+
|
|
61
|
+
options.methods.onLoad = function(props: unknown) {
|
|
62
|
+
before.forEach((func) => {
|
|
63
|
+
func.call(this, props, cloneOpt);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
originalOnLoad?.call(this, props);
|
|
67
|
+
|
|
68
|
+
after.forEach((func) => {
|
|
69
|
+
func.call(this, props, cloneOpt);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 内部保护字段 即不允许配置的字段名(所有方法下)
|
|
75
|
+
*/
|
|
76
|
+
const INNERFIELDS = ["disposer"];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 报错的形式避免输入字段和内部字段冲突
|
|
80
|
+
*/
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
function InternalFieldProtection(methodsConfig: object) {
|
|
83
|
+
const methodsConfigKeys = Object.keys(methodsConfig);
|
|
84
|
+
for (const key of INNERFIELDS) {
|
|
85
|
+
if (methodsConfigKeys.includes(key)) {
|
|
86
|
+
throw Error(`${key}已被内部字段占用`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 把函数配置放入一个配置中依次运行
|
|
92
|
+
*/
|
|
93
|
+
function _funcConfigHandle(methodsConfig: object, configList: Record<string, Func[]>) {
|
|
94
|
+
for (const key in configList) {
|
|
95
|
+
methodsConfig[key] = function(...args: unknown[]) {
|
|
96
|
+
configList[key].forEach(ele => ele.call(this, ...args));
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function funcConfigHandle(
|
|
102
|
+
finalOptionsForComponent: FinalOptionsForComponent,
|
|
103
|
+
isPage: boolean | undefined,
|
|
104
|
+
funcOptions: FuncOptions,
|
|
105
|
+
) {
|
|
106
|
+
// 测试框架无法测试page情形
|
|
107
|
+
/* istanbul ignore next */
|
|
108
|
+
if (isPage) {
|
|
109
|
+
// 页面时 生命周期方法(即 on 开头的方法),(https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html)
|
|
110
|
+
funcOptions.pageLifetimes && _funcConfigHandle(finalOptionsForComponent.methods ||= {}, funcOptions.pageLifetimes);
|
|
111
|
+
} else {
|
|
112
|
+
// 组件时
|
|
113
|
+
funcOptions.pageLifetimes
|
|
114
|
+
&& _funcConfigHandle(finalOptionsForComponent.pageLifetimes ||= {}, funcOptions.pageLifetimes);
|
|
115
|
+
}
|
|
116
|
+
funcOptions.lifetimes && _funcConfigHandle(finalOptionsForComponent.lifetimes ||= {}, funcOptions.lifetimes);
|
|
117
|
+
|
|
118
|
+
funcOptions.watch && _funcConfigHandle(finalOptionsForComponent.watch ||= {}, funcOptions.watch);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 把配置为函数的字段方法收集到funcConfig中
|
|
122
|
+
*/
|
|
123
|
+
function funcFieldsCollect(
|
|
124
|
+
options: SubComponentTrueOptions | RootComponentTrueOptions,
|
|
125
|
+
funcOptions: FuncOptions,
|
|
126
|
+
) {
|
|
127
|
+
for (const key in funcOptions) {
|
|
128
|
+
// key = "pageLifetimes" | "lifetimes" | "watch"
|
|
129
|
+
if (options[key]) {
|
|
130
|
+
for (const _key in options[key]) {
|
|
131
|
+
(funcOptions[key][_key] ||= []).push(options[key][_key]);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
Reflect.deleteProperty(options, key);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 其他字段加入到componentOptions对应字段配置中
|
|
140
|
+
function otherFieldsHandle(
|
|
141
|
+
finalOptions: FinalOptionsForComponent,
|
|
142
|
+
rootComponentOptions: RootComponentTrueOptions,
|
|
143
|
+
) {
|
|
144
|
+
for (const key in rootComponentOptions) {
|
|
145
|
+
const config = rootComponentOptions[key];
|
|
146
|
+
if (Array.isArray(config)) {
|
|
147
|
+
// 好像只有behaviors是数组吧.
|
|
148
|
+
/* istanbul ignore next */
|
|
149
|
+
(finalOptions[key] ||= []).push(...config);
|
|
150
|
+
} else {
|
|
151
|
+
Object.assign(finalOptions[key] ||= {}, config);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 把events字段配置变成函数放入到componentOptions.methods中
|
|
157
|
+
*/
|
|
158
|
+
function eventsHandle(componentOptions: FinalOptionsForComponent, eventsConfig: EventsConstraint) {
|
|
159
|
+
/* istanbul ignore next */
|
|
160
|
+
componentOptions.methods ||= {};
|
|
161
|
+
|
|
162
|
+
Object.assign(componentOptions.methods, eventsConfig);
|
|
163
|
+
}
|
|
164
|
+
function subComponentsHandle(
|
|
165
|
+
componentOptions: FinalOptionsForComponent,
|
|
166
|
+
subComponents: SubComponentTrueOptions[],
|
|
167
|
+
funcOptions: FuncOptions,
|
|
168
|
+
) {
|
|
169
|
+
subComponents.forEach((subOption) => {
|
|
170
|
+
subOption.events && eventsHandle(componentOptions, subOption.events);
|
|
171
|
+
|
|
172
|
+
funcFieldsCollect(subOption, funcOptions);
|
|
173
|
+
|
|
174
|
+
otherFieldsHandle(componentOptions, subOption);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 类型守卫
|
|
179
|
+
function IsFullCustomEvents(
|
|
180
|
+
customEventOptions: CustomEvents,
|
|
181
|
+
): customEventOptions is FullCustomEvents {
|
|
182
|
+
return Object.prototype.toString.call(customEventOptions) === "[object Object]";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 把customEvents字段配置变成函数放入到componentOptions.methods中
|
|
187
|
+
*/
|
|
188
|
+
function customEventsHandle(
|
|
189
|
+
componentOptions: FinalOptionsForComponent,
|
|
190
|
+
customEventsConfig: CustomEventConstraint,
|
|
191
|
+
) {
|
|
192
|
+
/* istanbul ignore next */
|
|
193
|
+
componentOptions.methods ||= {};
|
|
194
|
+
|
|
195
|
+
for (const key in customEventsConfig) {
|
|
196
|
+
const customEventOptions = customEventsConfig[key];
|
|
197
|
+
|
|
198
|
+
if (IsFullCustomEvents(customEventOptions)) {
|
|
199
|
+
componentOptions.methods[key] = function(this: Instance, detail: unknown) {
|
|
200
|
+
this.triggerEvent(key, detail, customEventOptions.options);
|
|
201
|
+
};
|
|
202
|
+
} else {
|
|
203
|
+
componentOptions.methods[key] = function(this: Instance, detail: unknown) {
|
|
204
|
+
this.triggerEvent(key, detail);
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 收集 rootComponentOptions 配置到 finalOptions 和 funcOptions 中
|
|
212
|
+
* @param finalOptions - 收集配置对象
|
|
213
|
+
* @param funcOptions - 收集特殊配置对象字段
|
|
214
|
+
* @param rootComponentOptions - 被收集的源配置对象
|
|
215
|
+
*/
|
|
216
|
+
function collectRootComponentOption(
|
|
217
|
+
finalOptions: FinalOptionsForComponent,
|
|
218
|
+
funcOptions: FuncOptions,
|
|
219
|
+
rootComponentOptions: RootComponentTrueOptions,
|
|
220
|
+
) {
|
|
221
|
+
rootComponentOptions.customEvents && customEventsHandle(finalOptions, rootComponentOptions.customEvents);
|
|
222
|
+
|
|
223
|
+
delete rootComponentOptions.customEvents;
|
|
224
|
+
|
|
225
|
+
rootComponentOptions.events && eventsHandle(finalOptions, rootComponentOptions.events);
|
|
226
|
+
|
|
227
|
+
delete rootComponentOptions.events;
|
|
228
|
+
|
|
229
|
+
funcFieldsCollect(rootComponentOptions, funcOptions);
|
|
230
|
+
|
|
231
|
+
otherFieldsHandle(finalOptions, rootComponentOptions);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 返回一个由rootComponentOption和subComponentsList配置整合的对象
|
|
235
|
+
* @param rootComponentOption -
|
|
236
|
+
* @param subComponentsList -
|
|
237
|
+
* @returns FinalOptionsForComponent
|
|
238
|
+
*/
|
|
239
|
+
export function collectOptionsForComponent(
|
|
240
|
+
rootComponentOption: RootComponentTrueOptions | undefined,
|
|
241
|
+
subComponentsList: SubComponentTrueOptions[] | undefined,
|
|
242
|
+
): FinalOptionsForComponent {
|
|
243
|
+
const finalOptionsForComponent: FinalOptionsForComponent = {
|
|
244
|
+
// default options
|
|
245
|
+
options: {
|
|
246
|
+
// addGlobalClass: true,
|
|
247
|
+
multipleSlots: true,
|
|
248
|
+
pureDataPattern: /^_/,
|
|
249
|
+
virtualHost: true,
|
|
250
|
+
},
|
|
251
|
+
// default behaviors
|
|
252
|
+
behaviors: [BStore, BComputedAndWatch],
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* 有些字段配置同时存在根组件和子组件当中(如pageLifetimes,lifetimes,watch字段), 且key相同值类型为函数。funcConfig对象用于收集这些配置为数组形式,最终再一起整合进finalOptionsForComponent配置中。即funcConfig是一个临时中介对象。
|
|
256
|
+
*/
|
|
257
|
+
const funcOptions: FuncOptions = {
|
|
258
|
+
pageLifetimes: {},
|
|
259
|
+
lifetimes: {},
|
|
260
|
+
watch: {},
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
if (rootComponentOption && !isEmptyObject(rootComponentOption)) {
|
|
264
|
+
collectRootComponentOption(finalOptionsForComponent, funcOptions, rootComponentOption);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (subComponentsList && !isEmptyObject(subComponentsList)) {
|
|
268
|
+
subComponentsHandle(finalOptionsForComponent, subComponentsList, funcOptions);
|
|
269
|
+
}
|
|
270
|
+
// 框架无法测试页面
|
|
271
|
+
/* istanbul ignore next */
|
|
272
|
+
if (!isEmptyObject(funcOptions)) {
|
|
273
|
+
funcConfigHandle(finalOptionsForComponent, rootComponentOption?.isPage, funcOptions);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
finalOptionsForComponent.methods && InternalFieldProtection(finalOptionsForComponent.methods);
|
|
277
|
+
|
|
278
|
+
// BBeforeCreate在最后面
|
|
279
|
+
finalOptionsForComponent.behaviors!.push(BBeforeCreate);
|
|
280
|
+
|
|
281
|
+
onLoadHijack(finalOptionsForComponent, [onLoadReceivedDataHandle, initComputed], []);
|
|
282
|
+
|
|
283
|
+
// 框架无法测试页面
|
|
284
|
+
/* istanbul ignore next */
|
|
285
|
+
if (finalOptionsForComponent.isPage) {
|
|
286
|
+
Reflect.deleteProperty(finalOptionsForComponent.options!, "virtualHost");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return finalOptionsForComponent;
|
|
290
|
+
}
|
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import type { IfExtends } from "hry-types/src/Any/IfExtends";
|
|
2
2
|
import type { Func } from "hry-types/src/Misc/Func";
|
|
3
|
-
import { BBeforeCreate } from "../../behaviors/BbeforeCreated";
|
|
4
|
-
import { BComputedAndWatch } from "../../behaviors/BComputedAndWatch";
|
|
5
|
-
import { initComputed } from "../../behaviors/BComputedAndWatch/initComputed";
|
|
6
|
-
import { BStore } from "../../behaviors/BStore";
|
|
7
3
|
import type { WMComponent, WMCompPageLifetimes, WMPageLifetimes } from "../../types/OfficialTypeAlias";
|
|
8
|
-
|
|
9
|
-
import { isEmptyObject } from "../../utils/isEmptyObject";
|
|
10
|
-
import { onLoadReceivedDataHandle } from "../../utils/onLoadReceivedDataHandle";
|
|
11
|
-
import { rootComponentFieldHandle } from "../../utils/rootComponentFieldHandle";
|
|
12
|
-
import { subComponentsHandle } from "../../utils/subComponentsHandle";
|
|
4
|
+
|
|
13
5
|
import type { LifetimesConstraint } from "../RootComponent/Lifetimes/LifetimesConstraint";
|
|
14
6
|
import type { RootComponentDoc } from "../RootComponent/RootComponentDoc";
|
|
15
7
|
import type { SubComponentDoc } from "../SubComponent/SubComponentDoc";
|
|
@@ -19,8 +11,10 @@ import type { CreatePageDoc } from "./ReturnType/CreatePageDoc";
|
|
|
19
11
|
import type { RootComponentOption } from "./RootComponent/RootComponentOption";
|
|
20
12
|
import type { SubComponentsOption } from "./SubComponents/SubComponentsOption";
|
|
21
13
|
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
14
|
+
import type { RootComponentTrueOptions } from "../RootComponent";
|
|
15
|
+
import { collectOptionsForComponent } from "./collectOptionsForComponent";
|
|
16
|
+
|
|
17
|
+
type Path = `/${string}`;
|
|
24
18
|
|
|
25
19
|
type RootOptions<
|
|
26
20
|
TRootComponentDoc extends RootComponentDoc,
|
|
@@ -55,19 +49,15 @@ interface DefineComponentConstructor {
|
|
|
55
49
|
* 临时的函数配置项
|
|
56
50
|
* 把根组件与子组件中配置类型为函数的相同字段配置收集在一起(数组)
|
|
57
51
|
*/
|
|
58
|
-
export type
|
|
59
|
-
pageLifetimes?: Record<string, Func[]>;
|
|
60
|
-
lifetimes?: Record<string, Func[]>;
|
|
61
|
-
watch?: Record<string, Func[]>;
|
|
62
|
-
};
|
|
52
|
+
export type FuncOptions = Record<"pageLifetimes" | "lifetimes" | "watch", Record<string, Func[]>>;
|
|
63
53
|
|
|
64
54
|
/**
|
|
65
|
-
*
|
|
55
|
+
* 最终传入原生Component的配置项
|
|
66
56
|
*/
|
|
67
|
-
export type
|
|
57
|
+
export type FinalOptionsForComponent = {
|
|
68
58
|
isPage?: boolean;
|
|
69
59
|
options?: WMComponent.Options;
|
|
70
|
-
properties?:
|
|
60
|
+
properties?: Record<string, any>;
|
|
71
61
|
data?: object;
|
|
72
62
|
store?: object;
|
|
73
63
|
computed?: Record<string, Func>;
|
|
@@ -79,71 +69,8 @@ export type ComponentOptions = {
|
|
|
79
69
|
pageLifetimes?: Partial<WMCompPageLifetimes & { load: Func } & WMPageLifetimes>;
|
|
80
70
|
};
|
|
81
71
|
|
|
82
|
-
export type Path = `/${string}`;
|
|
83
|
-
|
|
84
|
-
export type DefineComponentOptions = {
|
|
85
|
-
path?: Path;
|
|
86
|
-
name?: string;
|
|
87
|
-
rootComponent?: RootComponentDoc;
|
|
88
|
-
subComponents?: SubComponentDoc[];
|
|
89
|
-
};
|
|
90
|
-
|
|
91
72
|
export const DefineComponent: DefineComponentConstructor = function(options): any {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const finalOptions: ComponentOptions = {
|
|
96
|
-
// default options
|
|
97
|
-
options: {
|
|
98
|
-
// addGlobalClass: true,
|
|
99
|
-
multipleSlots: true,
|
|
100
|
-
pureDataPattern: /^_/,
|
|
101
|
-
virtualHost: true,
|
|
102
|
-
},
|
|
103
|
-
// default behaviors
|
|
104
|
-
behaviors: [BStore, BComputedAndWatch],
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* 有些选项配置是函数,且可能分布在根组件和子组件中,funcConfig 用于收集这些配置,最终再一起整合进finalOptions配置。rootComponentHandle和subComponentsHandle都会收集函数配置到funcConfig中,再由funcConfigHandle整合配置到finalOptions中。
|
|
108
|
-
*/
|
|
109
|
-
const allFuncConfig: FuncConfig = {};
|
|
110
|
-
|
|
111
|
-
if (options.rootComponent && !isEmptyObject(options.rootComponent)) {
|
|
112
|
-
rootComponentFieldHandle(options.rootComponent, finalOptions, allFuncConfig);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (options.subComponents && !isEmptyObject(options.subComponents)) {
|
|
116
|
-
subComponentsHandle(finalOptions, options.subComponents, allFuncConfig);
|
|
117
|
-
}
|
|
118
|
-
// 框架无法测试页面
|
|
119
|
-
/* istanbul ignore next */
|
|
120
|
-
if (!isEmptyObject(allFuncConfig)) {
|
|
121
|
-
funcConfigHandle(finalOptions, options.rootComponent?.isPage, allFuncConfig);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
finalOptions.methods && InternalFieldProtection(finalOptions.methods);
|
|
125
|
-
|
|
126
|
-
finalOptions.behaviors!.push(BBeforeCreate);
|
|
127
|
-
|
|
128
|
-
onLoadHijack(finalOptions, [onLoadReceivedDataHandle, initComputed], []);
|
|
129
|
-
|
|
130
|
-
// 框架无法测试页面
|
|
131
|
-
/* istanbul ignore next */
|
|
132
|
-
if (finalOptions.isPage) {
|
|
133
|
-
Reflect.deleteProperty(finalOptions.options!, "virtualHost");
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
Component(finalOptions as any);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export type PageOptions = {
|
|
140
|
-
path: Path;
|
|
141
|
-
rootComponent: RootComponentDoc & { isPage: true };
|
|
142
|
-
subComponents: SubComponentDoc[];
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
export type CompOptions = {
|
|
146
|
-
name: string;
|
|
147
|
-
rootComponent: RootComponentDoc & { isPage: undefined };
|
|
148
|
-
subComponents: SubComponentDoc[];
|
|
73
|
+
Component(
|
|
74
|
+
collectOptionsForComponent(options.rootComponent as RootComponentTrueOptions | undefined, options.subComponents),
|
|
75
|
+
);
|
|
149
76
|
};
|
|
@@ -47,4 +47,6 @@ export type FullCustomEvents = {
|
|
|
47
47
|
options: OptionsFieldsConfigOfCustomEvents;
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
export type
|
|
50
|
+
export type CustomEvents = FullCustomEvents | ShortCustomeEvents;
|
|
51
|
+
|
|
52
|
+
export type CustomEventConstraint = Record<string, CustomEvents>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type DataConstraint = Record<string, any>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { WMCompLifetimes } from "../../../types/OfficialTypeAlias";
|
|
2
|
-
import type {
|
|
2
|
+
import type { FinalOptionsForComponent } from "../../DefineComponent";
|
|
3
3
|
|
|
4
4
|
export type LifetimesConstraint = // 官方组件生命周期
|
|
5
5
|
& WMCompLifetimes["lifetimes"]
|
|
@@ -7,5 +7,5 @@ export type LifetimesConstraint = // 官方组件生命周期
|
|
|
7
7
|
/**
|
|
8
8
|
* 建立组件时的真正配置对象
|
|
9
9
|
*/
|
|
10
|
-
beforeCreate?: (options:
|
|
10
|
+
beforeCreate?: (options: FinalOptionsForComponent) => void;
|
|
11
11
|
};
|
|
@@ -73,3 +73,23 @@ type OptionalDocExpected = {
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
Checking<typeof OptionalDoc, OptionalDocExpected, Test.Pass>;
|
|
76
|
+
|
|
77
|
+
// properties 使用SpecificType时,接受interface类型
|
|
78
|
+
interface Foo {
|
|
79
|
+
name: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
RootComponent()({
|
|
83
|
+
properties: {
|
|
84
|
+
xxx: Object as SpecificType<{ dd: Foo }>,
|
|
85
|
+
user: {
|
|
86
|
+
type: Object as SpecificType<Foo>,
|
|
87
|
+
value: { name: "zhao" },
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
computed: {
|
|
91
|
+
ddd() {
|
|
92
|
+
this.data.user?.name;
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
});
|
|
@@ -3,7 +3,7 @@ import type { NoInfer } from "hry-types/src/Generic/NoInfer";
|
|
|
3
3
|
import type { EmptyObject } from "hry-types/src/Misc/EmptyObject";
|
|
4
4
|
import type { Func } from "hry-types/src/Misc/Func";
|
|
5
5
|
import type { ComputeIntersectionDeep } from "hry-types/src/Object/_api";
|
|
6
|
-
import type { WMCompOtherOption } from "../../types/OfficialTypeAlias";
|
|
6
|
+
import type { WMCompOtherOption, WMCompPageLifetimes, WMPageLifetimes } from "../../types/OfficialTypeAlias";
|
|
7
7
|
import type { ComponentDoc } from "../DefineComponent/ReturnType/ComponentDoc";
|
|
8
8
|
import type { ComputedConstraint } from "./Computed/ComputedConstraint";
|
|
9
9
|
import type { ComputedOption } from "./Computed/ComputedOption";
|
|
@@ -11,11 +11,13 @@ import type { GetComputedDoc } from "./Computed/GetComputedDoc";
|
|
|
11
11
|
import type { CustomEventConstraint } from "./CustomEvents/CustomEventConstraint";
|
|
12
12
|
import type { CustomEventsOption } from "./CustomEvents/CustomEventsOption";
|
|
13
13
|
import type { GetCustomEventDoc } from "./CustomEvents/GetCustomEventDoc";
|
|
14
|
+
import type { DataConstraint } from "./Data/DataConstraint";
|
|
14
15
|
import type { DataOption } from "./Data/DataOption";
|
|
15
16
|
import type { EventsConstraint } from "./Events/EventsConstraint";
|
|
16
17
|
import type { EventsOption } from "./Events/EventsOption";
|
|
17
18
|
import type { RootComponentInstance } from "./Instance/RootComponentInstance";
|
|
18
19
|
import type { IsPageOption } from "./IsPage/IsPageOption";
|
|
20
|
+
import type { LifetimesConstraint } from "./Lifetimes/LifetimesConstraint";
|
|
19
21
|
import type { LifetimesOption } from "./Lifetimes/LifetimesOption";
|
|
20
22
|
import type { MethodsConstraint } from "./Methods/MethodsConstraint";
|
|
21
23
|
import type { MethodsOption } from "./Methods/MethodsOption";
|
|
@@ -81,7 +83,7 @@ type RootComponentConstructor<TReceivedComponentDoc extends ComponentDoc[] | Com
|
|
|
81
83
|
| boolean
|
|
82
84
|
| null
|
|
83
85
|
| Literal[]
|
|
84
|
-
|
|
|
86
|
+
| object,
|
|
85
87
|
// TEvents 不能有默认值 {} 会引起事件参数类型(e)失效
|
|
86
88
|
TEvents extends EventsConstraint<TReceivedComponentDoc>,
|
|
87
89
|
TIsPage extends boolean = false,
|
|
@@ -135,5 +137,19 @@ export function RootComponent<
|
|
|
135
137
|
>(): RootComponentConstructor<
|
|
136
138
|
TReceivedComponentDoc
|
|
137
139
|
> {
|
|
138
|
-
return (options: any) => options;
|
|
140
|
+
return ((options: any) => options as RootComponentTrueOptions) as any;
|
|
139
141
|
}
|
|
142
|
+
|
|
143
|
+
export type RootComponentTrueOptions = {
|
|
144
|
+
isPage?: boolean;
|
|
145
|
+
properties?: PropertiesConstraint;
|
|
146
|
+
data?: DataConstraint;
|
|
147
|
+
computed?: ComputedConstraint;
|
|
148
|
+
customEvents?: CustomEventConstraint;
|
|
149
|
+
methods?: MethodsConstraint;
|
|
150
|
+
events?: EventsConstraint;
|
|
151
|
+
store?: StoreConstraint;
|
|
152
|
+
watch?: Record<string, Func>;
|
|
153
|
+
lifetimes?: LifetimesConstraint;
|
|
154
|
+
pageLifetimes?: Partial<WMCompPageLifetimes & { load: Func } & WMPageLifetimes>;
|
|
155
|
+
};
|
|
@@ -196,11 +196,10 @@ export function SubComponent<
|
|
|
196
196
|
CompDoc extends ComponentDoc,
|
|
197
197
|
Prefix extends string = "",
|
|
198
198
|
>(): IfExtends<EmptyObject, CompDoc, (opt: EmptyObject) => never, SubComponentConstructor<RootDoc, CompDoc, Prefix>> {
|
|
199
|
-
return ((options: any) => options) as any;
|
|
199
|
+
return ((options: any) => options as SubComponentTrueOptions) as any;
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
|
|
203
|
-
export type SubComponentOptions = {
|
|
202
|
+
export type SubComponentTrueOptions = {
|
|
204
203
|
inhrit?: string;
|
|
205
204
|
data?: Record<string, unknown>;
|
|
206
205
|
computed?: Record<string, Func>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { deepProxy, unwrap } from "
|
|
2
|
-
import type { ComputedDependence } from "../behaviors/BComputedAndWatch/initComputed";
|
|
3
|
-
import { isEqual } from "../behaviors/BComputedAndWatch/isEqual";
|
|
4
|
-
import type { Instance } from "../behaviors/BComputedAndWatch/types";
|
|
1
|
+
import { deepProxy, unwrap } from "./data-tracer";
|
|
5
2
|
import { getPathsValue } from "./getPathsValue";
|
|
6
3
|
|
|
4
|
+
import type { ComputedDependence } from "./initComputed";
|
|
5
|
+
import { isEqual } from "./isEqual";
|
|
6
|
+
import type { Instance } from "./types";
|
|
7
|
+
|
|
7
8
|
export function computedUpdater(this: Instance, isUpdated = false): boolean {
|
|
8
9
|
// console.log("computedUpdater开始");
|
|
9
10
|
|