import type { Action, Store } from './store'; type ActionCreator = (payload: P) => Action; interface BridgeStore { store: Store; actions: { [key: string]: ActionCreator }; } interface StoreAction { key: string; store: Store; actionCreator: ActionCreator; } const actionMap: { [key: string]: StoreAction[] } = {}; export const registerBridgeStore = ( ...stores: BridgeStore[] ): void => { for (const store of stores) { for (const [key, actionCreator] of Object.entries(store.actions)) { if (actionMap[key] === undefined) { actionMap[key] = []; } actionMap[key].push({ key, store: store.store as unknown as Store, actionCreator: actionCreator as unknown as ActionCreator< unknown, object >, }); } } }; export const onStoreAction = async ( event: string, payload: T ): Promise => { if (actionMap[event] !== undefined && actionMap[event].length > 0) { const actions = actionMap[event]; for (const action of actions) { await action.store.dispatch(action.actionCreator(payload)); } } };