glass-easel-miniprogram-adapter 0.2.0 → 0.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.
Files changed (62) hide show
  1. package/.eslintignore +4 -0
  2. package/dist/glass_easel_miniprogram_adapter.d.ts +872 -1
  3. package/dist/glass_easel_miniprogram_adapter.es.js +2 -0
  4. package/dist/glass_easel_miniprogram_adapter.es.js.map +1 -0
  5. package/dist/glass_easel_miniprogram_adapter.js +2 -2
  6. package/dist/glass_easel_miniprogram_adapter.js.map +1 -1
  7. package/jest.config.js +7 -4
  8. package/package.json +8 -6
  9. package/rollup.config.ts +83 -0
  10. package/src/backend.ts +7 -3
  11. package/src/behavior.ts +8 -2
  12. package/src/builder/base_behavior_builder.ts +437 -0
  13. package/src/builder/behavior_builder.ts +265 -0
  14. package/src/builder/component_builder.ts +337 -0
  15. package/src/builder/index.ts +2 -0
  16. package/src/builder/type_utils.ts +98 -0
  17. package/src/component.ts +10 -7
  18. package/src/env.ts +73 -0
  19. package/src/index.ts +3 -77
  20. package/src/intersection.ts +2 -2
  21. package/src/media_query.ts +4 -6
  22. package/src/selector_query.ts +3 -3
  23. package/src/space.ts +11 -1030
  24. package/src/types.ts +7 -5
  25. package/tests/base/env.ts +1 -1
  26. package/tests/env.test.ts +3 -3
  27. package/tests/selector.test.ts +137 -14
  28. package/tests/space.test.ts +8 -14
  29. package/tsconfig.json +1 -3
  30. package/dist/types/src/backend.d.ts +0 -59
  31. package/dist/types/src/backend.d.ts.map +0 -1
  32. package/dist/types/src/behavior.d.ts +0 -35
  33. package/dist/types/src/behavior.d.ts.map +0 -1
  34. package/dist/types/src/component.d.ts +0 -170
  35. package/dist/types/src/component.d.ts.map +0 -1
  36. package/dist/types/src/index.d.ts +0 -58
  37. package/dist/types/src/index.d.ts.map +0 -1
  38. package/dist/types/src/intersection.d.ts +0 -24
  39. package/dist/types/src/intersection.d.ts.map +0 -1
  40. package/dist/types/src/media_query.d.ts +0 -13
  41. package/dist/types/src/media_query.d.ts.map +0 -1
  42. package/dist/types/src/selector_query.d.ts +0 -98
  43. package/dist/types/src/selector_query.d.ts.map +0 -1
  44. package/dist/types/src/space.d.ts +0 -369
  45. package/dist/types/src/space.d.ts.map +0 -1
  46. package/dist/types/src/types.d.ts +0 -77
  47. package/dist/types/src/types.d.ts.map +0 -1
  48. package/dist/types/src/utils.d.ts +0 -2
  49. package/dist/types/src/utils.d.ts.map +0 -1
  50. package/dist/types/tests/base/env.d.ts +0 -3
  51. package/dist/types/tests/base/env.d.ts.map +0 -1
  52. package/dist/types/tests/data_update.test.d.ts +0 -2
  53. package/dist/types/tests/data_update.test.d.ts.map +0 -1
  54. package/dist/types/tests/env.test.d.ts +0 -2
  55. package/dist/types/tests/env.test.d.ts.map +0 -1
  56. package/dist/types/tests/selector.test.d.ts +0 -2
  57. package/dist/types/tests/selector.test.d.ts.map +0 -1
  58. package/dist/types/tests/space.test.d.ts +0 -2
  59. package/dist/types/tests/space.test.d.ts.map +0 -1
  60. package/dist/types/tests/trait_behavior.test.d.ts +0 -2
  61. package/dist/types/tests/trait_behavior.test.d.ts.map +0 -1
  62. package/webpack.config.js +0 -79
package/src/behavior.ts CHANGED
@@ -1,5 +1,6 @@
1
- import * as glassEasel from 'glass-easel'
2
- import { GeneralComponentDefinition, utils as typeUtils } from './types'
1
+ import type * as glassEasel from 'glass-easel'
2
+ import { type GeneralComponentDefinition, type utils as typeUtils } from './types'
3
+ import { type GeneralComponent } from './component'
3
4
 
4
5
  type DataList = typeUtils.DataList
5
6
  type PropertyList = typeUtils.PropertyList
