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

Commit f0806be

Browse files
committed
Merge pull request #22 from Microsoft/codepush_sync
CodePush.sync() API method
2 parents 43f0f44 + 9cef711 commit f0806be

File tree

1 file changed

+91
-2
lines changed

1 file changed

+91
-2
lines changed

CodePush.ios.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var NativeCodePush = require('react-native').NativeModules.CodePush;
1010
var requestFetchAdapter = require("./request-fetch-adapter.js");
1111
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
1212
var packageMixins = require("./package-mixins")(NativeCodePush);
13+
var { AlertIOS } = require("react-native");
1314

1415
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
1516
function setUpTestDependencies(providedTestSdk, providedTestConfig, testNativeBridge){
@@ -95,12 +96,100 @@ function notifyApplicationReady() {
9596
return NativeCodePush.notifyApplicationReady();
9697
}
9798

99+
/**
100+
* The sync method provides a simple, one-line experience for
101+
* incorporating the check, download and application of an update.
102+
*
103+
* It simply composes the existing API methods together and adds additional
104+
* support for respecting mandatory updates, ignoring previously failed
105+
* releases, and displaying a standard confirmation UI to the end-user
106+
* when an update is available.
107+
*/
108+
function sync(options = {}) {
109+
var syncOptions = {
110+
descriptionPrefix: " Description: ",
111+
appendReleaseDescription: false,
112+
113+
ignoreFailedUpdates: true,
114+
115+
mandatoryContinueButtonLabel: "Continue",
116+
mandatoryUpdateMessage: "An update is available that must be installed.",
117+
118+
optionalIgnoreButtonLabel: "Ignore",
119+
optionalInstallButtonLabel: "Install",
120+
optionalUpdateMessage: "An update is available. Would you like to install it?",
121+
122+
rollbackTimeout: 0,
123+
124+
updateTitle: "Update available",
125+
126+
...options
127+
};
128+
129+
return new Promise((resolve, reject) => {
130+
checkForUpdate()
131+
.then((remotePackage) => {
132+
if (!remotePackage || (remotePackage.failedApply && syncOptions.ignoreFailedUpdates)) {
133+
resolve(CodePush.SyncStatus.UP_TO_DATE);
134+
}
135+
else {
136+
var message = null;
137+
var dialogButtons = [
138+
{
139+
text: null,
140+
onPress: () => {
141+
remotePackage.download()
142+
.then((localPackage) => {
143+
resolve(CodePush.SyncStatus.UPDATE_APPLIED)
144+
return localPackage.apply(syncOptions.rollbackTimeout);
145+
})
146+
.catch(reject)
147+
.done();
148+
}
149+
}
150+
];
151+
152+
if (remotePackage.isMandatory) {
153+
message = syncOptions.mandatoryUpdateMessage;
154+
dialogButtons[0].text = syncOptions.mandatoryContinueButtonLabel;
155+
} else {
156+
message = syncOptions.optionalUpdateMessage;
157+
dialogButtons[0].text = syncOptions.optionalInstallButtonLabel;
158+
159+
// Since this is an optional update, add another button
160+
// to allow the end-user to ignore it
161+
dialogButtons.push({
162+
text: syncOptions.optionalIgnoreButtonLabel,
163+
onPress: () => resolve(CodePush.SyncStatus.UPDATE_IGNORED)
164+
});
165+
}
166+
167+
// If the update has a description, and the developer
168+
// explicitly chose to display it, then set that as the message
169+
if (syncOptions.appendReleaseDescription && remotePackage.description) {
170+
message += `${syncOptions.descriptionPrefix} ${remotePackage.description}`;
171+
}
172+
173+
AlertIOS.alert(syncOptions.updateTitle, message, dialogButtons);
174+
}
175+
})
176+
.catch(reject)
177+
.done();
178+
});
179+
};
180+
98181
var CodePush = {
99182
getConfiguration: getConfiguration,
100183
checkForUpdate: checkForUpdate,
101184
getCurrentPackage: getCurrentPackage,
102185
notifyApplicationReady: notifyApplicationReady,
103-
setUpTestDependencies: setUpTestDependencies
186+
setUpTestDependencies: setUpTestDependencies,
187+
sync: sync,
188+
SyncStatus: {
189+
UP_TO_DATE: 0, // The running app is up-to-date
190+
UPDATE_IGNORED: 1, // The app had an optional update and the end-user chose to ignore it
191+
UPDATE_APPLIED: 2 // The app had an optional/mandatory update that was successfully downloaded and is about to be applied
192+
}
104193
};
105194

106-
module.exports = CodePush;
195+
module.exports = CodePush;

0 commit comments

Comments
 (0)