11import * as Updates from "expo-updates" ;
2+ // Metro namespace imports evaluate unrelated React Native getters, including unavailable native modules.
3+ import { AppState } from "react-native" ;
24
35import {
46 type AtomCommandResult ,
@@ -418,22 +420,19 @@ async function defaultFlushPendingWrites(): Promise<void> {
418420async function defaultIsSafeToRestartInBackground ( ) : Promise < boolean > {
419421 const { isForegroundHandoffActive } = await import ( "../../lib/foreground-handoff" ) ;
420422 if ( isForegroundHandoffActive ( ) ) return false ;
421- const { AppState } = await import ( "react-native" ) ;
422423 return AppState . currentState === "background" ;
423424}
424425
425426function defaultOnNextBackground ( apply : ( ) => void , includeCurrent : boolean ) : void {
426- void import ( "react-native" ) . then ( ( { AppState } ) => {
427- const subscription = AppState . addEventListener ( "change" , ( state ) => {
428- if ( state !== "background" ) return ;
429- subscription . remove ( ) ;
430- apply ( ) ;
431- } ) ;
432- if ( includeCurrent && AppState . currentState === "background" ) {
433- subscription . remove ( ) ;
434- apply ( ) ;
435- }
427+ const subscription = AppState . addEventListener ( "change" , ( state ) => {
428+ if ( state !== "background" ) return ;
429+ subscription . remove ( ) ;
430+ apply ( ) ;
436431 } ) ;
432+ if ( includeCurrent && AppState . currentState === "background" ) {
433+ subscription . remove ( ) ;
434+ apply ( ) ;
435+ }
437436}
438437
439438export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000 ;
@@ -443,25 +442,23 @@ export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
443442 * the timer running.
444443 */
445444function defaultOnForegroundStay ( apply : ( ) => void ) : void {
446- void import ( "react-native" ) . then ( ( { AppState } ) => {
447- let timer : ReturnType < typeof setTimeout > | undefined ;
448- const arm = ( ) => {
449- timer ??= setTimeout ( ( ) => {
450- subscription . remove ( ) ;
451- apply ( ) ;
452- } , DEFERRED_INSTALL_PROMPT_AFTER_MS ) ;
453- } ;
454- const disarm = ( ) => {
455- if ( timer === undefined ) return ;
456- clearTimeout ( timer ) ;
457- timer = undefined ;
458- } ;
459- const subscription = AppState . addEventListener ( "change" , ( state ) => {
460- if ( state === "active" ) arm ( ) ;
461- else if ( state === "background" ) disarm ( ) ;
462- } ) ;
463- if ( AppState . currentState === "active" ) arm ( ) ;
445+ let timer : ReturnType < typeof setTimeout > | undefined ;
446+ const arm = ( ) => {
447+ timer ??= setTimeout ( ( ) => {
448+ subscription . remove ( ) ;
449+ apply ( ) ;
450+ } , DEFERRED_INSTALL_PROMPT_AFTER_MS ) ;
451+ } ;
452+ const disarm = ( ) => {
453+ if ( timer === undefined ) return ;
454+ clearTimeout ( timer ) ;
455+ timer = undefined ;
456+ } ;
457+ const subscription = AppState . addEventListener ( "change" , ( state ) => {
458+ if ( state === "active" ) arm ( ) ;
459+ else if ( state === "background" ) disarm ( ) ;
464460 } ) ;
461+ if ( AppState . currentState === "active" ) arm ( ) ;
465462}
466463
467464const defaultAppUpdateEnvironment : AppUpdateEnvironment = {
@@ -528,22 +525,20 @@ export function createAppUpdateForegroundRecheck(
528525 return ( ) => {
529526 if ( started || ! isAppUpdateCheckAvailable ( client ) ) return ;
530527 started = true ;
531- void import ( "react-native" ) . then ( ( { AppState } ) => {
532- let backgroundedAtMs : number | null = null ;
533- AppState . addEventListener ( "change" , ( state ) => {
534- if ( state === "background" ) {
535- backgroundedAtMs = Date . now ( ) ;
536- return ;
537- }
538- if ( state !== "active" ) return ;
539- const shouldCheck = shouldRecheckAppUpdateOnForeground (
540- backgroundedAtMs ,
541- Date . now ( ) ,
542- deferral . pendingInstall ,
543- ) ;
544- backgroundedAtMs = null ;
545- if ( shouldCheck ) void runAppUpdateCheck ( { client, deferral } ) ;
546- } ) ;
528+ let backgroundedAtMs : number | null = null ;
529+ AppState . addEventListener ( "change" , ( state ) => {
530+ if ( state === "background" ) {
531+ backgroundedAtMs = Date . now ( ) ;
532+ return ;
533+ }
534+ if ( state !== "active" ) return ;
535+ const shouldCheck = shouldRecheckAppUpdateOnForeground (
536+ backgroundedAtMs ,
537+ Date . now ( ) ,
538+ deferral . pendingInstall ,
539+ ) ;
540+ backgroundedAtMs = null ;
541+ if ( shouldCheck ) void runAppUpdateCheck ( { client, deferral } ) ;
547542 } ) ;
548543 } ;
549544}
0 commit comments