import type * as d from '../../declarations' ; import { appError , clearAppErrorModal , hmrWindow , logBuild , logDiagnostic , logReload , logWarn , emitBuildStatus , onBuildResults , } from '../client' ; import { OPEN_IN_EDITOR_URL } from '../dev-server-constants' ; export const initAppUpdate = ( win , config ) => { onBuildResults ( win , ( buildResults ) => { appUpdate ( win , config , buildResults ) ; } ) ; } ; const appUpdate = ( win , config , buildResults ) => { try { if ( buildResults . buildId === win [ 's-build-id' ] ) { return ; } win [ 's-build-id' ] = buildResults . buildId ; clearAppErrorModal ( { window : win } ) ; if ( buildResults . hasError ) { const editorId = Array . isArray ( config . editors ) && config . editors . length > 0 ? config . editors [ 0 ] . id : null ; const errorResults = appError ( { window : win , buildResults : buildResults , openInEditor : editorId ? ( data ) => { const qs = { file : data . file , line : data . line , column : data . column , editor : editorId , } ; const url = `${ OPEN_IN_EDITOR_URL }?${ Object . keys ( qs ) . map ( ( k ) => `${ k }=${ ( qs as any ) [ k ] }` ) . join ( '&' ) }` ; win . fetch ( url ) ; } : null , } ) ; errorResults . diagnostics . forEach ( logDiagnostic ) ; emitBuildStatus ( win , errorResults . status ) ; return ; } if ( win [ 's-initial-load' ] ) { appReset ( win , config , ( ) => { logReload ( `Initial load` ) ; win . location . reload ( true ) ; } ) ; return ; } if ( buildResults . hmr ) { appHmr ( win , buildResults . hmr ) ; } } catch ( e ) { console . error ( e ) ; } } ; const appHmr = ( win , hmr ) => { let shouldWindowReload = false ; if ( hmr . reloadStrategy === 'pageReload' ) { shouldWindowReload = true ; } if ( hmr . indexHtmlUpdated ) { logReload ( `Updated index.html` ) ; shouldWindowReload = true ; } if ( hmr . serviceWorkerUpdated ) { logReload ( `Updated Service Worker: sw.js` ) ; shouldWindowReload = true ; } if ( hmr . scriptsAdded && hmr . scriptsAdded . length > 0 ) { logReload ( `Added scripts: ${ hmr . scriptsAdded . join ( ', ' ) }` ) ; shouldWindowReload = true ; } if ( hmr . scriptsDeleted && hmr . scriptsDeleted . length > 0 ) { logReload ( `Deleted scripts: ${ hmr . scriptsDeleted . join ( ', ' ) }` ) ; shouldWindowReload = true ; } if ( hmr . excludeHmr && hmr . excludeHmr . length > 0 ) { logReload ( `Excluded From Hmr: ${ hmr . excludeHmr . join ( ', ' ) }` ) ; shouldWindowReload = true ; } if ( shouldWindowReload ) { win . location . reload ( true ) ; return ; } const results = hmrWindow ( { window : win , hmr : hmr } ) ; if ( results . updatedComponents . length > 0 ) { logBuild ( `Updated component${ results . updatedComponents . length > 1 ? 's' : '' }: ${ results . updatedComponents . join ( ', ' ) }` ) ; } if ( results . updatedInlineStyles . length > 0 ) { logBuild ( `Updated styles: ${ results . updatedInlineStyles . join ( ', ' ) }` ) ; } if ( results . updatedExternalStyles . length > 0 ) { logBuild ( `Updated stylesheets: ${ results . updatedExternalStyles . join ( ', ' ) }` ) ; } if ( results . updatedImages . length > 0 ) { logBuild ( `Updated images: ${ results . updatedImages . join ( ', ' ) }` ) ; } } ; export const appReset = ( win , config , cb ) => { win . history . replaceState ( { } , 'App' , config . basePath ) ; if ( ! win . navigator . serviceWorker || ! win . navigator . serviceWorker . getRegistration ) { cb ( ) ; } else { win . navigator . serviceWorker . getRegistration ( ) . then ( ( swRegistration ) => { if ( swRegistration ) { swRegistration . unregister ( ) . then ( ( hasUnregistered ) => { if ( hasUnregistered ) { logBuild ( `unregistered service worker` ) ; } cb ( ) ; } ) ; } else { cb ( ) ; } } ) . catch ( ( err ) => { logWarn ( 'Service Worker' , err ) ; cb ( ) ; } ) ; } } ;