From 2846f9eb2a8655175a024b16eaba22b522e88603 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 27 May 2026 11:20:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20app.emit=20=E5=9C=A8=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E9=85=8D=E7=BD=AE=E4=BA=8B=E4=BB=B6=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E5=BA=94=E7=9F=AD=E8=B7=AF=20super.emit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去掉 eventHelper.emit 前的 return,避免节点配置 events 后 app.on 注册的监听器被吞掉,并补充回归测试。 --- packages/core/src/App.ts | 2 +- packages/core/tests/App.spec.ts | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 3e9ca3b2..50bfbbdf 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -244,7 +244,7 @@ class App extends EventEmitter { node.data?.id && node.eventKeys.has(`${String(name)}_${node.data.id}`) ) { - return this.eventHelper.emit(node.eventKeys.get(`${String(name)}_${node.data.id}`)!, node, ...otherArgs); + this.eventHelper.emit(node.eventKeys.get(`${String(name)}_${node.data.id}`)!, node, ...otherArgs); } return super.emit(name, ...args); } diff --git a/packages/core/tests/App.spec.ts b/packages/core/tests/App.spec.ts index 82df2595..14d61819 100644 --- a/packages/core/tests/App.spec.ts +++ b/packages/core/tests/App.spec.ts @@ -431,6 +431,58 @@ describe('App 配置/方法/组件注册', () => { expect(typeof result).toBe('boolean'); }); + // 回归用例:节点配置了 events 时,eventHelper 派发不能短路掉 super.emit, + // 即 app.on(name, cb) 注册的回调依然要被触发。 + test('emit: 节点已绑定 events 时,app.on 注册的监听器仍然会被调用', () => { + const app = new App({ + config: { + type: NodeType.ROOT, + id: 'app', + items: [ + { + type: NodeType.PAGE, + id: 'p1', + items: [{ id: 'btn', type: 'button', events: [{ name: 'click', actions: [] }] }], + }, + ], + } as any, + }); + const node = app.getNode('btn')!; + const cb = vi.fn(); + app.on('click', cb); + + const result = app.emit('click', node, 'arg1'); + + expect(cb).toHaveBeenCalledTimes(1); + expect(cb).toHaveBeenCalledWith(node, 'arg1'); + // EventEmitter.emit 在有 listener 时返回 true + expect(result).toBe(true); + }); + + test('emit: 未命中节点 eventKeys 时,app.on 注册的监听器正常被调用', () => { + const app = new App({ + config: { + type: NodeType.ROOT, + id: 'app', + items: [ + { + type: NodeType.PAGE, + id: 'p1', + items: [{ id: 'btn', type: 'button' }], + }, + ], + } as any, + }); + const node = app.getNode('btn')!; + const cb = vi.fn(); + app.on('click', cb); + + app.emit('click', node, 'arg1'); + + expect(cb).toHaveBeenCalledTimes(1); + expect(cb).toHaveBeenCalledWith(node, 'arg1'); + }); + test('destroy 清理所有资源', () => { const app = new App({ config: {