Skip to content

Commit

Permalink
Merge pull request #177 from wechat-miniprogram/feat-adapter-write-in…
Browse files Browse the repository at this point in the history
…it-method

Adapter: write init method to method caller
  • Loading branch information
LastLeaf authored Jul 10, 2024
2 parents 398a0b1 + f9d916c commit 0f9698f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type MethodList = typeUtils.MethodList
type ChainingFilterType = typeUtils.ChainingFilterType
type ComponentMethod = typeUtils.ComponentMethod
type TaggedMethod<Fn extends ComponentMethod> = typeUtils.TaggedMethod<Fn>
type UnTaggedMethod<M extends TaggedMethod<any>> = typeUtils.UnTaggedMethod<M>

export class BaseBehaviorBuilder<
TPrevData extends DataList = Empty,
Expand Down Expand Up @@ -260,7 +261,12 @@ export class BaseBehaviorBuilder<
TPrevData,
TData,
TProperty,
TMethod,
TMethod &
(TExport extends void
? Empty
: {
[K in keyof TExport]: UnTaggedMethod<TExport[K]>
}),
TChainingFilter,
TPendingChainingFilter,
TComponentExport,
Expand Down Expand Up @@ -316,7 +322,7 @@ export class BaseBehaviorBuilder<
TExtraThisFields
>

return func.call(methodCaller, {
const exported = func.call(methodCaller, {
self: methodCaller,
data,
setData: (newData, callback) => {
Expand All @@ -335,6 +341,18 @@ export class BaseBehaviorBuilder<
method,
listener,
})

if (exported) {
const exportedKeys = Object.keys(exported)
for (let j = 0; j < exportedKeys.length; j += 1) {
const exportedKey = exportedKeys[j]!
const exportItem: unknown = exported[exportedKey]
if (glassEasel.Component.isTaggedMethod(exportItem)) {
;(methodCaller as { [key: string]: unknown })[exportedKey] = exportItem
}
}
}
return exported
})
return this as any
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MethodList = typeUtils.MethodList
type ChainingFilterType = typeUtils.ChainingFilterType
type ComponentMethod = typeUtils.ComponentMethod
type TaggedMethod<Fn extends ComponentMethod> = typeUtils.TaggedMethod<Fn>
type UnTaggedMethod<M extends TaggedMethod<any>> = typeUtils.UnTaggedMethod<M>
type ChainingFilterFunc<
TAddedFields extends { [key: string]: any },
TRemovedFields extends string = never,
Expand Down Expand Up @@ -217,7 +218,12 @@ export class BehaviorBuilder<
TPrevData,
TData,
TProperty,
TMethod,
TMethod &
(TExport extends void
? Empty
: {
[K in keyof TExport]: UnTaggedMethod<TExport[K]>
}),
TChainingFilter,
TPendingChainingFilter,
TComponentExport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MethodList = typeUtils.MethodList
type ChainingFilterType = typeUtils.ChainingFilterType
type ComponentMethod = typeUtils.ComponentMethod
type TaggedMethod<Fn extends ComponentMethod> = typeUtils.TaggedMethod<Fn>
type UnTaggedMethod<M extends TaggedMethod<any>> = typeUtils.UnTaggedMethod<M>

/**
* A direct way to create a component
Expand Down Expand Up @@ -241,7 +242,12 @@ export class ComponentBuilder<
TPrevData,
TData,
TProperty,
TMethod,
TMethod &
(TExport extends void
? Empty
: {
[K in keyof TExport]: UnTaggedMethod<TExport[K]>
}),
TChainingFilter,
TPendingChainingFilter,
TComponentExport,
Expand Down
9 changes: 4 additions & 5 deletions glass-easel-miniprogram-adapter/tests/space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,15 +855,14 @@ describe('define', () => {
// eslint-disable-next-line arrow-body-style
const parentType = codeSpace.componentEnv('path/to/comp', ({ Component }) => {
return Component()
.methods({
parentFn() {
return 456
},
})
.lifetime('attached', function () {
expect(this.selectComponent('#c', childType)!.childFn()).toBe(123)
callOrder.push(2)
})
.init(({ method }) => {
const parentFn = method(() => 456)
return { parentFn }
})
.register()
})

Expand Down
5 changes: 2 additions & 3 deletions glass-easel/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,7 @@ export class Component<
return taggedMethod
}

/** @internal */
static _$isTaggedMethod(func: unknown): func is TaggedMethod<ComponentMethod> {
static isTaggedMethod(func: unknown): func is TaggedMethod<ComponentMethod> {
return typeof func === 'function' && !!(func as unknown as { [tag: symbol]: true })[METHOD_TAG]
}

Expand Down Expand Up @@ -815,7 +814,7 @@ export class Component<
for (let j = 0; j < exportedKeys.length; j += 1) {
const exportedKey = exportedKeys[j]!
const exportItem: unknown = exported[exportedKey]
if (Component._$isTaggedMethod(exportItem)) {
if (Component.isTaggedMethod(exportItem)) {
if (cowMethodMap) {
cowMethodMap = false
comp._$methodMap = Object.create(comp._$methodMap) as MethodList
Expand Down

0 comments on commit 0f9698f

Please sign in to comment.