Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit b038fb4

Browse files
committed
update JS
1 parent 94c0652 commit b038fb4

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

CodePush.js

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -159,59 +159,62 @@ function log(message) {
159159
console.log(`[CodePush] ${message}`)
160160
}
161161

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.
164164
const notifyApplicationReady = (() => {
165165
let notifyApplicationReadyPromise;
166166
return () => {
167167
if (!notifyApplicationReadyPromise) {
168-
notifyApplicationReadyPromise = notifyApplicationReadyInternal();
168+
notifyApplicationReadyPromise = NativeCodePush.notifyApplicationReady();
169169
}
170170

171-
return notifyApplicationReadyPromise;
171+
return notifyApplicationReadyPromise
172+
.then(() => {
173+
tryReportStatus();
174+
});
172175
};
173176
})();
174177

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+
}
179195

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+
}
194211
}
195-
196-
log(`Reported status: ${JSON.stringify(statusReport)}`);
197-
NativeCodePush.recordStatusReported(statusReport);
212+
} else {
198213
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;
210215
}
211-
} else {
212-
resumeListener && AppState.removeEventListener("change", resumeListener);
213216
}
214-
}
217+
})();
215218

216219
function restartApp(onlyIfUpdateIsPending = false) {
217220
NativeCodePush.restartApp(onlyIfUpdateIsPending);

0 commit comments

Comments
 (0)