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

Commit b559ca2

Browse files
committed
Merge pull request #55 from Microsoft/updated-sync
Updated sync
2 parents ff891db + dae7043 commit b559ca2

20 files changed

+344
-174
lines changed

CodePush.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ failCallback:(void (^)(NSError *err))failCallback;
4949

5050
@interface CodePushPackage : NSObject
5151

52-
+ (void)applyPackage:(NSDictionary *)updatePackage
52+
+ (void)installPackage:(NSDictionary *)updatePackage
5353
error:(NSError **)error;
5454

5555
+ (NSDictionary *)getCurrentPackage:(NSError **)error;
@@ -72,8 +72,8 @@ failCallback:(void (^)(NSError *err))failCallback;
7272

7373
@end
7474

75-
typedef NS_ENUM(NSInteger, CodePushRestartMode) {
76-
CodePushRestartModeNone,
77-
CodePushRestartModeImmediate,
78-
CodePushRestartModeOnNextResume
75+
typedef NS_ENUM(NSInteger, CodePushInstallMode) {
76+
CodePushInstallModeImmediate,
77+
CodePushInstallModeOnNextRestart,
78+
CodePushInstallModeOnNextResume
7979
};

CodePush.ios.js

Lines changed: 115 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function getCurrentPackage() {
6060
return NativeCodePush.isFailedUpdate(currentPackage.packageHash);
6161
})
6262
.then((failedUpdate) => {
63-
localPackage.failedApply = failedUpdate;
63+
localPackage.failedInstall = failedUpdate;
6464
return NativeCodePush.isFirstRun(localPackage.packageHash);
6565
})
6666
.then((isFirstRun) => {
@@ -98,7 +98,7 @@ function checkForUpdate() {
9898
}
9999

100100
// Ignore updates that require a newer app version,
101-
// since the end-user couldn't reliably apply it
101+
// since the end-user couldn't reliably install it
102102
if (!update || update.updateAppVersion) {
103103
return resolve(null);
104104
}
@@ -107,7 +107,7 @@ function checkForUpdate() {
107107

108108
NativeCodePush.isFailedUpdate(update.packageHash)
109109
.then((isFailedHash) => {
110-
update.failedApply = isFailedHash;
110+
update.failedInstall = isFailedHash;
111111
resolve(update);
112112
})
113113
.catch(reject)
@@ -117,6 +117,11 @@ function checkForUpdate() {
117117
});
118118
}
119119

120+
/* Logs messages to console with the [CodePush] prefix */
121+
function log(message) {
122+
console.log(`[CodePush] ${message}`)
123+
}
124+
120125
/**
121126
* The sync method provides a simple, one-line experience for
122127
* incorporating the check, download and application of an update.
@@ -126,75 +131,128 @@ function checkForUpdate() {
126131
* releases, and displaying a standard confirmation UI to the end-user
127132
* when an update is available.
128133
*/
129-
function sync(options = {}) {
134+
function sync(options = {}, syncStatusChangeCallback, downloadProgressCallback) {
130135
var syncOptions = {
131-
descriptionPrefix: " Description: ",
132-
appendReleaseDescription: false,
133136

134137
ignoreFailedUpdates: true,
135-
136-
mandatoryContinueButtonLabel: "Continue",
137-
mandatoryUpdateMessage: "An update is available that must be installed.",
138-
139-
optionalIgnoreButtonLabel: "Ignore",
140-
optionalInstallButtonLabel: "Install",
141-
optionalUpdateMessage: "An update is available. Would you like to install it?",
142-
138+
installMode: CodePush.InstallMode.ON_NEXT_RESTART,
143139
rollbackTimeout: 0,
144-
145-
updateTitle: "Update available",
140+
updateDialog: null,
146141

147142
...options
148143
};
149-
144+
145+
syncStatusChangeCallback = typeof syncStatusChangeCallback == "function"
146+
? syncStatusChangeCallback
147+
: function(syncStatus) {
148+
switch(syncStatus) {
149+
case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
150+
log("Checking for update.");
151+
break;
152+
case CodePush.SyncStatus.AWAITING_USER_ACTION:
153+
log("Awaiting user action.");
154+
break;
155+
case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
156+
log("Downloading package.");
157+
break;
158+
case CodePush.SyncStatus.INSTALLING_UPDATE:
159+
log("Installing update.");
160+
break;
161+
case CodePush.SyncStatus.UP_TO_DATE:
162+
log("App is up to date.");
163+
break;
164+
case CodePush.SyncStatus.UPDATE_IGNORED:
165+
log("User cancelled the update.");
166+
break;
167+
case CodePush.SyncStatus.UPDATE_INSTALLED:
168+
/*
169+
* If the install mode is IMMEDIATE, this will not get returned as the
170+
* app will be restarted to a new Javascript context.
171+
*/
172+
if (syncOptions.installMode == CodePush.InstallMode.ON_NEXT_RESTART) {
173+
log("Update is installed and will be run on the next app restart.");
174+
} else {
175+
log("Update is installed and will be run when the app next resumes.");
176+
}
177+
break;
178+
case CodePush.SyncStatus.UNKNOWN_ERROR:
179+
log("An unknown error occurred.");
180+
break;
181+
}
182+
};
183+
184+
downloadProgressCallback = typeof downloadProgressCallback == "function"
185+
? downloadProgressCallback
186+
: function(downloadProgress) {
187+
log(`Expecting ${downloadProgress.totalBytes} bytes, received ${downloadProgress.receivedBytes} bytes.`);
188+
};
189+
150190
return new Promise((resolve, reject) => {
191+
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
151192
checkForUpdate()
152193
.then((remotePackage) => {
153-
if (!remotePackage || (remotePackage.failedApply && syncOptions.ignoreFailedUpdates)) {
194+
var doDownloadAndInstall = () => {
195+
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
196+
remotePackage.download(downloadProgressCallback)
197+
.then((localPackage) => {
198+
syncStatusChangeCallback(CodePush.SyncStatus.INSTALLING_UPDATE);
199+
return localPackage.install(syncOptions.rollbackTimeout, syncOptions.installMode, () => {
200+
syncStatusChangeCallback(CodePush.SyncStatus.UPDATE_INSTALLED);
201+
resolve(CodePush.SyncStatus.UPDATE_INSTALLED);
202+
});
203+
})
204+
.catch(reject)
205+
.done();
206+
}
207+
208+
if (!remotePackage || (remotePackage.failedInstall && syncOptions.ignoreFailedUpdates)) {
209+
syncStatusChangeCallback(CodePush.SyncStatus.UP_TO_DATE);
154210
resolve(CodePush.SyncStatus.UP_TO_DATE);
155211
}
156-
else {
212+
else if (syncOptions.updateDialog) {
213+
syncOptions.updateDialog = Object.assign(CodePush.DEFAULT_UPDATE_DIALOG, syncOptions.updateDialog);
214+
157215
var message = null;
158216
var dialogButtons = [
159217
{
160218
text: null,
161219
onPress: () => {
162-
remotePackage.download()
163-
.then((localPackage) => {
164-
resolve(CodePush.SyncStatus.UPDATE_APPLIED)
165-
return localPackage.apply(syncOptions.rollbackTimeout);
166-
})
167-
.catch(reject)
168-
.done();
220+
doDownloadAndInstall();
169221
}
170222
}
171223
];
172224

173225
if (remotePackage.isMandatory) {
174-
message = syncOptions.mandatoryUpdateMessage;
226+
message = syncOptions.updateDialog.mandatoryUpdateMessage;
175227
dialogButtons[0].text = syncOptions.mandatoryContinueButtonLabel;
176228
} else {
177-
message = syncOptions.optionalUpdateMessage;
178-
dialogButtons[0].text = syncOptions.optionalInstallButtonLabel;
229+
message = syncOptions.updateDialog.optionalUpdateMessage;
230+
dialogButtons[0].text = syncOptions.updateDialog.optionalInstallButtonLabel;
179231

180232
// Since this is an optional update, add another button
181233
// to allow the end-user to ignore it
182234
dialogButtons.push({
183-
text: syncOptions.optionalIgnoreButtonLabel,
235+
text: syncOptions.updateDialog.optionalIgnoreButtonLabel,
184236
onPress: () => resolve(CodePush.SyncStatus.UPDATE_IGNORED)
185237
});
186238
}
187239

188240
// If the update has a description, and the developer
189241
// explicitly chose to display it, then set that as the message
190-
if (syncOptions.appendReleaseDescription && remotePackage.description) {
191-
message += `${syncOptions.descriptionPrefix} ${remotePackage.description}`;
242+
if (syncOptions.updateDialog.appendReleaseDescription && remotePackage.description) {
243+
message += `${syncOptions.updateDialog.descriptionPrefix} ${remotePackage.description}`;
192244
}
193245

246+
syncStatusChangeCallback(CodePush.SyncStatus.AWAITING_USER_ACTION);
194247
AlertIOS.alert(syncOptions.updateTitle, message, dialogButtons);
248+
} else {
249+
doDownloadAndInstall();
195250
}
196251
})
197-
.catch(reject)
252+
.catch((error) => {
253+
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
254+
reject(error);
255+
})
198256
.done();
199257
});
200258
};
@@ -203,19 +261,35 @@ var CodePush = {
203261
checkForUpdate: checkForUpdate,
204262
getConfiguration: getConfiguration,
205263
getCurrentPackage: getCurrentPackage,
264+
log: log,
206265
notifyApplicationReady: NativeCodePush.notifyApplicationReady,
207266
setUpTestDependencies: setUpTestDependencies,
208267
sync: sync,
209-
RestartMode: {
210-
NONE: NativeCodePush.codePushRestartModeNone, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
211-
IMMEDIATE: NativeCodePush.codePushRestartModeImmediate, // Restart the app immediately
212-
ON_NEXT_RESUME: NativeCodePush.codePushRestartModeOnNextResume // Restart the app the next time it is resumed from the background
268+
InstallMode: {
269+
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
270+
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
271+
ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background
213272
},
214273
SyncStatus: {
215-
UP_TO_DATE: 0, // The running app is up-to-date
216-
UPDATE_IGNORED: 1, // The app had an optional update and the end-user chose to ignore it
217-
UPDATE_APPLIED: 2 // The app had an optional/mandatory update that was successfully downloaded and is about to be applied
274+
CHECKING_FOR_UPDATE: 0,
275+
AWAITING_USER_ACTION: 1,
276+
DOWNLOADING_PACKAGE: 2,
277+
INSTALLING_UPDATE: 3,
278+
UP_TO_DATE: 4, // The running app is up-to-date
279+
UPDATE_IGNORED: 5, // The app had an optional update and the end-user chose to ignore it
280+
UPDATE_INSTALLED: 6, // The app had an optional/mandatory update that was successfully downloaded and is about to be installed.
281+
UNKNOWN_ERROR: -1
282+
},
283+
DEFAULT_UPDATE_DIALOG: {
284+
appendReleaseDescription: false,
285+
descriptionPrefix: " Description: ",
286+
mandatoryContinueButtonLabel: "Continue",
287+
mandatoryUpdateMessage: "An update is available that must be installed.",
288+
optionalIgnoreButtonLabel: "Ignore",
289+
optionalInstallButtonLabel: "Install",
290+
optionalUpdateMessage: "An update is available. Would you like to install it?",
291+
updateTitle: "Update available",
218292
}
219293
};
220294

221-
module.exports = CodePush;
295+
module.exports = CodePush;

CodePush.m

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ - (void)checkForPendingUpdateDuringResume
102102

103103
- (NSDictionary *)constantsToExport
104104
{
105-
// Export the values of the CodePushRestartMode enum
105+
// Export the values of the CodePushInstallMode enum
106106
// so that the script-side can easily stay in sync
107-
return @{ @"codePushRestartModeNone": @(CodePushRestartModeNone),
108-
@"codePushRestartModeImmediate": @(CodePushRestartModeImmediate),
109-
@"codePushRestartModeOnNextResume": @(CodePushRestartModeOnNextResume)
107+
return @{ @"codePushInstallModeOnNextRestart": @(CodePushInstallModeOnNextRestart),
108+
@"codePushInstallModeImmediate": @(CodePushInstallModeImmediate),
109+
@"codePushInstallModeOnNextResume": @(CodePushInstallModeOnNextResume)
110110
};
111111
};
112112

@@ -124,7 +124,7 @@ - (CodePush *)init
124124
if (self) {
125125
// Do an async check to see whether
126126
// we need to start the rollback timer
127-
// due to a pending update being applied at start
127+
// due to a pending update being installed at start
128128
[self checkForPendingUpdate:NO];
129129

130130
// Register for app resume notifications so that we
@@ -204,7 +204,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
204204
rollbackTimeout:(int)rollbackTimeout
205205
{
206206
// Since we're not restarting, we need to store the fact that the update
207-
// was applied, but hasn't yet become "active".
207+
// was installed, but hasn't yet become "active".
208208
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
209209
NSDictionary *pendingUpdate = [[NSDictionary alloc] initWithObjectsAndKeys:
210210
packageHash,PendingUpdateHashKey,
@@ -225,31 +225,36 @@ - (void)startRollbackTimer:(int)rollbackTimeout
225225
}
226226

227227
// JavaScript-exported module methods
228-
RCT_EXPORT_METHOD(applyUpdate:(NSDictionary*)updatePackage
228+
RCT_EXPORT_METHOD(installUpdate:(NSDictionary*)updatePackage
229229
rollbackTimeout:(int)rollbackTimeout
230-
restartMode:(CodePushRestartMode)restartMode
230+
installMode:(CodePushInstallMode)installMode
231231
resolver:(RCTPromiseResolveBlock)resolve
232232
rejecter:(RCTPromiseRejectBlock)reject)
233233
{
234234
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
235235
NSError *error;
236-
[CodePushPackage applyPackage:updatePackage
236+
[CodePushPackage installPackage:updatePackage
237237
error:&error];
238238

239239
if (error) {
240240
reject(error);
241241
} else {
242-
if (restartMode == CodePushRestartModeImmediate) {
243-
[self initializeUpdateWithRollbackTimeout:rollbackTimeout needsRestart:YES];
244-
} else {
245-
_resumablePendingUpdateAvailable = (restartMode == CodePushRestartModeOnNextResume);
242+
if (installMode != CodePushInstallModeImmediate) {
243+
_resumablePendingUpdateAvailable = (installMode == CodePushInstallModeOnNextResume);
246244
[self savePendingUpdate:updatePackage[@"packageHash"]
247245
rollbackTimeout:rollbackTimeout];
248246
}
247+
// Signal to JS that the update has been applied.
248+
resolve(nil);
249249
}
250250
});
251251
}
252252

253+
// Only to be used in the case of installing updates with InstallMode.IMMEDIATE
254+
RCT_EXPORT_METHOD(restartApp:(int)rollbackTimeout){
255+
[self initializeUpdateWithRollbackTimeout:rollbackTimeout needsRestart:YES];
256+
}
257+
253258
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage
254259
resolver:(RCTPromiseResolveBlock)resolve
255260
rejecter:(RCTPromiseRejectBlock)reject)

CodePush.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
13BE3DEE1AC21097009241FE /* CodePush.m in Sources */ = {isa = PBXBuildFile; fileRef = 13BE3DED1AC21097009241FE /* CodePush.m */; };
11-
1B23B9141BF9267B000BB2F0 /* RCTConvert+CodePushRestartMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushRestartMode.m */; };
11+
1B23B9141BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */; };
1212
54FFEDE01BF550630061DD23 /* CodePushDownloadHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 54FFEDDF1BF550630061DD23 /* CodePushDownloadHandler.m */; };
1313
810D4E6D1B96935000B397E9 /* CodePushPackage.m in Sources */ = {isa = PBXBuildFile; fileRef = 810D4E6C1B96935000B397E9 /* CodePushPackage.m */; };
1414
81D51F3A1B6181C2000DA084 /* CodePushConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 81D51F391B6181C2000DA084 /* CodePushConfig.m */; };
@@ -30,7 +30,7 @@
3030
134814201AA4EA6300B7C361 /* libCodePush.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libCodePush.a; sourceTree = BUILT_PRODUCTS_DIR; };
3131
13BE3DEC1AC21097009241FE /* CodePush.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CodePush.h; sourceTree = "<group>"; };
3232
13BE3DED1AC21097009241FE /* CodePush.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePush.m; sourceTree = "<group>"; };
33-
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushRestartMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+CodePushRestartMode.m"; sourceTree = "<group>"; };
33+
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+CodePushInstallMode.m"; sourceTree = "<group>"; };
3434
54FFEDDF1BF550630061DD23 /* CodePushDownloadHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePushDownloadHandler.m; sourceTree = "<group>"; };
3535
810D4E6C1B96935000B397E9 /* CodePushPackage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePushPackage.m; sourceTree = "<group>"; };
3636
81D51F391B6181C2000DA084 /* CodePushConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CodePushConfig.m; sourceTree = "<group>"; };
@@ -58,7 +58,7 @@
5858
58B511D21A9E6C8500147676 = {
5959
isa = PBXGroup;
6060
children = (
61-
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushRestartMode.m */,
61+
1B23B9131BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m */,
6262
54FFEDDF1BF550630061DD23 /* CodePushDownloadHandler.m */,
6363
810D4E6C1B96935000B397E9 /* CodePushPackage.m */,
6464
81D51F391B6181C2000DA084 /* CodePushConfig.m */,
@@ -124,7 +124,7 @@
124124
isa = PBXSourcesBuildPhase;
125125
buildActionMask = 2147483647;
126126
files = (
127-
1B23B9141BF9267B000BB2F0 /* RCTConvert+CodePushRestartMode.m in Sources */,
127+
1B23B9141BF9267B000BB2F0 /* RCTConvert+CodePushInstallMode.m in Sources */,
128128
81D51F3A1B6181C2000DA084 /* CodePushConfig.m in Sources */,
129129
54FFEDE01BF550630061DD23 /* CodePushDownloadHandler.m in Sources */,
130130
13BE3DEE1AC21097009241FE /* CodePush.m in Sources */,

CodePushPackage.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ + (void)downloadPackage:(NSDictionary *)updatePackage
208208
[downloadHandler download:updatePackage[@"downloadUrl"]];
209209
}
210210

211-
+ (void)applyPackage:(NSDictionary *)updatePackage
211+
+ (void)installPackage:(NSDictionary *)updatePackage
212212
error:(NSError **)error
213213
{
214214
NSString *packageHash = updatePackage[@"packageHash" ];

0 commit comments

Comments
 (0)