import { ReactElement , version } from 'react' ; import Reconciler , { FiberRoot , Reconciler as ReconcilerType } from 'react-reconciler' ; import ReactBabylonJSHostConfig , { Container } from './ReactBabylonJSHostConfig' ; export const roots = new Map < Container , FiberRoot > ( ) ; export type RendererOptions = { usePrimary ? : boolean ; } ; export interface ReconcilerInstance { /**
     * Replace previous root node with a new one or update props of the current root node.
     */ render : ( element : ReactElement | null , container : Container , callback : ( ) => void , parentComponent : any ) => any ; /**
     * Manually unmount the whole app.
     */ unmount : ( container : Container ) => void ; } const ReconcilerSecondary = Reconciler ( ReactBabylonJSHostConfig ) ; const ReconcilerPrimary = Reconciler ( { ... ReactBabylonJSHostConfig , isPrimaryRenderer : true , } ) ; /**
 * This is a work in progress in experimental state.
 */ export function createPortal ( children , containerInfo , key ? , usePrimary = false ) { const reconciler = usePrimary === true ? ReconcilerPrimary : ReconcilerSecondary ; let target = containerInfo ; if ( '__rb_createdInstance' in containerInfo ) { target = containerInfo . __rb_createdInstance ; } return reconciler . createPortal ( children , target , null , key ) ; } export function createReconciler ( rendererOptions ) { const reconciler = rendererOptions . usePrimary === true ? ReconcilerPrimary : ReconcilerSecondary ; function render ( element , container , callback , parentComponent ) { let root = roots . get ( container ) ; if ( ! root ) { root = ( reconciler as any ) . createContainer ( container , false , false ) as FiberRoot ; roots . set ( container , root ) ; reconciler . injectIntoDevTools ( { findFiberByHostInstance : ( reconciler as any ) . findFiberByHostInstance , bundleType : process . env . NODE_ENV === 'prod' ? 1 : 0 , version , rendererPackageName : 'react-babylonjs' , } ) ; } reconciler . updateContainer ( element , root , parentComponent , callback ) ; return reconciler . getPublicRootInstance ( root ) ; } function unmount ( container ) { const root = roots . get ( container ) ! ; reconciler . updateContainer ( null , root , null , ( ) => { } ) ; roots . delete ( container ) ; } return { render , unmount , } ; }