Skip to content

Commit

Permalink
fix: broadcast 触发onBroadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
allenve committed Dec 6, 2024
1 parent 69b2804 commit db4d808
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/amis-core/src/actions/BroadcastAction.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
);
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/amis-core/src/utils/renderer-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,34 @@ export async function dispatchEvent(
return Promise.resolve(rendererEvent);
}

export async function dispatchGlobalEventForRenderer(
eventName: string,
renderer: React.Component<RendererProps>,
scoped: IScopedContext,
data: any,
broadcast: RendererEvent<any>
) {
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');
Expand Down

0 comments on commit db4d808

Please sign in to comment.