import { SHOW , HIDE , ERROR } from '../constants' ; import { getHistory } from '../storage' ; import router from '../router' ; import { emit as pageEmit } from '../pageLifeCycles' ; import { emit } from '../appLifeCycles' ; function initAppLifeCycles ( ) { if ( typeof document !== 'undefined' && typeof window !== 'undefined' ) { document . addEventListener ( 'visibilitychange' , function ( ) { const history = getHistory ( ) ; const currentPathName = history ? history . location . pathname : router . current . pathname ; if ( currentPathName === router . current . pathname ) { router . current . visibilityState = ! router . current . visibilityState ; if ( router . current . visibilityState ) { emit ( SHOW ) ; pageEmit ( SHOW , router . current . pathname ) ; } else { pageEmit ( HIDE , router . current . pathname ) ; emit ( HIDE ) ; } } } ) ; window . addEventListener ( 'error' , event => { emit ( ERROR , null , event . error ) ; } ) ; } } export default initAppLifeCycles ;