@@ -2,15 +2,24 @@ import { useLayoutEffect, useRef, useState } from 'react';
22import * as React from 'react' ;
33import ReactDOM from 'react-dom' ;
44import ReactDOMClient from 'react-dom/client' ;
5- import { RouterContext } from './context' ;
65import type {
76 ProviderParams ,
87 RenderFnParams ,
98} from '@module-federation/bridge-shared' ;
10- import { LoggerInstance , atLeastReact18 } from './utils' ;
119import { ErrorBoundary } from 'react-error-boundary' ;
10+ import { RouterContext } from './context' ;
11+ import { LoggerInstance , atLeastReact18 } from './utils' ;
12+ import { getInstance } from '@module-federation/runtime' ;
1213
14+ type RenderParams = RenderFnParams & {
15+ [ key : string ] : unknown ;
16+ } ;
17+ type DestroyParams = {
18+ moduleName : string ;
19+ dom : HTMLElement ;
20+ } ;
1321type RootType = HTMLElement | ReactDOMClient . Root ;
22+
1423type ProviderFnParams < T > = {
1524 rootComponent : React . ComponentType < T > ;
1625 render ?: (
@@ -22,6 +31,9 @@ type ProviderFnParams<T> = {
2231export function createBridgeComponent < T > ( bridgeInfo : ProviderFnParams < T > ) {
2332 return ( ) => {
2433 const rootMap = new Map < any , RootType > ( ) ;
34+ const instance = getInstance ( ) ;
35+ LoggerInstance . log ( `createBridgeComponent remote instance` , instance ) ;
36+
2537 const RawComponent = ( info : { propsInfo : T ; appInfo : ProviderParams } ) => {
2638 const { appInfo, propsInfo, ...restProps } = info ;
2739 const { moduleName, memoryRoute, basename = '/' } = appInfo ;
@@ -37,7 +49,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
3749 } ;
3850
3951 return {
40- async render ( info : RenderFnParams & any ) {
52+ async render ( info : RenderParams ) {
4153 LoggerInstance . log ( `createBridgeComponent render Info` , info ) ;
4254 const {
4355 moduleName,
@@ -47,6 +59,10 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
4759 fallback,
4860 ...propsInfo
4961 } = info ;
62+
63+ const beforeBridgeRenderRes =
64+ instance ?. bridgeHook ?. lifecycle ?. beforeBridgeRender ?. emit ( info ) || { } ;
65+
5066 const rootComponentWithErrorBoundary = (
5167 // set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
5268 < ErrorBoundary FallbackComponent = { fallback } >
@@ -56,11 +72,13 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
5672 basename,
5773 memoryRoute,
5874 } }
59- propsInfo = { propsInfo }
75+ propsInfo = {
76+ { ...propsInfo , ...beforeBridgeRenderRes ?. extraProps } as T
77+ }
6078 />
6179 </ ErrorBoundary >
6280 ) ;
63-
81+ // call render function
6482 if ( atLeastReact18 ( React ) ) {
6583 if ( bridgeInfo ?. render ) {
6684 // in case bridgeInfo?.render is an async function, resolve this to promise
@@ -77,18 +95,27 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
7795 const renderFn = bridgeInfo ?. render || ReactDOM . render ;
7896 renderFn ?.( rootComponentWithErrorBoundary , info . dom ) ;
7997 }
98+
99+ instance ?. bridgeHook ?. lifecycle ?. afterBridgeRender ?. emit ( info ) || { } ;
80100 } ,
81- async destroy ( info : { dom : HTMLElement } ) {
101+
102+ async destroy ( info : DestroyParams ) {
82103 LoggerInstance . log ( `createBridgeComponent destroy Info` , {
83104 dom : info . dom ,
84105 } ) ;
106+
107+ instance ?. bridgeHook ?. lifecycle ?. beforeBridgeDestroy ?. emit ( info ) ;
108+
109+ // call destroy function
85110 if ( atLeastReact18 ( React ) ) {
86111 const root = rootMap . get ( info . dom ) ;
87112 ( root as ReactDOMClient . Root ) ?. unmount ( ) ;
88113 rootMap . delete ( info . dom ) ;
89114 } else {
90115 ReactDOM . unmountComponentAtNode ( info . dom ) ;
91116 }
117+
118+ instance ?. bridgeHook ?. lifecycle ?. afterBridgeDestroy ?. emit ( info ) ;
92119 } ,
93120 rawComponent : bridgeInfo . rootComponent ,
94121 __BRIDGE_FN__ : ( _args : T ) => { } ,
0 commit comments