Skip to content

Commit

Permalink
feat: add bridgeHook in runtime core
Browse files Browse the repository at this point in the history
  • Loading branch information
danpeen committed Nov 1, 2024
1 parent fc7b133 commit f673f69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion packages/bridge/bridge-react/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
dom: info.dom,
});

// call beforeBridgeDestroy hook
if (
bridgeInfo?.hooks &&
bridgeInfo?.hooks.beforeBridgeDestroy &&
Expand Down
20 changes: 14 additions & 6 deletions packages/bridge/bridge-react/src/remote/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import * as ReactRouterDOM from 'react-router-dom';
import type { ProviderParams } from '@module-federation/bridge-shared';
import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
import { ErrorBoundaryPropsWithComponent } from 'react-error-boundary';
import { registerBridgeLifeCycle } from '../lifecycle';
// import { registerBridgeLifeCycle } from '../lifecycle';
import { LoggerInstance, pathJoin } from '../utils';
import { getInstance } from '@module-federation/runtime';

export const getModuleName = (id: string) => {
// separate module name without detailed module path
Expand Down Expand Up @@ -55,7 +56,8 @@ const RemoteAppWrapper = forwardRef(function (
props: RemoteAppParams & RenderFnParams,
ref,
) {
const bridgeHook = registerBridgeLifeCycle();
const host = getInstance();
LoggerInstance.log(`RemoteAppWrapper host >>>`, host);
const RemoteApp = () => {
LoggerInstance.log(`RemoteAppWrapper RemoteApp props >>>`, { props });
const {
Expand Down Expand Up @@ -96,9 +98,12 @@ const RemoteAppWrapper = forwardRef(function (
renderProps,
);

if (bridgeHook && bridgeHook?.lifecycle?.beforeBridgeRender) {
if (
host?.bridgeHook &&
host?.bridgeHook?.lifecycle?.beforeBridgeRender
) {
const beforeBridgeRenderRes =
bridgeHook?.lifecycle?.beforeBridgeRender.emit({
host?.bridgeHook?.lifecycle?.beforeBridgeRender.emit({
...renderProps,
});
const extraProps =
Expand All @@ -124,8 +129,11 @@ const RemoteAppWrapper = forwardRef(function (
`createRemoteComponent LazyComponent destroy >>>`,
{ moduleName, basename, dom: renderDom.current },
);
if (bridgeHook && bridgeHook?.lifecycle?.afterBridgeDestroy) {
bridgeHook?.lifecycle?.afterBridgeDestroy.emit({
if (
host?.bridgeHook &&
host?.bridgeHook?.lifecycle?.afterBridgeDestroy
) {
host?.bridgeHook?.lifecycle?.afterBridgeDestroy.emit({
moduleName,
dom: renderDom.current,
basename,
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ export class FederationHost {
Promise<Response> | void | false
>(),
});
bridgeHook = new PluginSystem({
beforeBridgeRender: new SyncHook<[Record<string, any>], any>(),
afterBridgeRender: new SyncHook<[Record<string, any>], any>(),
beforeBridgeDestroy: new SyncHook<[Record<string, any>], any>(),
afterBridgeDestroy: new SyncHook<[Record<string, any>], any>(),
});

constructor(userOptions: UserOptions) {
// TODO: Validate the details of the options
Expand Down

0 comments on commit f673f69

Please sign in to comment.