Skip to content

Commit db5d056

Browse files
committed
refactor: fail fast when CodePush native bridge is unavailable
1 parent bcb635f commit db5d056

1 file changed

Lines changed: 27 additions & 61 deletions

File tree

src/CodePush.js

Lines changed: 27 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ function getNativeCodePush() {
2323
return NativeCodePush;
2424
}
2525

26-
function getPackageMixins() {
27-
getNativeCodePush();
28-
return PackageMixins;
26+
function requireNativeCodePush(callerName) {
27+
const nativeCodePush = getNativeCodePush();
28+
if (!nativeCodePush) {
29+
throw new Error(`The CodePush native module isn't available during ${callerName}.`);
30+
}
31+
32+
return nativeCodePush;
2933
}
3034

31-
function logMissingNativeModule() {
32-
log("The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly.");
35+
function requirePackageMixins(callerName) {
36+
const nativeCodePush = requireNativeCodePush(callerName);
37+
if (!PackageMixins) {
38+
PackageMixins = require("./package-mixins")(nativeCodePush);
39+
}
40+
41+
return PackageMixins;
3342
}
3443

3544
/**
@@ -90,7 +99,7 @@ async function checkForUpdate(handleBinaryVersionMismatchCallback = null) {
9099
* for their specific deployment and version and which are actually
91100
* different from the CodePush update they have already installed.
92101
*/
93-
const nativeConfig = await getConfiguration();
102+
const nativeConfig = await getConfiguration("checkForUpdate");
94103

95104
// Use dynamically overridden getCurrentPackage() during tests.
96105
const localPackage = await module.exports.getCurrentPackage();
@@ -240,13 +249,8 @@ async function checkForUpdate(handleBinaryVersionMismatchCallback = null) {
240249

241250
return null;
242251
} else {
243-
const nativeCodePush = getNativeCodePush();
244-
const packageMixins = getPackageMixins();
245-
if (!nativeCodePush || !packageMixins) {
246-
logMissingNativeModule();
247-
return null;
248-
}
249-
252+
const nativeCodePush = requireNativeCodePush("checkForUpdate");
253+
const packageMixins = requirePackageMixins("checkForUpdate");
250254
const remotePackage = { ...update, ...packageMixins.remote() };
251255
remotePackage.failedInstall = await nativeCodePush.isFailedUpdate(remotePackage.packageHash);
252256
return remotePackage;
@@ -282,17 +286,13 @@ function mapToRemotePackageMetadata(updateInfo) {
282286

283287
const getConfiguration = (() => {
284288
let config;
285-
return async function getConfiguration() {
289+
return async function getConfiguration(callerName = "getConfiguration") {
286290
if (config) {
287291
return config;
288292
} else if (testConfig) {
289293
return testConfig;
290294
} else {
291-
const nativeCodePush = getNativeCodePush();
292-
if (!nativeCodePush) {
293-
throw new Error("The CodePush native module isn't available yet.");
294-
}
295-
295+
const nativeCodePush = requireNativeCodePush(callerName);
296296
config = await nativeCodePush.getConfiguration();
297297
return config;
298298
}
@@ -304,12 +304,8 @@ async function getCurrentPackage() {
304304
}
305305

306306
async function getUpdateMetadata(updateState) {
307-
const nativeCodePush = getNativeCodePush();
308-
const packageMixins = getPackageMixins();
309-
if (!nativeCodePush || !packageMixins) {
310-
return null;
311-
}
312-
307+
const nativeCodePush = requireNativeCodePush("getUpdateMetadata");
308+
const packageMixins = requirePackageMixins("getUpdateMetadata");
313309
let updateMetadata = await nativeCodePush.getUpdateMetadata(updateState || CodePush.UpdateState.RUNNING);
314310
if (updateMetadata) {
315311
updateMetadata = { ...packageMixins.local, ...updateMetadata };
@@ -333,12 +329,7 @@ const notifyApplicationReady = (() => {
333329
})();
334330

335331
async function notifyApplicationReadyInternal() {
336-
const nativeCodePush = getNativeCodePush();
337-
if (!nativeCodePush) {
338-
logMissingNativeModule();
339-
return null;
340-
}
341-
332+
const nativeCodePush = requireNativeCodePush("notifyApplicationReady");
342333
await nativeCodePush.notifyApplicationReady();
343334
const statusReport = await nativeCodePush.getNewStatusReport();
344335
statusReport && tryReportStatus(statusReport); // Don't wait for this to complete.
@@ -349,7 +340,6 @@ async function notifyApplicationReadyInternal() {
349340
async function tryReportStatus(statusReport, retryOnAppResume) {
350341
const nativeCodePush = getNativeCodePush();
351342
if (!nativeCodePush) {
352-
logMissingNativeModule();
353343
retryOnAppResume && retryOnAppResume.remove();
354344
return;
355345
}
@@ -417,11 +407,7 @@ async function shouldUpdateBeIgnored(remotePackage, syncOptions) {
417407
return true;
418408
}
419409

420-
const nativeCodePush = getNativeCodePush();
421-
if (!nativeCodePush) {
422-
return true;
423-
}
424-
410+
const nativeCodePush = requireNativeCodePush("shouldUpdateBeIgnored");
425411
const latestRollbackInfo = await nativeCodePush.getLatestRollbackInfo();
426412
if (!validateLatestRollbackInfo(latestRollbackInfo, remotePackage.packageHash)) {
427413
log("The latest rollback info is not valid.");
@@ -478,12 +464,7 @@ function setUpTestDependencies(testSdk, providedTestConfig, testNativeBridge) {
478464
}
479465

480466
async function restartApp(onlyIfUpdateIsPending = false) {
481-
const nativeCodePush = getNativeCodePush();
482-
if (!nativeCodePush) {
483-
logMissingNativeModule();
484-
return;
485-
}
486-
467+
const nativeCodePush = requireNativeCodePush("restartApp");
487468
nativeCodePush.restartApp(onlyIfUpdateIsPending);
488469
}
489470

@@ -903,30 +884,15 @@ Object.assign(CodePush, {
903884
setUpTestDependencies,
904885
sync,
905886
disallowRestart: () => {
906-
const nativeCodePush = getNativeCodePush();
907-
if (!nativeCodePush) {
908-
logMissingNativeModule();
909-
return;
910-
}
911-
887+
const nativeCodePush = requireNativeCodePush("disallowRestart");
912888
return nativeCodePush.disallow();
913889
},
914890
allowRestart: () => {
915-
const nativeCodePush = getNativeCodePush();
916-
if (!nativeCodePush) {
917-
logMissingNativeModule();
918-
return;
919-
}
920-
891+
const nativeCodePush = requireNativeCodePush("allowRestart");
921892
return nativeCodePush.allow();
922893
},
923894
clearUpdates: () => {
924-
const nativeCodePush = getNativeCodePush();
925-
if (!nativeCodePush) {
926-
logMissingNativeModule();
927-
return;
928-
}
929-
895+
const nativeCodePush = requireNativeCodePush("clearUpdates");
930896
return nativeCodePush.clearUpdates();
931897
},
932898
InstallMode: {

0 commit comments

Comments
 (0)