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

Commit 7af04c2

Browse files
committed
Merge branch 'master' of github.com:Microsoft/react-native-code-push into add-postlink-hooks
2 parents 22be520 + 53892d9 commit 7af04c2

File tree

4 files changed

+309
-44
lines changed

4 files changed

+309
-44
lines changed

CodePush.js

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Alert } from "./AlertAdapter";
33
import requestFetchAdapter from "./request-fetch-adapter";
44
import { AppState, Platform } from "react-native";
55
import RestartManager from "./RestartManager";
6-
import log from './logging';
6+
import log from "./logging";
77

88
let NativeCodePush = require("react-native").NativeModules.CodePush;
99
const PackageMixins = require("./package-mixins")(NativeCodePush);
@@ -75,7 +75,7 @@ async function checkForUpdate(deploymentKey = null) {
7575
localPackage && (update.packageHash === localPackage.packageHash) ||
7676
(!localPackage || localPackage._isDebugOnly) && config.packageHash === update.packageHash) {
7777
if (update && update.updateAppVersion) {
78-
log("An update is available but it is targeting a newer binary version than you are currently running.");
78+
log("An update is available but it is not targeting the binary version of your app.");
7979
}
8080

8181
return null;
@@ -401,12 +401,65 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
401401

402402
let CodePush;
403403

404+
function codePushify(options = {}) {
405+
let React;
406+
let ReactNative = require("react-native");
407+
408+
try { React = require("react"); } catch (e) { }
409+
if (!React) {
410+
try { React = ReactNative.React; } catch (e) { }
411+
if (!React) {
412+
throw new Error("Unable to find the 'React' module.");
413+
}
414+
}
415+
416+
if (!React.Component) {
417+
throw new Error(
418+
`Unable to find the "Component" class, please either:
419+
1. Upgrade to a newer version of React Native that supports it, or
420+
2. Call the codePush.sync API in your component instead of using the @codePush decorator`
421+
);
422+
}
423+
424+
var decorator = (RootComponent) => {
425+
return class CodePushComponent extends React.Component {
426+
componentDidMount() {
427+
if (options.checkFrequency === CodePush.CheckFrequency.MANUAL) {
428+
CodePush.notifyAppReady();
429+
} else {
430+
let rootComponentInstance = this.refs.rootComponent;
431+
let syncStatusCallback = rootComponentInstance && rootComponentInstance.codePushStatusDidChange;
432+
let downloadProgressCallback = rootComponentInstance && rootComponentInstance.codePushDownloadDidProgress;
433+
CodePush.sync(options, syncStatusCallback, downloadProgressCallback);
434+
if (options.checkFrequency === CodePush.CheckFrequency.ON_APP_RESUME) {
435+
ReactNative.AppState.addEventListener("change", (newState) => {
436+
newState === "active" && CodePush.sync(options, syncStatusCallback, downloadProgressCallback);
437+
});
438+
}
439+
}
440+
}
441+
442+
render() {
443+
return <RootComponent {...this.props} ref={"rootComponent"} />
444+
}
445+
}
446+
}
447+
448+
if (typeof options === "function") {
449+
// Infer that the root component was directly passed to us.
450+
return decorator(options);
451+
} else {
452+
return decorator;
453+
}
454+
}
455+
404456
// If the "NativeCodePush" variable isn't defined, then
405457
// the app didn't properly install the native module,
406458
// and therefore, it doesn't make sense initializing
407459
// the JS interface when it wouldn't work anyways.
408460
if (NativeCodePush) {
409-
CodePush = {
461+
CodePush = codePushify;
462+
Object.assign(CodePush, {
410463
AcquisitionSdk: Sdk,
411464
checkForUpdate,
412465
getConfiguration,
@@ -436,6 +489,11 @@ if (NativeCodePush) {
436489
DOWNLOADING_PACKAGE: 7,
437490
INSTALLING_UPDATE: 8
438491
},
492+
CheckFrequency: {
493+
ON_APP_START: 0,
494+
ON_APP_RESUME: 1,
495+
MANUAL: 2
496+
},
439497
UpdateState: {
440498
RUNNING: NativeCodePush.codePushUpdateStateRunning,
441499
PENDING: NativeCodePush.codePushUpdateStatePending,
@@ -455,7 +513,7 @@ if (NativeCodePush) {
455513
optionalUpdateMessage: "An update is available. Would you like to install it?",
456514
title: "Update available"
457515
}
458-
};
516+
});
459517
} else {
460518
log("The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly.");
461519
}

0 commit comments

Comments
 (0)