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

Commit 9f15475

Browse files
authored
Merge pull request #431 from Microsoft/codepushify
Add @codepush decorator
2 parents 13df1f2 + 907354b commit 9f15475

File tree

3 files changed

+281
-39
lines changed

3 files changed

+281
-39
lines changed

CodePush.js

Lines changed: 54 additions & 3 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);
@@ -401,12 +401,58 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
401401

402402
let CodePush;
403403

404+
function codePushify(options = {}) {
405+
return (RootComponent) => {
406+
let React;
407+
let ReactNative = require("react-native");
408+
409+
try { React = require("react"); } catch (e) { }
410+
if (!React) {
411+
try { React = ReactNative.React; } catch (e) { }
412+
if (!React) {
413+
throw new Error("Unable to find the 'React' module.");
414+
}
415+
}
416+
417+
if (!React.Component) {
418+
throw new Error(
419+
`Unable to find the "Component" class, please either:
420+
1. Upgrade to a newer version of React Native that supports it, or
421+
2. Call the codePush.sync API in your component instead of using the @codePush decorator`
422+
);
423+
}
424+
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+
404449
// If the "NativeCodePush" variable isn't defined, then
405450
// the app didn't properly install the native module,
406451
// and therefore, it doesn't make sense initializing
407452
// the JS interface when it wouldn't work anyways.
408453
if (NativeCodePush) {
409-
CodePush = {
454+
CodePush = codePushify;
455+
Object.assign(CodePush, {
410456
AcquisitionSdk: Sdk,
411457
checkForUpdate,
412458
getConfiguration,
@@ -436,6 +482,11 @@ if (NativeCodePush) {
436482
DOWNLOADING_PACKAGE: 7,
437483
INSTALLING_UPDATE: 8
438484
},
485+
CheckFrequency: {
486+
ON_APP_START: 0,
487+
ON_APP_RESUME: 1,
488+
MANUAL: 2
489+
},
439490
UpdateState: {
440491
RUNNING: NativeCodePush.codePushUpdateStateRunning,
441492
PENDING: NativeCodePush.codePushUpdateStatePending,
@@ -455,7 +506,7 @@ if (NativeCodePush) {
455506
optionalUpdateMessage: "An update is available. Would you like to install it?",
456507
title: "Update available"
457508
}
458-
};
509+
});
459510
} else {
460511
log("The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly.");
461512
}

0 commit comments

Comments
 (0)