annil 1.0.0 → 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.
Files changed (137) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +38 -2
  3. package/dist/api/DefineComponent/ReturnType/test/normalComponentDoc.test.js.map +1 -1
  4. package/dist/api/DefineComponent/ReturnType/test/normalPageDoc.test.js.map +1 -1
  5. package/dist/api/DefineComponent/collectOptionsForComponent.d.ts +4 -0
  6. package/dist/api/DefineComponent/collectOptionsForComponent.js +152 -0
  7. package/dist/api/DefineComponent/collectOptionsForComponent.js.map +1 -0
  8. package/dist/api/DefineComponent/index.d.ts +4 -28
  9. package/dist/api/DefineComponent/index.js +2 -32
  10. package/dist/api/DefineComponent/index.js.map +1 -1
  11. package/dist/api/RootComponent/CustomEvents/CustomEventConstraint.d.ts +2 -1
  12. package/dist/api/RootComponent/Data/DataConstraint.d.ts +1 -0
  13. package/dist/api/RootComponent/Data/DataConstraint.js +2 -0
  14. package/dist/api/RootComponent/Data/DataConstraint.js.map +1 -0
  15. package/dist/api/RootComponent/Lifetimes/LifetimesConstraint.d.ts +4 -1
  16. package/dist/api/RootComponent/Lifetimes/test/error.test.js +1 -0
  17. package/dist/api/RootComponent/Lifetimes/test/error.test.js.map +1 -1
  18. package/dist/api/RootComponent/Lifetimes/test/normal.test.js +3 -0
  19. package/dist/api/RootComponent/Lifetimes/test/normal.test.js.map +1 -1
  20. package/dist/api/RootComponent/Properties/{expected/normalEmpty.js → test/normalEmpty.test.js} +1 -1
  21. package/dist/api/RootComponent/Properties/test/normalEmpty.test.js.map +1 -0
  22. package/dist/api/RootComponent/Properties/test/normalOptional.test.js +15 -0
  23. package/dist/api/RootComponent/Properties/test/normalOptional.test.js.map +1 -1
  24. package/dist/api/RootComponent/Watch/test/WatchProperties.test.js +1 -1
  25. package/dist/api/RootComponent/Watch/test/WatchProperties.test.js.map +1 -1
  26. package/dist/api/RootComponent/index.d.ts +19 -2
  27. package/dist/api/RootComponent/index.js +1 -1
  28. package/dist/api/RootComponent/index.js.map +1 -1
  29. package/dist/api/SubComponent/SubComputed/test/normal.test.js.map +1 -1
  30. package/dist/api/SubComponent/SubData/test/error.test.js.map +1 -1
  31. package/dist/api/SubComponent/SubData/test/normal.test.js.map +1 -1
  32. package/dist/api/SubComponent/SubInstance/test/normal.test.js.map +1 -1
  33. package/dist/api/SubComponent/SubPageLifetimes/test/normal.test.js.map +1 -1
  34. package/dist/api/SubComponent/SubWatch/test/WatchRootData.test.js.map +1 -1
  35. package/dist/api/SubComponent/index.d.ts +2 -2
  36. package/dist/api/SubComponent/index.js.map +1 -1
  37. package/dist/{utils → behaviors/BComputedAndWatch}/computedUpdater.d.ts +1 -1
  38. package/dist/{utils → behaviors/BComputedAndWatch}/computedUpdater.js +2 -2
  39. package/dist/behaviors/BComputedAndWatch/computedUpdater.js.map +1 -0
  40. package/dist/behaviors/BComputedAndWatch/data-tracer.js +0 -3
  41. package/dist/behaviors/BComputedAndWatch/data-tracer.js.map +1 -1
  42. package/dist/{utils → behaviors/BComputedAndWatch}/getPathsValue.d.ts +1 -1
  43. package/dist/behaviors/BComputedAndWatch/getPathsValue.js.map +1 -0
  44. package/dist/behaviors/BComputedAndWatch/index.js +10 -2
  45. package/dist/behaviors/BComputedAndWatch/index.js.map +1 -1
  46. package/dist/behaviors/BComputedAndWatch/initComputed.js +4 -12
  47. package/dist/behaviors/BComputedAndWatch/initComputed.js.map +1 -1
  48. package/dist/behaviors/BComputedAndWatch/isEqual.js.map +1 -1
  49. package/dist/behaviors/BStore.js.map +1 -1
  50. package/dist/behaviors/BbeforeCreated.js +1 -2
  51. package/dist/behaviors/BbeforeCreated.js.map +1 -1
  52. package/dist/types/GetComponentPrefix.js +1 -4
  53. package/dist/types/GetComponentPrefix.js.map +1 -1
  54. package/dist/types/OfficialTypeAlias.d.ts +2 -2
  55. package/dist/types/ReplacePrefix.js +1 -4
  56. package/dist/types/ReplacePrefix.js.map +1 -1
  57. package/package.json +5 -4
  58. package/src/api/DefineComponent/ReturnType/test/normalComponentDoc.test.ts +2 -1
  59. package/src/api/DefineComponent/ReturnType/test/normalPageDoc.test.ts +2 -1
  60. package/src/api/DefineComponent/collectOptionsForComponent.ts +290 -0
  61. package/src/api/DefineComponent/index.ts +13 -81
  62. package/src/api/RootComponent/Computed/test/normal.test.ts +1 -1
  63. package/src/api/RootComponent/CustomEvents/CustomEventConstraint.ts +3 -1
  64. package/src/api/RootComponent/Data/DataConstraint.ts +1 -0
  65. package/src/api/RootComponent/IsPage/IsPageOption.ts +6 -1
  66. package/src/api/RootComponent/Lifetimes/LifetimesConstraint.ts +8 -7
  67. package/src/api/RootComponent/Lifetimes/test/error.test.ts +1 -1
  68. package/src/api/RootComponent/Lifetimes/test/normal.test.ts +3 -3
  69. package/src/api/RootComponent/PageLifetimes/test/normal.test.ts +1 -1
  70. package/src/api/RootComponent/Properties/test/normalOptional.test.ts +20 -0
  71. package/src/api/RootComponent/Watch/test/WatchComputed.test.ts +1 -1
  72. package/src/api/RootComponent/Watch/test/WatchData.test.ts +1 -1
  73. package/src/api/RootComponent/Watch/test/WatchProperties.test.ts +8 -2
  74. package/src/api/RootComponent/index.ts +19 -3
  75. package/src/api/SubComponent/SubComputed/test/normal.test.ts +2 -1
  76. package/src/api/SubComponent/SubData/test/error.test.ts +2 -1
  77. package/src/api/SubComponent/SubData/test/normal.test.ts +2 -1
  78. package/src/api/SubComponent/SubInstance/test/normal.test.ts +2 -1
  79. package/src/api/SubComponent/SubPageLifetimes/test/normal.test.ts +2 -1
  80. package/src/api/SubComponent/SubWatch/test/WatchRootData.test.ts +2 -1
  81. package/src/api/SubComponent/SubWatch/test/error.test.ts +1 -1
  82. package/src/api/SubComponent/index.ts +11 -4
  83. package/src/{utils → behaviors/BComputedAndWatch}/computedUpdater.ts +5 -4
  84. package/src/behaviors/BComputedAndWatch/data-tracer.ts +0 -3
  85. package/src/{utils → behaviors/BComputedAndWatch}/getPathsValue.ts +1 -1
  86. package/src/behaviors/BComputedAndWatch/index.ts +21 -8
  87. package/src/behaviors/BComputedAndWatch/initComputed.ts +26 -19
  88. package/src/behaviors/BComputedAndWatch/isEqual.ts +1 -0
  89. package/src/behaviors/BStore.ts +5 -3
  90. package/src/behaviors/BbeforeCreated.ts +4 -2
  91. package/src/types/GetComponentPrefix.ts +11 -11
  92. package/src/types/OfficialTypeAlias.ts +2 -2
  93. package/src/types/ReplacePrefix.ts +9 -10
  94. package/src/types/SpecificType.ts +0 -4
  95. package/tsconfig.json +6 -15
  96. package/dist/api/RootComponent/Data/expected/error.js +0 -10
  97. package/dist/api/RootComponent/Data/expected/error.js.map +0 -1
  98. package/dist/api/RootComponent/Data/expected/normal.d.ts +0 -1
  99. package/dist/api/RootComponent/Data/expected/normal.js +0 -33
  100. package/dist/api/RootComponent/Data/expected/normal.js.map +0 -1
  101. package/dist/api/RootComponent/Properties/expected/error.d.ts +0 -1
  102. package/dist/api/RootComponent/Properties/expected/error.js +0 -38
  103. package/dist/api/RootComponent/Properties/expected/error.js.map +0 -1
  104. package/dist/api/RootComponent/Properties/expected/normalEmpty.d.ts +0 -1
  105. package/dist/api/RootComponent/Properties/expected/normalEmpty.js.map +0 -1
  106. package/dist/api/RootComponent/Properties/expected/normalOptional.d.ts +0 -1
  107. package/dist/api/RootComponent/Properties/expected/normalOptional.js +0 -80
  108. package/dist/api/RootComponent/Properties/expected/normalOptional.js.map +0 -1
  109. package/dist/api/RootComponent/Properties/expected/normalRequired.d.ts +0 -69
  110. package/dist/api/RootComponent/Properties/expected/normalRequired.js +0 -44
  111. package/dist/api/RootComponent/Properties/expected/normalRequired.js.map +0 -1
  112. package/dist/api/RootComponent/Properties/test/normalEmpty.test..d.ts +0 -1
  113. package/dist/api/RootComponent/Properties/test/normalEmpty.test..js +0 -20
  114. package/dist/api/RootComponent/Properties/test/normalEmpty.test..js.map +0 -1
  115. package/dist/types/RestorePropTypeOfList.d.ts +0 -2
  116. package/dist/types/RestorePropTypeOfList.js +0 -3
  117. package/dist/types/RestorePropTypeOfList.js.map +0 -1
  118. package/dist/utils/computedUpdater.js.map +0 -1
  119. package/dist/utils/getPathsValue.js.map +0 -1
  120. package/dist/utils/initWatchOldValue.d.ts +0 -2
  121. package/dist/utils/initWatchOldValue.js +0 -10
  122. package/dist/utils/initWatchOldValue.js.map +0 -1
  123. package/dist/utils/preprocessingOptions.d.ts +0 -18
  124. package/dist/utils/preprocessingOptions.js +0 -184
  125. package/dist/utils/preprocessingOptions.js.map +0 -1
  126. package/src/api/RootComponent/Data/expected/error.ts +0 -14
  127. package/src/api/RootComponent/Data/expected/normal.ts +0 -68
  128. package/src/api/RootComponent/Properties/expected/error.ts +0 -49
  129. package/src/api/RootComponent/Properties/expected/normalEmpty.ts +0 -33
  130. package/src/api/RootComponent/Properties/expected/normalOptional.ts +0 -136
  131. package/src/api/RootComponent/Properties/expected/normalRequired.ts +0 -100
  132. package/src/types/RestorePropTypeOfList.ts +0 -20
  133. package/src/utils/initWatchOldValue.ts +0 -12
  134. package/src/utils/preprocessingOptions.ts +0 -334
  135. /package/dist/api/RootComponent/{Data/expected/error.d.ts → Properties/test/normalEmpty.test.d.ts} +0 -0
  136. /package/dist/{utils → behaviors/BComputedAndWatch}/getPathsValue.js +0 -0
  137. /package/src/api/RootComponent/Properties/test/{normalEmpty.test..ts → normalEmpty.test.ts} +0 -0
