import ReactDOM from 'react-dom' ; import { noop } from '../common' ; type Defer = { promise : Promise < any > ; resolve ( value ? ) : void ; } ; const defer = { } ; defer . promise = new Promise ( resolve => { defer . resolve = resolve ; } ) ; let render = noop ; let hasMountedAtLeastOnce = false ; export default ( ) => defer . promise ; function getSlaveRuntime ( ) { const plugins = require ( 'umi/_runtimePlugin' ) ; const config = plugins . mergeConfig ( 'qiankun' ) || { } ; const { slave } = config ; return slave || config ; } export function genBootstrap ( promises , oldRender ) { return async ( ... args ) => { const slaveRuntime = getSlaveRuntime ( ) ; if ( slaveRuntime . bootstrap ) await slaveRuntime . bootstrap ( ... args ) ; render = ( ) => promises . then ( oldRender ) . catch ( e => { if ( process . env . NODE_ENV === 'development' ) { console . error ( 'Render failed' , e ) ; } } ) ; } ; } export function genMount ( ) { return async ( ... args ) => { defer . resolve ( ) ; const slaveRuntime = getSlaveRuntime ( ) ; if ( slaveRuntime . mount ) await slaveRuntime . mount ( ... args ) ; if ( hasMountedAtLeastOnce ) { render ( ) ; } hasMountedAtLeastOnce = true ; } ; } export function genUnmount ( mountElementId ) { return async ( ... args ) => { const container = document . getElementById ( mountElementId ) ; if ( container ) { ReactDOM . unmountComponentAtNode ( container ) ; } const slaveRuntime = getSlaveRuntime ( ) ; if ( slaveRuntime . unmount ) await slaveRuntime . unmount ( ... args ) ; } ; }