@@ -2,15 +2,24 @@ import { useLayoutEffect, useRef, useState } from 'react';
2
2
import * as React from 'react' ;
3
3
import ReactDOM from 'react-dom' ;
4
4
import ReactDOMClient from 'react-dom/client' ;
5
- import { RouterContext } from './context' ;
6
5
import type {
7
6
ProviderParams ,
8
7
RenderFnParams ,
9
8
} from '@module-federation/bridge-shared' ;
10
- import { LoggerInstance , atLeastReact18 } from './utils' ;
11
9
import { ErrorBoundary } from 'react-error-boundary' ;
10
+ import { RouterContext } from './context' ;
11
+ import { LoggerInstance , atLeastReact18 } from './utils' ;
12
+ import { getInstance } from '@module-federation/runtime' ;
12
13
14
+ type RenderParams = RenderFnParams & {
15
+ [ key : string ] : unknown ;
16
+ } ;
17
+ type DestroyParams = {
18
+ moduleName : string ;
19
+ dom : HTMLElement ;
20
+ } ;
13
21
type RootType = HTMLElement | ReactDOMClient . Root ;
22
+
14
23
type ProviderFnParams < T > = {
15
24
rootComponent : React . ComponentType < T > ;
16
25
render ?: (
@@ -22,6 +31,9 @@ type ProviderFnParams<T> = {
22
31
export function createBridgeComponent < T > ( bridgeInfo : ProviderFnParams < T > ) {
23
32
return ( ) => {
24
33
const rootMap = new Map < any , RootType > ( ) ;
34
+ const instance = getInstance ( ) ;
35
+ LoggerInstance . log ( `createBridgeComponent remote instance` , instance ) ;
36
+
25
37
const RawComponent = ( info : { propsInfo : T ; appInfo : ProviderParams } ) => {
26
38
const { appInfo, propsInfo, ...restProps } = info ;
27
39
const { moduleName, memoryRoute, basename = '/' } = appInfo ;
@@ -37,7 +49,7 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
37
49
} ;
38
50
39
51
return {
40
- async render ( info : RenderFnParams & any ) {
52
+ async render ( info : RenderParams ) {
41
53
LoggerInstance . log ( `createBridgeComponent render Info` , info ) ;
42
54
const {
43
55
moduleName,
@@ -47,6 +59,10 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
47
59
fallback,
48
60
...propsInfo
49
61
} = info ;
62
+
63
+ const beforeBridgeRenderRes =
64
+ instance ?. bridgeHook ?. lifecycle ?. beforeBridgeRender ?. emit ( info ) || { } ;
65
+
50
66
const rootComponentWithErrorBoundary = (
51
67
// set ErrorBoundary for RawComponent rendering error, usually caused by user app rendering error
52
68
< ErrorBoundary FallbackComponent = { fallback } >
@@ -56,11 +72,13 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
56
72
basename,
57
73
memoryRoute,
58
74
} }
59
- propsInfo = { propsInfo }
75
+ propsInfo = {
76
+ { ...propsInfo , ...beforeBridgeRenderRes ?. extraProps } as T
77
+ }
60
78
/>
61
79
</ ErrorBoundary >
62
80
) ;
63
-
81
+ // call render function
64
82
if ( atLeastReact18 ( React ) ) {
65
83
if ( bridgeInfo ?. render ) {
66
84
// in case bridgeInfo?.render is an async function, resolve this to promise
@@ -77,18 +95,27 @@ export function createBridgeComponent<T>(bridgeInfo: ProviderFnParams<T>) {
77
95
const renderFn = bridgeInfo ?. render || ReactDOM . render ;
78
96
renderFn ?.( rootComponentWithErrorBoundary , info . dom ) ;
79
97
}
98
+
99
+ instance ?. bridgeHook ?. lifecycle ?. afterBridgeRender ?. emit ( info ) || { } ;
80
100
} ,
81
- async destroy ( info : { dom : HTMLElement } ) {
101
+
102
+ async destroy ( info : DestroyParams ) {
82
103
LoggerInstance . log ( `createBridgeComponent destroy Info` , {
83
104
dom : info . dom ,
84
105
} ) ;
106
+
107
+ instance ?. bridgeHook ?. lifecycle ?. beforeBridgeDestroy ?. emit ( info ) ;
108
+
109
+ // call destroy function
85
110
if ( atLeastReact18 ( React ) ) {
86
111
const root = rootMap . get ( info . dom ) ;
87
112
( root as ReactDOMClient . Root ) ?. unmount ( ) ;
88
113
rootMap . delete ( info . dom ) ;
89
114
} else {
90
115
ReactDOM . unmountComponentAtNode ( info . dom ) ;
91
116
}
117
+
118
+ instance ?. bridgeHook ?. lifecycle ?. afterBridgeDestroy ?. emit ( info ) ;
92
119
} ,
93
120
rawComponent : bridgeInfo . rootComponent ,
94
121
__BRIDGE_FN__ : ( _args : T ) => { } ,
0 commit comments