@@ -159,59 +159,62 @@ function log(message) {
159
159
console . log ( `[CodePush] ${ message } ` )
160
160
}
161
161
162
- // This ensures that notifyApplicationReadyInternal is only called once
163
- // in the lifetime of this module instance.
162
+ // This ensures that the native call to notifyApplicationReady
163
+ // only happens once in the lifetime of this module instance.
164
164
const notifyApplicationReady = ( ( ) => {
165
165
let notifyApplicationReadyPromise ;
166
166
return ( ) => {
167
167
if ( ! notifyApplicationReadyPromise ) {
168
- notifyApplicationReadyPromise = notifyApplicationReadyInternal ( ) ;
168
+ notifyApplicationReadyPromise = NativeCodePush . notifyApplicationReady ( ) ;
169
169
}
170
170
171
- return notifyApplicationReadyPromise ;
171
+ return notifyApplicationReadyPromise
172
+ . then ( ( ) => {
173
+ tryReportStatus ( ) ;
174
+ } ) ;
172
175
} ;
173
176
} ) ( ) ;
174
177
175
- async function notifyApplicationReadyInternal ( ) {
176
- await NativeCodePush . notifyApplicationReady ( ) ;
177
- tryReportStatus ( ) ;
178
- }
178
+ let tryReportStatus = ( function ( ) {
179
+ let resumeListener ;
180
+ return async function ( ) {
181
+ const statusReport = await NativeCodePush . getNewStatusReport ( ) ;
182
+ if ( statusReport ) {
183
+ const config = await getConfiguration ( ) ;
184
+ const previousLabelOrAppVersion = statusReport . previousLabelOrAppVersion ;
185
+ const previousDeploymentKey = statusReport . previousDeploymentKey || config . deploymentKey ;
186
+ try {
187
+ if ( statusReport . appVersion ) {
188
+ const sdk = getPromisifiedSdk ( requestFetchAdapter , config ) ;
189
+ await sdk . reportStatusDeploy ( /* deployedPackage */ null , /* status */ null , previousLabelOrAppVersion , previousDeploymentKey ) ;
190
+ } else {
191
+ config . deploymentKey = statusReport . package . deploymentKey ;
192
+ const sdk = getPromisifiedSdk ( requestFetchAdapter , config ) ;
193
+ await sdk . reportStatusDeploy ( statusReport . package , statusReport . status , previousLabelOrAppVersion , previousDeploymentKey ) ;
194
+ }
179
195
180
- async function tryReportStatus ( resumeListener ) {
181
- const statusReport = await NativeCodePush . getNewStatusReport ( ) ;
182
- if ( statusReport ) {
183
- const config = await getConfiguration ( ) ;
184
- const previousLabelOrAppVersion = statusReport . previousLabelOrAppVersion ;
185
- const previousDeploymentKey = statusReport . previousDeploymentKey || config . deploymentKey ;
186
- try {
187
- if ( statusReport . appVersion ) {
188
- const sdk = getPromisifiedSdk ( requestFetchAdapter , config ) ;
189
- await sdk . reportStatusDeploy ( /* deployedPackage */ null , /* status */ null , previousLabelOrAppVersion , previousDeploymentKey ) ;
190
- } else {
191
- config . deploymentKey = statusReport . package . deploymentKey ;
192
- const sdk = getPromisifiedSdk ( requestFetchAdapter , config ) ;
193
- await sdk . reportStatusDeploy ( statusReport . package , statusReport . status , previousLabelOrAppVersion , previousDeploymentKey ) ;
196
+ log ( `Reported status: ${ JSON . stringify ( statusReport ) } ` ) ;
197
+ NativeCodePush . recordStatusReported ( statusReport ) ;
198
+ resumeListener && AppState . removeEventListener ( "change" , resumeListener ) ;
199
+ resumeListener = null ;
200
+ } catch ( e ) {
201
+ log ( `Report status failed: ${ JSON . stringify ( statusReport ) } ` ) ;
202
+ NativeCodePush . saveStatusReportForRetry ( statusReport ) ;
203
+ // Try again when the app resumes
204
+ if ( ! resumeListener ) {
205
+ resumeListener = ( newState ) => {
206
+ newState === "active" && tryReportStatus ( resumeListener ) ;
207
+ } ;
208
+
209
+ AppState . addEventListener ( "change" , resumeListener ) ;
210
+ }
194
211
}
195
-
196
- log ( `Reported status: ${ JSON . stringify ( statusReport ) } ` ) ;
197
- NativeCodePush . recordStatusReported ( statusReport ) ;
212
+ } else {
198
213
resumeListener && AppState . removeEventListener ( "change" , resumeListener ) ;
199
- } catch ( e ) {
200
- log ( `Report status failed: ${ JSON . stringify ( statusReport ) } ` ) ;
201
- NativeCodePush . saveStatusReportForRetry ( statusReport ) ;
202
- // Try again when the app resumes
203
- if ( ! resumeListener ) {
204
- resumeListener = ( newState ) => {
205
- newState === "active" && tryReportStatus ( resumeListener ) ;
206
- } ;
207
-
208
- AppState . addEventListener ( "change" , resumeListener ) ;
209
- }
214
+ resumeListener = null ;
210
215
}
211
- } else {
212
- resumeListener && AppState . removeEventListener ( "change" , resumeListener ) ;
213
216
}
214
- }
217
+ } ) ( ) ;
215
218
216
219
function restartApp ( onlyIfUpdateIsPending = false ) {
217
220
NativeCodePush . restartApp ( onlyIfUpdateIsPending ) ;
0 commit comments