import type * as d from '../declarations' ; import { BUILD } from '@app-data' ; import { consoleError , loadModule , styles } from '@platform' ; import { CMP_FLAGS , HOST_FLAGS } from '@utils' ; import { proxyComponent } from './proxy-component' ; import { safeCall , scheduleUpdate } from './update-component' ; import { computeMode } from './mode' ; import { getScopeId , registerStyle } from './styles' ; import { PROXY_FLAGS } from './runtime-constants' ; import { createTime , uniqueTime } from './profile' ; export const initializeComponent = async ( elm , hostRef , cmpMeta , hmrVersionId ? , Cstr ? ) => { if ( ( BUILD . lazyLoad || BUILD . hydrateServerSide || BUILD . style ) && ( hostRef . $flags$ & HOST_FLAGS . hasInitializedComponent ) === 0 ) { if ( BUILD . lazyLoad || BUILD . hydrateClientSide ) { hostRef . $flags$ |= HOST_FLAGS . hasInitializedComponent ; Cstr = loadModule ( cmpMeta , hostRef , hmrVersionId ) ; if ( Cstr . then ) { const endLoad = uniqueTime ( `st:load:${ cmpMeta . $tagName$ }:${ hostRef . $modeName$ }` , `[Stencil] Load module for <${ cmpMeta . $tagName$ }>` ) ; Cstr = await Cstr ; endLoad ( ) ; } if ( ( BUILD . isDev || BUILD . isDebug ) && ! Cstr ) { throw new Error ( `Constructor for "${ cmpMeta . $tagName$ }#${ hostRef . $modeName$ }" was not found` ) ; } if ( BUILD . member && ! Cstr . isProxied ) { if ( BUILD . watchCallback ) { cmpMeta . $watchers$ = Cstr . watchers ; } proxyComponent ( Cstr , cmpMeta , PROXY_FLAGS . proxyState ) ; Cstr . isProxied = true ; } const endNewInstance = createTime ( 'createInstance' , cmpMeta . $tagName$ ) ; if ( BUILD . member ) { hostRef . $flags$ |= HOST_FLAGS . isConstructingInstance ; } try { new ( Cstr as any ) ( hostRef ) ; } catch ( e ) { consoleError ( e ) ; } if ( BUILD . member ) { hostRef . $flags$ &= ~ HOST_FLAGS . isConstructingInstance ; } if ( BUILD . watchCallback ) { hostRef . $flags$ |= HOST_FLAGS . isWatchReady ; } endNewInstance ( ) ; fireConnectedCallback ( hostRef . $lazyInstance$ ) ; } else { Cstr = elm . constructor as any ; hostRef . $flags$ |= HOST_FLAGS . hasInitializedComponent ; customElements . whenDefined ( cmpMeta . $tagName$ ) . then ( ( ) => ( hostRef . $flags$ |= HOST_FLAGS . isWatchReady ) ) ; } if ( BUILD . style && Cstr . style ) { let style = Cstr . style ; if ( BUILD . mode && typeof style !== 'string' ) { style = style [ ( hostRef . $modeName$ = computeMode ( elm ) ) ] ; if ( BUILD . hydrateServerSide && hostRef . $modeName$ ) { elm . setAttribute ( 's-mode' , hostRef . $modeName$ ) ; } } const scopeId = getScopeId ( cmpMeta , hostRef . $modeName$ ) ; if ( ! styles . has ( scopeId ) ) { const endRegisterStyles = createTime ( 'registerStyles' , cmpMeta . $tagName$ ) ; if ( ! BUILD . hydrateServerSide && BUILD . shadowDom && BUILD . shadowDomShim && cmpMeta . $flags$ & CMP_FLAGS . needsShadowDomShim ) { style = await import ( '../utils/shadow-css' ) . then ( ( m ) => m . scopeCss ( style , scopeId , false ) ) ; } registerStyle ( scopeId , style , ! ! ( cmpMeta . $flags$ & CMP_FLAGS . shadowDomEncapsulation ) ) ; endRegisterStyles ( ) ; } } } const ancestorComponent = hostRef . $ancestorComponent$ ; const schedule = ( ) => scheduleUpdate ( hostRef , true ) ; if ( BUILD . asyncLoading && ancestorComponent && ancestorComponent [ 's-rc' ] ) { ancestorComponent [ 's-rc' ] . push ( schedule ) ; } else { schedule ( ) ; } } ; export const fireConnectedCallback = ( instance ) => { if ( BUILD . lazyLoad && BUILD . connectedCallback ) { safeCall ( instance , 'connectedCallback' ) ; } } ;