import { AsyncFrameworkFn , FrameworkFn , Component , AsyncComponent , Props , Slots } from './types' ; export { Framework , Props , Slots } from './types' ; const findById = ( id ) => document . getElementById ( id ) || console . error ( `Missing #${ id }, could not mount island.` ) ; export function hydrateNow ( framework , component , id , props , slots ) { const el = findById ( id ) ; if ( el ) framework ( component , id , el , props , slots ) ; } async function resolveAndHydrate ( frameworkFn , componentFn , id , props , slots ) { const [ framework , component ] = await Promise . all ( [ frameworkFn ( ) , componentFn ( ) ] ) ; hydrateNow ( framework , component , id , props , slots ) ; } export function hydrateWhenIdle ( framework , component , id , props , slots ) { const whenIdle = window . requestIdleCallback || setTimeout ; const cancelIdle = window . cancelIdleCallback || clearTimeout ; const idleId = whenIdle ( ( ) => resolveAndHydrate ( framework , component , id , props , slots ) ) ; if ( import . meta . env . DISPOSE_ISLANDS ) onDispose ( id , ( ) => cancelIdle ( idleId ) ) ; } export function hydrateOnMediaQuery ( framework , component , id , props , slots ) { const mediaQuery = matchMedia ( props . _mediaQuery as string ) ; delete props . _mediaQuery ; const onChange = ( fn = null ) => mediaQuery . onchange = fn ; const hydrate = ( ) => { onChange ( ) ; resolveAndHydrate ( framework , component , id , props , slots ) ; } ; mediaQuery . matches ? hydrate ( ) : onChange ( hydrate ) ; if ( import . meta . env . DISPOSE_ISLANDS ) onDispose ( id , onChange ) ; } export function hydrateWhenVisible ( framework , component , id , props , slots ) { const el = findById ( id ) ; if ( el ) { const observer = new IntersectionObserver ( ( [ { isIntersecting } ] ) => { if ( isIntersecting ) { stopObserver ( ) ; resolveAndHydrate ( framework , component , id , props , slots ) ; } } ) ; const stopObserver = ( ) => observer . disconnect ( ) ; Array . from ( el . children ) . forEach ( child => observer . observe ( child ) ) ; if ( import . meta . env . DISPOSE_ISLANDS ) onDispose ( id , stopObserver ) ; } } export const onDispose = ( id , fn ) => ( window as any ) . __ILE_DISPOSE__ ?. set ( id , fn ) ;