From db4d808e41e4f5f23916399ed3dae87ae8a9c14e Mon Sep 17 00:00:00 2001 From: allenve Date: Fri, 6 Dec 2024 10:26:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20broadcast=20=E8=A7=A6=E5=8F=91onBroadcas?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../amis-core/src/actions/BroadcastAction.ts | 21 +++++++------- .../amis-core/src/utils/renderer-event.ts | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/packages/amis-core/src/actions/BroadcastAction.ts b/packages/amis-core/src/actions/BroadcastAction.ts index dc8201f3f3a..4f48cec12eb 100644 --- a/packages/amis-core/src/actions/BroadcastAction.ts +++ b/packages/amis-core/src/actions/BroadcastAction.ts @@ -1,6 +1,8 @@ -import {RendererProps} from '../factory'; import {createObject} from '../utils/helper'; -import {RendererEvent, dispatchGlobalEvent} from '../utils/renderer-event'; +import { + RendererEvent, + dispatchGlobalEventForRenderer +} from '../utils/renderer-event'; import { RendererAction, ListenerAction, @@ -39,15 +41,14 @@ export class BroadcastAction implements RendererAction { const eventName = action.args?.eventName || action.eventName!; - return await dispatchGlobalEvent(eventName, action.data); // 直接触发对应的动作 - // return await dispatchEvent( - // action.args?.eventName || action.eventName!, - // renderer, - // event.context.scoped, - // action.data, - // event - // ); + return await dispatchGlobalEventForRenderer( + eventName, + renderer, + event.context.scoped, + action.data, + event + ); } } diff --git a/packages/amis-core/src/utils/renderer-event.ts b/packages/amis-core/src/utils/renderer-event.ts index 62a54efc301..3a90b26faff 100644 --- a/packages/amis-core/src/utils/renderer-event.ts +++ b/packages/amis-core/src/utils/renderer-event.ts @@ -390,6 +390,34 @@ export async function dispatchEvent( return Promise.resolve(rendererEvent); } +export async function dispatchGlobalEventForRenderer( + eventName: string, + renderer: React.Component, + scoped: IScopedContext, + data: any, + broadcast: RendererEvent +) { + const from = renderer?.props.id || renderer?.props.name || ''; + debug( + 'event', + `dispatch \`${eventName}\` from 「${renderer?.props.type || 'unknown'}${ + from ? `#${from}` : '' + }」`, + data + ); + + renderer?.props?.env?.beforeDispatchEvent?.( + eventName, + renderer, + scoped, + data, + broadcast + ); + + renderer.props.onBroadcast?.(eventName, broadcast, data); + dispatchGlobalEvent(eventName, data); +} + export async function dispatchGlobalEvent(eventName: string, data: any) { if (!BroadcastChannel) { console.error('BroadcastChannel is not supported in your browser');