import { createElement , ElementType , RenderObject , RenderSingleChild , RenderText , RenderView } from '@canvas-ui/core' ; import assert from 'assert' ; import type { ReactNode } from 'react' ; import type { FiberRoot } from 'react-reconciler' ; import { unstable_now as now , unstable_scheduleCallback as scheduleDeferredCallback , unstable_cancelCallback as cancelDeferredCallback , } from 'scheduler' ; import { Reconciler } from './reconciler' ; import { AnyProps , diffProps , setInitialProps , setText , UpdatePayload , updateProps } from './utils' ; const renderer = Reconciler < ElementType , AnyProps , RenderView , RenderObject , RenderText , unknown , RenderObject , null , UpdatePayload [ ] , unknown , number , - 1 > ( { supportsMutation : true , supportsPersistence : false , createInstance ( type , props , _rootContainer , _hostContext , _internalHandle ) { const instance = createElement ( type ) ; setInitialProps ( type , instance , props ) ; return instance ; } , createTextInstance ( _text , _rootContainer , _hostContext , _internalHandle ) { throw new Error ( 'Not implemented' ) ; } , appendInitialChild ( parentInstance , child ) { assert ( parentInstance instanceof RenderView || parentInstance instanceof RenderSingleChild ) ; removeFromParentIfNeeded ( child ) ; parentInstance . appendChild ( child ) ; } , finalizeInitialChildren ( _instance , _type , _props , _rootContainer , _hostContext ) { return false ; } , prepareUpdate : ( _instance , _type , oldProps , newProps , _rootContainer , _hostContext ) => { return diffProps ( oldProps , newProps ) ; } , shouldSetTextContent ( type , _props ) { return type === 'Text' ; } , getRootHostContext ( _rootContainer ) { return null ; } , getChildHostContext ( parentHostContext , _type , _rootContainer ) { return parentHostContext ; } , getPublicInstance ( instance ) { return instance ; } , prepareForCommit ( _containerInfo ) { return null ; } , resetAfterCommit ( _containerInfo ) { } , now , setTimeout : setTimeout , clearTimeout : clearTimeout , noTimeout : - 1 , scheduleDeferredCallback , cancelDeferredCallback , isPrimaryRenderer : false , appendChild ( parentInstance , child ) { assert ( parentInstance instanceof RenderSingleChild || parentInstance instanceof RenderView ) ; removeFromParentIfNeeded ( child ) ; parentInstance . appendChild ( child ) ; } , appendChildToContainer ( container , child ) { assert ( container instanceof RenderSingleChild || container instanceof RenderView ) ; removeFromParentIfNeeded ( child ) ; container . appendChild ( child ) ; } , insertBefore ( parentInstance , child , beforeChild ) { assert ( parentInstance instanceof RenderView ) ; removeFromParentIfNeeded ( child ) ; parentInstance . insertBefore ( child , beforeChild ) ; } , insertInContainerBefore ( container , child , beforeChild ) { assert ( container instanceof RenderView ) ; container . insertBefore ( child , beforeChild ) ; } , removeChild ( parentInstance , child ) { assert ( parentInstance instanceof RenderSingleChild || parentInstance instanceof RenderView ) ; parentInstance . removeChild ( child ) ; } , removeChildFromContainer ( container , child ) { assert ( container instanceof RenderView ) ; container . removeChild ( child ) ; } , resetTextContent ( instance ) { setText ( instance , '' ) ; } , commitTextUpdate ( _textInstance , _newText , _oldText ) { throw new Error ( 'Not implemented' ) ; } , commitMount ( _instance , _type , _newProps , _internalHandle ) { } , commitUpdate ( instance , updatePayload , _type , _prevProps , _nextProps , _internalHandle ) { updateProps ( instance , updatePayload ) ; } , supportsHydration : false , shouldDeprioritizeSubtree : ( ) => { return false ; } } ) ; renderer . injectIntoDevTools ( { bundleType : process . env . NODE_ENV === 'production' ? 0 : 1 , version : process . env . PKG_VERSION ?? '0.0.0' , rendererPackageName : '@canvas-ui/react' , findHostInstanceByFiber : renderer . findHostInstance , } as any ) ; const roots = new Map < RenderView , FiberRoot > ( ) ; export enum ReactRootTags { LegacyRoot = 0 , BlockingRoot = 1 , ConcurrentRoot = 2 } export function render ( element , container ) { let root = roots . get ( container ) ; if ( ! root ) { const newRoot = ( root = renderer . createContainer ( container , false , false ) ) ; roots . set ( container , newRoot ) ; } renderer . updateContainer ( element , root , null , ( ) => undefined ) ; return renderer . getPublicRootInstance ( root ) ; } export function unmountComponentAtNode ( container , callback ? ) { const root = roots . get ( container ) ; if ( root ) { renderer . updateContainer ( null , root , null , ( ) => { roots . delete ( container ) ; callback ?. ( container ) ; } ) ; return true ; } return false ; } const hasSymbol = typeof Symbol === 'function' && Symbol . for ; const REACT_PORTAL_TYPE = hasSymbol ? Symbol . for ( 'react.portal' ) : 0xeaca ; export function createPortal ( children , containerInfo ? , implementation ? , key ? ) { return { $$typeof : REACT_PORTAL_TYPE , key , children , containerInfo , implementation , } ; } function removeFromParentIfNeeded ( child ) { const { parent } = child ; if ( parent instanceof RenderView || parent instanceof RenderSingleChild ) { parent . removeChild ( child ) ; } }