Skip to content

Commit

Permalink
feat(adapter): write method defined in init to method caller
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Jul 10, 2024
1 parent 35f156d commit f9d916c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class BaseBehaviorBuilder<
TExtraThisFields
>

return func.call(methodCaller, {
const exported = func.call(methodCaller, {
self: methodCaller,
data,
setData: (newData, callback) => {
Expand All @@ -341,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
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 f9d916c

Please sign in to comment.