@@ -1,334 +0,0 @@
1
- import type { Func } from "hry-types/src/Misc/Func";
2
- import { comparer, reaction, toJS } from "mobx";
3
- import type { ComponentOptions, FuncConfig } from "../api/DefineComponent";
4
-
5
- import type { FullCustomEvents, ShortCustomeEvents } from "../api/RootComponent/CustomEvents/CustomEventConstraint";
6
- import type { PageInstance } from "../api/RootComponent/Instance/RootComponentInstance";
7
- import type { RootComponentDoc } from "../api/RootComponent/RootComponentDoc";
8
- import type { SubComponentOptions } from "../api/SubComponent";
9
- import type { Instance } from "../behaviors/BComputedAndWatch/types";
10
- import { deepClone } from "./deepClone";
11
- import { INNERMARKER } from "./InnerMarker";
12
-
13
- // 类型守卫
14
- function IsFullCustomEvents(
15
- customEventOptions: FullCustomEvents | ShortCustomeEvents,
16
- ): customEventOptions is FullCustomEvents {
17
- return Object.prototype.toString.call(customEventOptions) === "[object Object]";
18
- }
19
- /**
20
- * 把customEvents字段配置变成函数放入到componentOptions.methods中
21
- */
22
- function customEventsHandle(componentOptions: ComponentOptions, customEventsConfig: object) {
23
- componentOptions.methods ||= {};
24
-
25
- for (const key in customEventsConfig) {
26
- const customEventOptions = customEventsConfig[key];
27
- if (IsFullCustomEvents(customEventOptions)) {
28
- componentOptions.methods[key] = function(detail: unknown) {
29
- this.triggerEvents(key, detail, customEventOptions.options);
30
- };
31
- } else {
32
- componentOptions.methods[key] = function(detail: unknown) {
33
- this.triggerEvents(key, detail);
34
- };
35
- }
36
- }
37
- }
38
- /**
39
- * 把customEvents字段配置变成函数放入到componentOptions.methods中
40
- */
41
- function eventsHandle(componentOptions: ComponentOptions, eventsConfig: object) {
42
- componentOptions.methods ||= {};
43
-
44
- Object.assign(componentOptions.methods, eventsConfig);
45
- }
46
- /**
47
- * 把配置为函数的字段方法收集到funcConfig中
48
- */
49
- function funcFieldsCollect(
50
- config: Record<string, Func>,
51
- funcConfig: FuncConfig,
52
- type: "pageLifetimes" | "lifetimes" | "watch",
53
- ) {
54
- for (const key in config) {
55
- const handler = config[key];
56
- // 收集声明周期函数
57
- const tempCache: object = funcConfig[type] ||= {};
58
- const filedList: Func[] = tempCache[key] ||= [];
59
-
60
- filedList.push(handler);
61
- }
62
- }
63
- // 其他字段加入到componentOptions对应字段配置中
64
- function otherFieldsHandle(
65
- componentOptions: ComponentOptions,
66
- rootComponentOptions: Omit<RootComponentDoc, "customEvents" | "events" | "pageLifetimes">,
67
- ) {
68
- for (const key in rootComponentOptions) {
69
- const config = rootComponentOptions[key];
70
- if (Array.isArray(config)) {
71
- componentOptions[key] ||= ([] as any[]).push(...config);
72
- } else {
73
- Object.assign(componentOptions[key] ||= {}, config);
74
- }
75
- }
76
- }
77
- export function rootComponentFieldHandle(
78
- rootComponentConfig: RootComponentDoc,
79
- componentOptions: ComponentOptions,
80
- funcConfig: FuncConfig,
81
- ) {
82
- rootComponentConfig.customEvents && customEventsHandle(componentOptions, rootComponentConfig.customEvents);
83
-
84
- rootComponentConfig.events && eventsHandle(componentOptions, rootComponentConfig.events);
85
-
86
- rootComponentConfig.pageLifetimes
87
- && funcFieldsCollect(rootComponentConfig.pageLifetimes, funcConfig, "pageLifetimes");
88
-
89
- rootComponentConfig.lifetimes && funcFieldsCollect(rootComponentConfig.lifetimes, funcConfig, "lifetimes");
90
-
91
- rootComponentConfig.watch && funcFieldsCollect(rootComponentConfig.watch, funcConfig, "watch");
92
-
93
- otherFieldsHandle(componentOptions, rootComponentConfig);
94
- }
95
-
96
- export function subComponentsHandle(
97
- componentOptions: ComponentOptions,
98
- subComponents: SubComponentOptions[],
99
- funcConfig: FuncConfig,
100
- ) {
101
- subComponents.forEach((subOption) => {
102
- subOption.events && eventsHandle(componentOptions, subOption.events);
103
-
104
- subOption.pageLifetimes && funcFieldsCollect(subOption.pageLifetimes, funcConfig, "pageLifetimes");
105
-
106
- subOption.lifetimes && funcFieldsCollect(subOption.lifetimes, funcConfig, "lifetimes");
107
-
108
- subOption.watch && funcFieldsCollect(subOption.watch, funcConfig, "watch");
109
-
110
- otherFieldsHandle(componentOptions, subOption);
111
- });
112
- }
113
-
114
- /**
115
- * 把函数配置放入一个配置中依次运行
116
- */
117
- function funcCofnigHandle(methodsConfig: object, configList: Record<string, Func[]>) {
118
- for (const key in configList) {
119
- methodsConfig[key] = function(...args: unknown[]) {
120
- configList[key].forEach(ele => ele.call(this, ...args));
121
- };
122
- }
123
- }
124
-
125
- export function funcConfigHandle(
126
- componentOptions: ComponentOptions,
127
- isPage: boolean | undefined,
128
- funcConfig: FuncConfig,
129
- ) {
130
- if (isPage) {
131
- // 页面时 生命周期方法(即 on 开头的方法),(https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/component.html)
132
- funcConfig.pageLifetimes && funcCofnigHandle(componentOptions.methods ||= {}, funcConfig.pageLifetimes);
133
- } else {
134
- // 组件时
135
- funcConfig.pageLifetimes && funcCofnigHandle(componentOptions.pageLifetimes ||= {}, funcConfig.pageLifetimes);
136
- }
137
- funcConfig.lifetimes && funcCofnigHandle(componentOptions.lifetimes ||= {}, funcConfig.lifetimes);
138
-
139
- funcConfig.watch && funcCofnigHandle(componentOptions.watch ||= {}, funcConfig.watch);
140
- }
141
-
142
- export function createdHijack(
143
- componentOptions: ComponentOptions,
144
- before: Func[] = [],
145
- after: Func[] = [],
146
- ) {
147
- const lifetimes = componentOptions.lifetimes ||= {};
148
- // const originalOptions = deepClone(options);
149
- const originalCreatedFn = lifetimes.created;
150
-
151
- lifetimes.created = function() {
152
- before.forEach((func) => {
153
- func.call(this, componentOptions);
154
- });
155
-
156
- originalCreatedFn && originalCreatedFn.call(this);
157
-
158
- after.forEach((func) => {
159
- func.call(this, componentOptions);
160
- });
161
- };
162
- }
163
-
164
- /**
165
- * 把computed配置加入到methods中,带入到组件实例中
166
- */
167
- export function addComputedFieldToMethods(componentOptions: ComponentOptions) {
168
- componentOptions.methods ??= {};
169
-
170
- componentOptions.methods.__computedConfig__ = () => componentOptions.computed;
171
-
172
- componentOptions.methods.xxx = () => ({
173
- a() {
174
- return 123;
175
- },
176
- b: 345,
177
- });
178
- // delete componentOptions.computed;
179
- }
180
-
181
- // /**
182
- // * 初始化store把store配置加入到methods中,带入到组件实例中
183
- // */
184
- // export function addStoreConfigToMethods(componentOptions: ComponentOptions) {
185
- // // 把响应式数据配置保留在methods的__storeConfig__字段下带入到组件实例中(不用函数返回方式也可以,但不符合methods字段类型),后续再从原型上删除。
186
- // componentOptions.methods ||= {};
187
-
188
- // const storeConfig = componentOptions.store;
189
-
190
- // componentOptions.methods.__storeConfig__ = () => storeConfig;
191
-
192
- // delete componentOptions.store;
193
- // }
194
-
195
- export function attachedHijack(
196
- componentOptions: ComponentOptions,
197
- beforeHandleList: Func[] = [],
198
- afterHandleList: Func[] = [],
199
- ) {
200
- componentOptions.lifetimes ??= {};
201
-
202
- const originAttached = componentOptions.lifetimes.attached;
203
-
204
- componentOptions.lifetimes.attached = function() {
205
- beforeHandleList.forEach(handleFunc => {
206
- handleFunc.call(this);
207
- });
208
-
209
- originAttached && originAttached.call(this);
210
-
211
- afterHandleList.forEach(handleFunc => {
212
- handleFunc.call(this);
213
- });
214
- };
215
- }
216
-
217
- export function detachedHijack(
218
- componentOptions: ComponentOptions,
219
- beforeHandleList: Func[] = [],
220
- afterHandleList: Func[] = [],
221
- ) {
222
- componentOptions.lifetimes ??= {};
223
-
224
- const originDetached = componentOptions.lifetimes.detached;
225
-
226
- componentOptions.lifetimes.detached = function() {
227
- beforeHandleList.forEach(handleFunc => {
228
- handleFunc.call(this);
229
- });
230
-
231
- originDetached && originDetached.call(this);
232
-
233
- afterHandleList.forEach(handleFunc => {
234
- handleFunc.call(this);
235
- });
236
- };
237
- }
238
-
239
- export function storeHandle(this: Instance) {
240
- // 取出通过methods带入的responsiveConfig
241
- const responsiveConfig = this.__storeConfig__?.();
242
- if (!responsiveConfig) return;
243
- for (const key in responsiveConfig) {
244
- // 占用this的key,未对有可能的键冲突做处理
245
- this.disposer ||= {};
246
-
247
- // 添加响应式逻辑
248
- this.disposer[key] = reaction(
249
- responsiveConfig[key],
250
- (value: unknown) => {
251
- // 加入到待setData对象中
252
- this.setData({
253
- [key]: toJS(value),
254
- });
255
- },
256
- {
257
- equals: comparer.default,
258
- // fireImmediately: false,
259
- },
260
- );
261
- }
262
- // methods属性是加载在实例原型上的
263
- Reflect.deleteProperty((this as any).__proto__, "__storeConfig__");
264
- }
265
-
266
- /**
267
- * 配置中排除指定字段(报错)
268
- */
269
- export function excludeFields(config: object, excludeKeys: string[]) {
270
- const methodsConfigKeys = Object.keys(config);
271
- for (const key of excludeKeys) {
272
- if (methodsConfigKeys.includes(key)) {
273
- throw Error(`${key}已被内部字段占用`);
274
- }
275
- }
276
- }
277
-
278
- /**
279
- * onLoad生命周期劫持函数
280
- */
281
- export function onLoadHijack(
282
- options: ComponentOptions,
283
- before: Func[],
284
- after: Func[] = [],
285
- ) {
286
- options.methods ||= {};
287
-
288
- const cloneOpt = deepClone(options);
289
- const originalOnLoad: Func | undefined = options.methods.onLoad;
290
-
291
- options.methods.onLoad = function(props: unknown) {
292
- before.forEach((func) => {
293
- func.call(this, props, cloneOpt);
294
- });
295
-
296
- originalOnLoad?.call(this, props);
297
-
298
- after.forEach((func) => {
299
- func.call(this, props, cloneOpt);
300
- });
301
- };
302
- }
303
-
304
- /**
305
- * 对页面onLoad的参数data处理
306
- * 1. 多余字段检测
307
- * 2. 少传字段报错
308
- * 3. 类型错误报错
309
- * 针对通过 navigateTo传过来的数据解析
310
- */
311
- /**
312
- * @param option - option中的url是拼接了encodeURIComponent转码的data对象的,key为INNERMARKER.url
313
- */
314
- export function onLoadReceivedDataHandle(
315
- this: PageInstance,
316
- option: Record<typeof INNERMARKER.url, string>,
317
- ) {
318
- const innerData: string | undefined = option[INNERMARKER.url];
319
- // 未使用自定义的navigateTo
320
- if (innerData === undefined) return;
321
- const optionData = JSON.parse(decodeURIComponent(innerData));
322
-
323
- // 使用navigateTo
324
- for (const key in optionData) {
325
- option[key] = optionData[key];
326
- }
327
-
328
- // setData会触发计算属性 继承属性等 。
329
- this.setData(option as any);
330
-
331
- delete option[INNERMARKER.url];
332
-
333
- delete this.data[INNERMARKER.url];
334
- }