@@ -23,19 +24,24 @@ export class Behavior<
23
24
  TProperty extends PropertyList,
24
25
  TMethod extends MethodList,
25
26
  TChainingFilter extends ChainingFilterType,
27
+ TComponentExport = never,
26
28
  > {
27
29
  /** @internal */
28
30
  _$: glassEasel.GeneralBehavior
29
31
  /** @internal */
30
32
  _$bindedDefinitionFilter?: (target: GeneralComponentDefinition) => void
33
+ /** @internal */
34
+ _$export?: (source: GeneralComponent | null) => TComponentExport
31
35
 
32
36
  /** @internal */
33
37
  constructor(
34
38
  inner: glassEasel.Behavior<TData, TProperty, TMethod, TChainingFilter>,
35
39
  parents: GeneralBehavior[],
36
40
  definitionFilter: DefinitionFilter | undefined,
41
+ componentExport: ((source: GeneralComponent | null) => TComponentExport) | undefined,
37
42
  ) {
38
43
  this._$ = inner as glassEasel.GeneralBehavior
44
+ this._$export = componentExport
39
45
 
40
46
  // processing definition filter
41
47
  if (definitionFilter !== undefined) {
@@ -0,0 +1,437 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
2
+
3
+ import * as glassEasel from 'glass-easel'
4
+ import { Behavior, ComponentType, TraitBehavior } from '../behavior'
5
+ import type { BehaviorDefinition, utils as typeUtils } from '../types'
6
+ import type { GeneralBehavior } from '../behavior'
7
+ import type { AllData, Component, GeneralComponent } from '../component'
8
+ import type { CodeSpace } from '../space'
9
+ import type {
10
+ ResolveBehaviorBuilder,
11
+ Lifetimes,
12
+ RelationParams,
13
+ TraitRelationParams,
14
+ BuilderContext,
15
+ } from './type_utils'
16
+
17
+ type Empty = typeUtils.Empty
18
+ type DataList = typeUtils.DataList
19
+ type PropertyList = typeUtils.PropertyList
20
+ type PropertyType = typeUtils.PropertyType
21
+ type PropertyTypeToValueType<T extends PropertyType> = typeUtils.PropertyTypeToValueType<T>
22
+ type MethodList = typeUtils.MethodList
23
+ type ChainingFilterType = typeUtils.ChainingFilterType
24
+ type ComponentMethod = typeUtils.ComponentMethod
25
+ type TaggedMethod<Fn extends ComponentMethod> = typeUtils.TaggedMethod<Fn>
26
+
27
+ export class BaseBehaviorBuilder<
28
+ TPrevData extends DataList = Empty,
29
+ TData extends DataList = Empty,
30
+ TProperty extends PropertyList = Empty,
31
+ TMethod extends MethodList = Empty,
32
+ TChainingFilter extends ChainingFilterType = never,
33
+ TPendingChainingFilter extends ChainingFilterType = never,
34
+ TComponentExport = never,
35
+ > {
36
+ protected _$codeSpace!: CodeSpace
37
+ protected _$!: glassEasel.BehaviorBuilder<
38
+ TPrevData,
39
+ TData,
40
+ TProperty,
41
+ TMethod,
42
+ TChainingFilter,
43
+ TPendingChainingFilter
44
+ >
45
+ protected _$parents: GeneralBehavior[] = []
46
+ protected _$export?: (source: GeneralComponent | null) => TComponentExport
47
+
48
+ /** Implement a trait behavior */
49
+ implement<TIn extends { [key: string]: any }>(
50
+ traitBehavior: TraitBehavior<TIn, any>,
51
+ impl: TIn,
52
+ ): ResolveBehaviorBuilder<this, TChainingFilter> {
53
+ this._$.implement(traitBehavior._$, impl)
54
+ return this as any
55
+ }
56
+
57
+ /** Add external classes */
58
+ externalClasses(list: string[]): this {
59
+ this._$.externalClasses(list)
60
+ return this
61
+ }
62
+
63
+ /** Set the export value when the component is being selected */
64
+ export<TNewComponentExport>(
65
+ f: (this: GeneralComponent, source: GeneralComponent | null) => TNewComponentExport,
66
+ ): ResolveBehaviorBuilder<
67
+ BaseBehaviorBuilder<
68
+ TPrevData,
69
+ TData,
70
+ TProperty,
71
+ TMethod,
72
+ TChainingFilter,
73
+ TPendingChainingFilter,
74
+ TNewComponentExport
75
+ >,
76
+ TChainingFilter
77
+ > {
78
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
79
+ this._$export = f as any
80
+ return this as any
81
+ }
82
+
83
+ data<T extends DataList>(
84
+ gen: () => typeUtils.NewFieldList<AllData<TData, TProperty>, T>,
85
+ ): ResolveBehaviorBuilder<
86
+ BaseBehaviorBuilder<
87
+ T,
88
+ TData & T,
89
+ TProperty,
90
+ TMethod,
91
+ TChainingFilter,
92
+ TPendingChainingFilter,
93
+ TComponentExport
94
+ >,
95
+ TChainingFilter
96
+ > {
97
+ this._$.data(gen)
98
+ return this as any
99
+ }
100
+
101
+ property<N extends string, T extends PropertyType, V extends PropertyTypeToValueType<T>>(
102
+ name: N,
103
+ def: N extends keyof (TData & TProperty) ? never : typeUtils.PropertyListItem<T, V>,
104
+ ): ResolveBehaviorBuilder<
105
+ BaseBehaviorBuilder<
106
+ TPrevData,
107
+ TData,
108
+ TProperty & Record<N, unknown extends V ? T : typeUtils.PropertyOption<T, V>>,
109
+ TMethod,
110
+ TChainingFilter,
111
+ TPendingChainingFilter,
112
+ TComponentExport
113
+ >,
114
+ TChainingFilter
115
+ > {
116
+ this._$.property(name, def)
117
+ return this as any
118
+ }
119
+
120
+ /**
121
+ * Add some public methods
122
+ *
123
+ * The public method can be used as an event handler, and can be visited in component instance.
124
+ */
125
+ methods<T extends MethodList>(
126
+ funcs: T & ThisType<Component<TData, TProperty, TMethod & T, any>>,
127
+ ): ResolveBehaviorBuilder<
128
+ BaseBehaviorBuilder<
129
+ TPrevData,
130
+ TData,
131
+ TProperty,
132
+ TMethod & T,
133
+ TChainingFilter,
134
+ TPendingChainingFilter,
135
+ TComponentExport
136
+ >,
137
+ TChainingFilter
138
+ > {
139
+ this._$.methods(funcs)
140
+ return this as any
141
+ }
142
+
143
+ /**
144
+ * Add a data observer
145
+ */
146
+ observer<
147
+ P extends typeUtils.ObserverDataPathStrings<
148
+ typeUtils.DataWithPropertyValues<TPrevData, TProperty>
149
+ >,
150
+ V = typeUtils.GetFromObserverPathString<
151
+ typeUtils.DataWithPropertyValues<TPrevData, TProperty>,
152
+ P
153
+ >,
154
+ >(
155
+ paths: P,
156
+ func: (this: Component<TData, TProperty, TMethod, any>, newValue: V) => void,
157
+ once?: boolean,
158
+ ): ResolveBehaviorBuilder<this, TChainingFilter>
159
+ observer<
160
+ P extends typeUtils.ObserverDataPathStrings<
161
+ typeUtils.DataWithPropertyValues<TPrevData, TProperty>
162
+ >[],
163
+ V = {
164
+ [K in keyof P]: typeUtils.GetFromObserverPathString<
165
+ typeUtils.DataWithPropertyValues<TPrevData, TProperty>,
166
+ P[K]
167
+ >
168
+ },
169
+ >(
170
+ paths: readonly [...P],
171
+ func: (
172
+ this: Component<TData, TProperty, TMethod, any>,
173
+ ...newValues: V extends any[] ? V : never
174
+ ) => void,
175
+ once?: boolean,
176
+ ): ResolveBehaviorBuilder<this, TChainingFilter>
177
+ observer(
178
+ paths: string | readonly string[],
179
+ func: (this: Component<TData, TProperty, TMethod, any>, ...args: any[]) => any,
180
+ once = false,
181
+ ): ResolveBehaviorBuilder<this, TChainingFilter> {
182
+ this._$.observer(paths as any, func as any, once)
183
+ return this as any
184
+ }
185
+
186
+ /**
187
+ * Add a lifetime callback
188
+ */
189
+ lifetime<L extends keyof Lifetimes>(
190
+ name: L,
191
+ func: (
192
+ this: Component<TData, TProperty, TMethod, any>,
193
+ ...args: Parameters<Lifetimes[L]>
194
+ ) => ReturnType<Lifetimes[L]>,
195
+ once = false,
196
+ ): ResolveBehaviorBuilder<this, TChainingFilter> {
197
+ this._$.lifetime(name, func as any, once)
198
+ return this as any
199
+ }
200
+
201
+ /**
202
+ * Add a page-lifetime callback
203
+ */
204
+ pageLifetime(
205
+ name: string,
206
+ func: (this: Component<TData, TProperty, TMethod, any>, ...args: any[]) => any,
207
+ once = false,
208
+ ): ResolveBehaviorBuilder<this, TChainingFilter> {
209
+ this._$.pageLifetime(name, func as any, once)
210
+ return this as any
211
+ }
212
+
213
+ /**
214
+ * Add a relation
215
+ */
216
+ relation(
217
+ name: string,
218
+ rel: RelationParams & ThisType<Component<TData, TProperty, TMethod, TComponentExport>>,
219
+ ): ResolveBehaviorBuilder<this, TChainingFilter> {
220
+ const target =
221
+ rel.target instanceof Behavior || rel.target instanceof ComponentType
222
+ ? rel.target._$
223
+ : rel.target
224
+ this._$.relation(name, {
225
+ target,
226
+ type: rel.type,
227
+ linked: rel.linked,
228
+ linkChanged: rel.linkChanged,
229
+ unlinked: rel.unlinked,
230
+ linkFailed: rel.linkFailed,
231
+ } as any)
232
+ return this as any
233
+ }
234
+
235
+ init<TExport extends Record<string, TaggedMethod<(...args: any[]) => any>> | void>(
236
+ func: (
237
+ this: Component<TData, TProperty, TMethod, TComponentExport>,
238
+ builderContext: BuilderContext<
239
+ TPrevData,
240
+ TProperty,
241
+ Component<TData, TProperty, TMethod, TComponentExport>
242
+ >,
243
+ ) => TExport,
244
+ // eslint-disable-next-line function-paren-newline
245
+ ): ResolveBehaviorBuilder<
246
+ BaseBehaviorBuilder<
247
+ TPrevData,
248
+ TData,
249
+ TProperty,
250
+ TMethod,
251
+ TChainingFilter,
252
+ TPendingChainingFilter,
253
+ TComponentExport
254
+ >,
255
+ TChainingFilter
256
+ > {
257
+ this._$.init(function ({
258
+ data,
259
+ setData,
260
+ implement,
261
+ relation,
262
+ observer,
263
+ lifetime,
264
+ pageLifetime,
265
+ method,
266
+ listener,
267
+ }) {
268
+ const relationInit = ((rel: RelationParams | TraitRelationParams<any>) => {
269
+ if (rel.target instanceof TraitBehavior) {
270
+ return relation({
271
+ target: rel.target._$,
272
+ type: rel.type,
273
+ linked: rel.linked,
274
+ linkChanged: rel.linkChanged,
275
+ unlinked: rel.unlinked,
276
+ linkFailed: rel.linkFailed,
277
+ } as any)
278
+ }
279
+ const target =
280
+ rel.target instanceof Behavior || rel.target instanceof ComponentType
281
+ ? rel.target._$
282
+ : rel.target
283
+ return relation({
284
+ target,
285
+ type: rel.type,
286
+ linked: rel.linked,
287
+ linkChanged: rel.linkChanged,
288
+ unlinked: rel.unlinked,
289
+ linkFailed: rel.linkFailed,
290
+ } as any)
291
+ }) as BuilderContext<TPrevData, TProperty, TMethod>['relation']
292
+
293
+ const implementInit = ((traitBehavior: TraitBehavior<any>, impl: Record<string, any>) => {
294
+ implement(traitBehavior._$, impl)
295
+ }) as BuilderContext<TPrevData, TProperty, TMethod>['implement']
296
+
297
+ const methodCaller = this as unknown as Component<TData, TProperty, TMethod, TComponentExport>
298
+
299
+ return func.call(methodCaller, {
300
+ self: methodCaller,
301
+ data,
302
+ setData: (newData, callback) => {
303
+ setData(newData)
304
+ if (callback) {
305
+ glassEasel.triggerRender(methodCaller._$, () => {
306
+ callback()
307
+ })
308
+ }
309
+ },
310
+ implement: implementInit,
311
+ relation: relationInit,
312
+ observer,
313
+ lifetime,
314
+ pageLifetime,
315
+ method,
316
+ listener,
317
+ })
318
+ })
319
+ return this as any
320
+ }
321
+
322
+ definition<
323
+ TNewData extends DataList = Empty,
324
+ TNewProperty extends PropertyList = Empty,
325
+ TNewMethod extends MethodList = Empty,
326
+ TNewComponentExport = never,
327
+ >(
328
+ def: BehaviorDefinition<TNewData, TNewProperty, TNewMethod, TNewComponentExport> &
329
+ ThisType<
330
+ Component<
331
+ TData & TNewData,
332
+ TProperty & TNewProperty,
333
+ TMethod & TNewMethod,
334
+ TNewComponentExport
335
+ >
336
+ >,
337
+ ): ResolveBehaviorBuilder<
338
+ BaseBehaviorBuilder<
339
+ TPrevData,
340
+ TData & TNewData,
341
+ TProperty & TNewProperty,
342
+ TMethod & TNewMethod,
343
+ TChainingFilter,
344
+ TPendingChainingFilter,
345
+ TNewComponentExport
346
+ >,
347
+ TChainingFilter
348
+ > {
349
+ def.behaviors?.forEach((beh) => beh._$bindedDefinitionFilter?.(def))
350
+ const inner = this._$
351
+ const {
352
+ behaviors,
353
+ properties: rawProperties,
354
+ data: rawData,
355
+ observers: rawObservers,
356
+ methods,
357
+ created,
358
+ attached,
359
+ ready,
360
+ moved,
361
+ detached,
362
+ lifetimes: rawLifetimes,
363
+ pageLifetimes: rawPageLifetimes,
364
+ relations: rawRelations,
365
+ externalClasses,
366
+ export: exports,
367
+ } = def
368
+ behaviors?.forEach((beh) => {
369
+ this._$parents.push(beh as GeneralBehavior)
370
+ inner.behavior(beh._$)
371
+ })
372
+ if (rawData !== undefined) {
373
+ if (typeof rawData === 'function') {
374
+ inner.data(rawData as any)
375
+ } else {
376
+ inner.staticData(rawData as any)
377
+ }
378
+ }
379
+ if (rawProperties !== undefined) {
380
+ const keys = Object.keys(rawProperties)
381
+ for (let i = 0; i < keys.length; i += 1) {
382
+ const name = keys[i]!
383
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
384
+ inner.property(name, rawProperties[name] as any)
385
+ }
386
+ }
387
+ if (rawObservers !== undefined) {
388
+ if (Array.isArray(rawObservers)) {
389
+ for (let i = 0; i < rawObservers.length; i += 1) {
390
+ const { fields, observer } = rawObservers[i]!
391
+ inner.observer(fields || ('**' as any), observer)
392
+ }
393
+ } else {
394
+ const keys = Object.keys(rawObservers)
395
+ for (let i = 0; i < keys.length; i += 1) {
396
+ const fields = keys[i]!
397
+ const observer = rawObservers[fields]!
398
+ inner.observer(fields as any, observer)
399
+ }
400
+ }
401
+ }
402
+ if (methods) inner.methods(methods)
403
+ if (created && rawLifetimes?.created === undefined) inner.lifetime('created', created, true)
404
+ if (attached && rawLifetimes?.attached === undefined) inner.lifetime('attached', attached, true)
405
+ if (ready && rawLifetimes?.ready === undefined) inner.lifetime('ready', ready, true)
406
+ if (moved && rawLifetimes?.moved === undefined) inner.lifetime('moved', moved, true)
407
+ if (detached && rawLifetimes?.detached === undefined) inner.lifetime('detached', detached, true)
408
+ if (rawLifetimes) {
409
+ const keys = Object.keys(rawLifetimes)
410
+ for (let i = 0; i < keys.length; i += 1) {
411
+ const name = keys[i]!
412
+ const func = rawLifetimes[name]!
413
+ inner.lifetime(name as any, func, true)
414
+ }
415
+ }
416
+ if (rawPageLifetimes) {
417
+ const keys = Object.keys(rawPageLifetimes)
418
+ for (let i = 0; i < keys.length; i += 1) {
419
+ const name = keys[i]!
420
+ const func = rawPageLifetimes[name]!
421
+ inner.pageLifetime(name, func, true)
422
+ }
423
+ }
424
+ if (rawRelations) {
425
+ const keys = Object.keys(rawRelations)
426
+ for (let i = 0; i < keys.length; i += 1) {
427
+ const name = keys[i]!
428
+ const rel = rawRelations[name]!
429
+ inner.relation(name, rel)
430
+ }
431
+ }
432
+ if (externalClasses) inner.externalClasses(externalClasses)
433
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
434
+ if (exports) this._$export = exports as any
435
+ return this as any
436
+ }
437
+ }