const SW_SUPPORTED = FLAG_PRODUCTION && navigator . serviceWorker !== undefined && window . isSecureContext && ( document . location as any ) . hostname === HOSTNAME_PRODUCTION ; interface WorkerListener { ( isPending ) : void ; } let worker = null ; let listeners = [ ] ; let pending = false ; export function addListener ( cb ) { listeners . push ( cb ) ; } export function removeListener ( cb ) { listeners . splice ( listeners . indexOf ( cb ) , 1 ) ; } export function isPending ( ) { return pending ; } export function update ( ) { if ( worker !== null ) { worker . postMessage ( { action : 'skipWaiting' } ) ; } } function setPending ( state ) { pending = state ; listeners . forEach ( ( cb ) => cb ( state ) ) ; } function trackInstallation ( workerInstalling ) { workerInstalling . addEventListener ( 'statechange' , ( ) => { if ( workerInstalling . state == 'installed' ) { worker = workerInstalling ; setPending ( true ) ; } } ) ; } if ( SW_SUPPORTED ) { window . addEventListener ( 'beforeinstallprompt' , ( e ) => { e . preventDefault ( ) ; } ) ; if ( navigator . serviceWorker !== undefined ) { window . addEventListener ( 'load' , ( ) => { navigator . serviceWorker . register ( '/sw.js' ) . then ( ( registration ) => { if ( navigator . serviceWorker . controller === null ) { return ; } if ( registration . waiting ) { worker = registration . waiting ; setPending ( true ) ; return ; } if ( registration . installing ) { trackInstallation ( registration . installing ) ; return ; } registration . addEventListener ( 'updatefound' , ( ) => { if ( registration . installing !== null ) { trackInstallation ( registration . installing ) ; } } ) ; } ) ; } ) ; let refreshing = false ; navigator . serviceWorker . addEventListener ( 'controllerchange' , ( ) => { if ( ! refreshing ) { window . location . reload ( ) ; refreshing = true ; } } ) ; } }