1
+ import { isMiniProgramEnvironment } from '.'
1
2
import { GeneralFuncType , FuncArr } from './func_arr'
2
3
import { TraitBehavior , TraitGroup } from './trait_behavior'
3
4
import {
@@ -156,7 +157,7 @@ class ChainingPolyfillMetadata {
156
157
const exportedKey = exportedKeys [ j ] !
157
158
const exportItem = ( exported as { [ k in string ] : unknown } ) [ exportedKey ]
158
159
if ( isTaggedMethod ( exportItem ) ) {
159
- comp . methods [ exportedKey ] = exportItem
160
+ comp [ exportedKey ] = exportItem
160
161
}
161
162
}
162
163
}
@@ -175,55 +176,6 @@ class ChainingPolyfillMetadata {
175
176
}
176
177
}
177
178
178
- const getChainingPolyfillMetadata = ( comp : GeneralComponent ) : ChainingPolyfillMetadata => {
179
- return comp . _$chainingPolyfill
180
- }
181
-
182
- // const takeChainingPolyfillInitData = (
183
- // comp: GeneralComponent,
184
- // ): ChainingPolyfillInitData | undefined => {
185
- // const id = comp.data._$chainingPolyfillId
186
- // if (!(id >= 0)) return undefined
187
- // comp.data._$chainingPolyfillId = -1
188
- // const initData = initDataMap[id]
189
- // const cloneMap = (src: { [k: string]: FuncArr<any> }) => {
190
- // const dest = {} as typeof src
191
- // const keys = Object.keys(src)
192
- // for (let i = 0; i < keys.length; i += 1) {
193
- // const key = keys[i]!
194
- // dest[key] = src[key].clone()
195
- // }
196
- // return dest
197
- // }
198
- // return {
199
- // lifetimes: cloneMap(initData.lifetimes),
200
- // pageLifetimes: cloneMap(initData.pageLifetimes),
201
- // observers: cloneMap(initData.observers),
202
- // initFuncs: initData.initFuncs.clone(),
203
- // }
204
- // }
205
-
206
- const generateChainingPolyfillBehavior = ( initData : ChainingPolyfillInitData ) => {
207
- return Behavior ( {
208
- lifetimes : {
209
- created ( ) {
210
- const chainingPolyfillMetadata = new ChainingPolyfillMetadata ( initData )
211
- ; ( this as any ) . _$chainingPolyfill = chainingPolyfillMetadata
212
- const ctx = chainingPolyfillMetadata . generateBuilderContext ( this as any )
213
- initData . initFuncs . call ( this , [ ctx , chainingPolyfillMetadata ] )
214
- chainingPolyfillMetadata . setInitDone ( )
215
- } ,
216
- } ,
217
- methods : {
218
- traitBehavior < TOut extends { [ x : string ] : any } > (
219
- traitBehavior : TraitBehavior < any , TOut > ,
220
- ) : TOut | undefined {
221
- return getChainingPolyfillMetadata ( this ) . traitGroup . get ( traitBehavior )
222
- } ,
223
- } ,
224
- } )
225
- }
226
-
227
179
export class BaseBehaviorBuilder <
228
180
TPrevData extends DataList = Empty ,
229
181
TData extends DataList = Empty ,
@@ -234,6 +186,7 @@ export class BaseBehaviorBuilder<
234
186
TComponentExport = never ,
235
187
TExtraThisFields extends DataList = Empty ,
236
188
> {
189
+ private _$instanceMap : WeakMap < GeneralComponent , ChainingPolyfillMetadata > = new WeakMap ( )
237
190
private _$initData = {
238
191
lifetimes : { } ,
239
192
pageLifetimes : { } ,
@@ -244,43 +197,83 @@ export class BaseBehaviorBuilder<
244
197
data : { } as DataList ,
245
198
properties : { } as WechatMiniprogram . Behavior . PropertyOption ,
246
199
methods : { } as MethodList ,
247
- behaviors : [ generateChainingPolyfillBehavior ( this . _$initData ) ] ,
248
- lifetimes : {
200
+ behaviors : [ ] as WechatMiniprogram . Behavior . BehaviorIdentifier < any , any , any , any > [ ] ,
201
+ lifetimes : { } as Lifetimes ,
202
+ pageLifetimes : { } ,
203
+ observers : [ ] as { fields : string ; observer : GeneralFuncType } [ ] ,
204
+ relations : { } as { [ key : string ] : WechatMiniprogram . Component . RelationOption } ,
205
+ externalClasses : [ ] as string [ ] ,
206
+ export : undefined as undefined | ( ( ) => TComponentExport ) ,
207
+ options : undefined as undefined | WechatMiniprogram . Component . ComponentOptions ,
208
+ }
209
+
210
+ private getChainingPolyfillMetadata (
211
+ comp : GeneralComponent ,
212
+ ) : ChainingPolyfillMetadata | undefined {
213
+ return this . _$instanceMap . get ( comp )
214
+ }
215
+
216
+ private generateChainingPolyfillBehavior ( ) {
217
+ const instanceMap = this . _$instanceMap
218
+ const initData = this . _$initData
219
+ const getChainingPolyfillMetadata = this . getChainingPolyfillMetadata . bind ( this )
220
+ return Behavior ( {
221
+ lifetimes : {
222
+ created ( ) {
223
+ const chainingPolyfillMetadata = new ChainingPolyfillMetadata ( initData )
224
+ instanceMap . set ( this , chainingPolyfillMetadata )
225
+ const ctx = chainingPolyfillMetadata . generateBuilderContext ( this as any )
226
+ initData . initFuncs . call ( this , [ ctx , chainingPolyfillMetadata ] )
227
+ chainingPolyfillMetadata . setInitDone ( )
228
+ } ,
229
+ } ,
230
+ methods : {
231
+ traitBehavior < TOut extends { [ x : string ] : any } > (
232
+ traitBehavior : TraitBehavior < any , TOut > ,
233
+ ) : TOut | undefined {
234
+ return getChainingPolyfillMetadata ( this ) ! . traitGroup . get ( traitBehavior )
235
+ } ,
236
+ } ,
237
+ } )
238
+ }
239
+
240
+ constructor ( ) {
241
+ const getChainingPolyfillMetadata = this . getChainingPolyfillMetadata . bind ( this )
242
+
243
+ // add an early behavior
244
+ const beh = this . generateChainingPolyfillBehavior ( )
245
+ this . _$definition . behaviors . push ( beh )
246
+
247
+ // init lifetime handlers
248
+ this . _$definition . lifetimes = {
249
249
created ( ) {
250
- getChainingPolyfillMetadata ( this ) . callLifetime ( this , 'created' )
250
+ getChainingPolyfillMetadata ( this ) ! . callLifetime ( this , 'created' )
251
251
} ,
252
252
attached ( ) {
253
- getChainingPolyfillMetadata ( this ) . callLifetime ( this , 'attached' )
253
+ getChainingPolyfillMetadata ( this ) ! . callLifetime ( this , 'attached' )
254
254
} ,
255
255
moved ( ) {
256
- getChainingPolyfillMetadata ( this ) . callLifetime ( this , 'moved' )
256
+ getChainingPolyfillMetadata ( this ) ! . callLifetime ( this , 'moved' )
257
257
} ,
258
258
detached ( ) {
259
- getChainingPolyfillMetadata ( this ) . callLifetime ( this , 'detached' )
259
+ getChainingPolyfillMetadata ( this ) ! . callLifetime ( this , 'detached' )
260
260
} ,
261
261
ready ( ) {
262
- getChainingPolyfillMetadata ( this ) . callLifetime ( this , 'ready' )
262
+ getChainingPolyfillMetadata ( this ) ! . callLifetime ( this , 'ready' )
263
263
} ,
264
- } ,
265
- pageLifetimes : {
264
+ }
265
+ this . _$definition . pageLifetimes = {
266
266
show ( ) {
267
- getChainingPolyfillMetadata ( this ) . callPageLifetime ( this , 'show' )
267
+ getChainingPolyfillMetadata ( this ) ! . callPageLifetime ( this , 'show' )
268
268
} ,
269
269
hide ( ) {
270
- getChainingPolyfillMetadata ( this ) . callPageLifetime ( this , 'hide' )
270
+ getChainingPolyfillMetadata ( this ) ! . callPageLifetime ( this , 'hide' )
271
271
} ,
272
272
resize ( size : unknown ) {
273
- getChainingPolyfillMetadata ( this ) . callPageLifetime ( this , 'resize' , size )
273
+ getChainingPolyfillMetadata ( this ) ! . callPageLifetime ( this , 'resize' , size )
274
274
} ,
275
- } ,
276
- observers : [ ] as { fields : string ; observer : GeneralFuncType } [ ] ,
277
- relations : { } as { [ key : string ] : WechatMiniprogram . Component . RelationOption } ,
278
- externalClasses : [ ] as string [ ] ,
279
- export : undefined as undefined | ( ( ) => TComponentExport ) ,
280
- options : undefined as undefined | WechatMiniprogram . Component . ComponentOptions ,
281
- }
275
+ }
282
276
283
- constructor ( ) {
284
277
// make observer '**' automatically available
285
278
this . observer ( '**' , ( ) => { } )
286
279
}
@@ -307,7 +300,9 @@ export class BaseBehaviorBuilder<
307
300
TExtraThisFields
308
301
>
309
302
> {
310
- if ( ! this . _$definition . export ) this . _$definition . behaviors . unshift ( 'wx://component-export' )
303
+ if ( ! this . _$definition . export && isMiniProgramEnvironment ( ) ) {
304
+ this . _$definition . behaviors . unshift ( 'wx://component-export' )
305
+ }
311
306
this . _$definition . export = f as any
312
307
return this as any
313
308
}
@@ -398,18 +393,22 @@ export class BaseBehaviorBuilder<
398
393
paths : string | readonly string [ ] ,
399
394
func : ( this : Component < TData , TProperty , TMethod , TExtraThisFields > , ...args : any [ ] ) => any ,
400
395
) : ResolveBehaviorBuilder < this> {
396
+ const getChainingPolyfillMetadata = this . getChainingPolyfillMetadata . bind ( this )
401
397
const fields = typeof paths === 'string' ? paths : paths . join ( ', ' )
402
- const assistObservers = new FuncArr ( )
403
- assistObservers . add ( func )
404
- const observer = function (
405
- this : Component < TData , TProperty , TMethod , TExtraThisFields > ,
406
- ...args : any [ ]
407
- ) {
408
- assistObservers . call ( this , args )
409
- }
410
- this . _$definition . observers . push ( { fields, observer } )
411
398
const observers = this . _$initData . observers
412
- observers [ fields ] = assistObservers
399
+ if ( ! observers [ fields ] ) {
400
+ observers [ fields ] = new FuncArr ( )
401
+ const observer = function (
402
+ this : Component < TData , TProperty , TMethod , TExtraThisFields > ,
403
+ ...args : any [ ]
404
+ ) {
405
+ const assistObservers = getChainingPolyfillMetadata ( this ) ! . observers [ fields ]
406
+ assistObservers . call ( this , args )
407
+ }
408
+ this . _$definition . observers . push ( { fields, observer } )
409
+ }
410
+ const assistObservers = observers [ fields ]
411
+ assistObservers . add ( func )
413
412
return this as any
414
413
}
415
414
@@ -537,7 +536,7 @@ export class BaseBehaviorBuilder<
537
536
pageLifetimes : rawPageLifetimes ,
538
537
relations : rawRelations ,
539
538
externalClasses,
540
- export : exports ,
539
+ export : exported ,
541
540
} = definition
542
541
if ( behaviors ) {
543
542
this . _$definition . behaviors . push ( ...( behaviors as any [ ] ) )
@@ -584,7 +583,7 @@ export class BaseBehaviorBuilder<
584
583
Object . assign ( this . _$definition . relations , rawRelations )
585
584
}
586
585
if ( externalClasses ) this . externalClasses ( externalClasses )
587
- if ( exports ) this . export ( exports )
586
+ if ( exported ) this . export ( exported )
588
587
return this as any
589
588
}
590
589
}
0 commit comments