From f9d916cf3e0644066addc6d886f8bfbe034db499 Mon Sep 17 00:00:00 2001 From: LastLeaf Date: Wed, 10 Jul 2024 14:50:12 +0800 Subject: [PATCH] feat(adapter): write method defined in init to method caller --- .../src/builder/base_behavior_builder.ts | 14 +++++++++++++- .../tests/space.test.ts | 9 ++++----- glass-easel/src/component.ts | 5 ++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/glass-easel-miniprogram-adapter/src/builder/base_behavior_builder.ts b/glass-easel-miniprogram-adapter/src/builder/base_behavior_builder.ts index cb43e83..f5496cf 100644 --- a/glass-easel-miniprogram-adapter/src/builder/base_behavior_builder.ts +++ b/glass-easel-miniprogram-adapter/src/builder/base_behavior_builder.ts @@ -322,7 +322,7 @@ export class BaseBehaviorBuilder< TExtraThisFields > - return func.call(methodCaller, { + const exported = func.call(methodCaller, { self: methodCaller, data, setData: (newData, callback) => { @@ -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 } diff --git a/glass-easel-miniprogram-adapter/tests/space.test.ts b/glass-easel-miniprogram-adapter/tests/space.test.ts index 808a3cf..b96a60f 100644 --- a/glass-easel-miniprogram-adapter/tests/space.test.ts +++ b/glass-easel-miniprogram-adapter/tests/space.test.ts @@ -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() }) diff --git a/glass-easel/src/component.ts b/glass-easel/src/component.ts index 4d9c110..65f25ad 100644 --- a/glass-easel/src/component.ts +++ b/glass-easel/src/component.ts @@ -502,8 +502,7 @@ export class Component< return taggedMethod } - /** @internal */ - static _$isTaggedMethod(func: unknown): func is TaggedMethod { + static isTaggedMethod(func: unknown): func is TaggedMethod { return typeof func === 'function' && !!(func as unknown as { [tag: symbol]: true })[METHOD_TAG] } @@ -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