@@ -466,20 +466,6 @@ function isAppUpdateUnavailableError(error: unknown): boolean {
466466 return typeof code === "string" && UPDATE_CHECK_UNAVAILABLE_ERROR_CODES . has ( code ) ;
467467}
468468
469- export function createAppUpdateLaunchCheck (
470- client : AppUpdateClient = Updates ,
471- ) : ( ) => Promise < void > | undefined {
472- let started = false ;
473-
474- return ( ) => {
475- if ( started || ! isAppUpdateCheckAvailable ( client ) ) return undefined ;
476- started = true ;
477- return runAppUpdateCheck ( { client } ) ;
478- } ;
479- }
480-
481- export const checkForAppUpdateOnLaunch = createAppUpdateLaunchCheck ( ) ;
482-
483469export const FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS = 15 * 60 * 1000 ;
484470
485471export function shouldRecheckAppUpdateOnForeground (
@@ -494,31 +480,30 @@ export function shouldRecheckAppUpdateOnForeground(
494480 ) ;
495481}
496482
497- export function createAppUpdateForegroundRecheck (
483+ /**
484+ * App-lifetime update checks: one at launch, then again when the app returns
485+ * from a long stay in the background.
486+ */
487+ export function installAppUpdateChecks (
498488 client : AppUpdateClient = Updates ,
499489 deferral : AppUpdateDeferral = appUpdateDeferral ,
500490) : ( ) => void {
501- let started = false ;
502-
503- return ( ) => {
504- if ( started || ! isAppUpdateCheckAvailable ( client ) ) return ;
505- started = true ;
506- let backgroundedAtMs : number | null = null ;
507- AppState . addEventListener ( "change" , ( state ) => {
508- if ( state === "background" ) {
509- backgroundedAtMs = Date . now ( ) ;
510- return ;
511- }
512- if ( state !== "active" ) return ;
513- const shouldCheck = shouldRecheckAppUpdateOnForeground (
514- backgroundedAtMs ,
515- Date . now ( ) ,
516- deferral . pendingInstall ,
517- ) ;
518- backgroundedAtMs = null ;
519- if ( shouldCheck ) void runAppUpdateCheck ( { client, deferral } ) ;
520- } ) ;
521- } ;
491+ if ( ! isAppUpdateCheckAvailable ( client ) ) return ( ) => undefined ;
492+ void runAppUpdateCheck ( { client, deferral } ) ;
493+ let backgroundedAtMs : number | null = null ;
494+ const subscription = AppState . addEventListener ( "change" , ( state ) => {
495+ if ( state === "background" ) {
496+ backgroundedAtMs = Date . now ( ) ;
497+ return ;
498+ }
499+ if ( state !== "active" ) return ;
500+ const shouldCheck = shouldRecheckAppUpdateOnForeground (
501+ backgroundedAtMs ,
502+ Date . now ( ) ,
503+ deferral . pendingInstall ,
504+ ) ;
505+ backgroundedAtMs = null ;
506+ if ( shouldCheck ) void runAppUpdateCheck ( { client, deferral } ) ;
507+ } ) ;
508+ return ( ) => subscription . remove ( ) ;
522509}
523-
524- export const startAppUpdateForegroundRecheck = createAppUpdateForegroundRecheck ( ) ;
0 commit comments