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

Commit 838efc8

Browse files
committed
Merge pull request #294 from Microsoft/updateMetadata
codePush.getUpdateMetadata
2 parents fbc6bbb + 4e63185 commit 838efc8

File tree

11 files changed

+291
-153
lines changed

11 files changed

+291
-153
lines changed

CodePush.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,16 @@ const getConfiguration = (() => {
100100
})();
101101

102102
async function getCurrentPackage() {
103-
const localPackage = await NativeCodePush.getCurrentPackage();
104-
if (localPackage) {
105-
localPackage.failedInstall = await NativeCodePush.isFailedUpdate(localPackage.packageHash);
106-
localPackage.isFirstRun = await NativeCodePush.isFirstRun(localPackage.packageHash);
103+
return await getUpdateMetadata(CodePush.UpdateState.LATEST);
104+
}
105+
106+
async function getUpdateMetadata(updateState) {
107+
const updateMetadata = await NativeCodePush.getUpdateMetadata(updateState || CodePush.UpdateState.RUNNING);
108+
if (updateMetadata) {
109+
updateMetadata.failedInstall = await NativeCodePush.isFailedUpdate(updateMetadata.packageHash);
110+
updateMetadata.isFirstRun = await NativeCodePush.isFirstRun(updateMetadata.packageHash);
107111
}
108-
return localPackage;
112+
return updateMetadata;
109113
}
110114

111115
function getPromisifiedSdk(requestFetchAdapter, config) {
@@ -383,45 +387,52 @@ let CodePush;
383387
// and therefore, it doesn't make sense initializing
384388
// the JS interface when it wouldn't work anyways.
385389
if (NativeCodePush) {
386-
CodePush = {
387-
AcquisitionSdk: Sdk,
388-
checkForUpdate,
389-
getConfiguration,
390-
getCurrentPackage,
391-
log,
392-
notifyApplicationReady,
393-
restartApp,
394-
setUpTestDependencies,
395-
sync,
396-
InstallMode: {
397-
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
398-
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
399-
ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background
400-
},
401-
SyncStatus: {
402-
CHECKING_FOR_UPDATE: 0,
403-
AWAITING_USER_ACTION: 1,
404-
DOWNLOADING_PACKAGE: 2,
405-
INSTALLING_UPDATE: 3,
406-
UP_TO_DATE: 4, // The running app is up-to-date
407-
UPDATE_IGNORED: 5, // The app had an optional update and the end-user chose to ignore it
408-
UPDATE_INSTALLED: 6, // The app had an optional/mandatory update that was successfully downloaded and is about to be installed.
409-
SYNC_IN_PROGRESS: 7, // There is an ongoing "sync" operation in progress.
410-
UNKNOWN_ERROR: -1
411-
},
412-
DEFAULT_UPDATE_DIALOG: {
413-
appendReleaseDescription: false,
414-
descriptionPrefix: " Description: ",
415-
mandatoryContinueButtonLabel: "Continue",
416-
mandatoryUpdateMessage: "An update is available that must be installed.",
417-
optionalIgnoreButtonLabel: "Ignore",
418-
optionalInstallButtonLabel: "Install",
419-
optionalUpdateMessage: "An update is available. Would you like to install it?",
420-
title: "Update available"
421-
}
390+
CodePush = {
391+
AcquisitionSdk: Sdk,
392+
checkForUpdate,
393+
getConfiguration,
394+
getCurrentPackage,
395+
getUpdateMetadata,
396+
log,
397+
notifyAppReady: notifyApplicationReady,
398+
notifyApplicationReady,
399+
restartApp,
400+
setUpTestDependencies,
401+
sync,
402+
InstallMode: {
403+
IMMEDIATE: NativeCodePush.codePushInstallModeImmediate, // Restart the app immediately
404+
ON_NEXT_RESTART: NativeCodePush.codePushInstallModeOnNextRestart, // Don't artificially restart the app. Allow the update to be "picked up" on the next app restart
405+
ON_NEXT_RESUME: NativeCodePush.codePushInstallModeOnNextResume // Restart the app the next time it is resumed from the background
406+
},
407+
SyncStatus: {
408+
CHECKING_FOR_UPDATE: 0,
409+
AWAITING_USER_ACTION: 1,
410+
DOWNLOADING_PACKAGE: 2,
411+
INSTALLING_UPDATE: 3,
412+
UP_TO_DATE: 4, // The running app is up-to-date
413+
UPDATE_IGNORED: 5, // The app had an optional update and the end-user chose to ignore it
414+
UPDATE_INSTALLED: 6, // The app had an optional/mandatory update that was successfully downloaded and is about to be installed.
415+
SYNC_IN_PROGRESS: 7, // There is an ongoing "sync" operation in progress.
416+
UNKNOWN_ERROR: -1
417+
},
418+
UpdateState: {
419+
RUNNING: NativeCodePush.codePushUpdateStateRunning,
420+
PENDING: NativeCodePush.codePushUpdateStatePending,
421+
LATEST: NativeCodePush.codePushUpdateStateLatest
422+
},
423+
DEFAULT_UPDATE_DIALOG: {
424+
appendReleaseDescription: false,
425+
descriptionPrefix: " Description: ",
426+
mandatoryContinueButtonLabel: "Continue",
427+
mandatoryUpdateMessage: "An update is available that must be installed.",
428+
optionalIgnoreButtonLabel: "Ignore",
429+
optionalInstallButtonLabel: "Install",
430+
optionalUpdateMessage: "An update is available. Would you like to install it?",
431+
title: "Update available"
422432
}
433+
};
423434
} else {
424-
log("The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly.");
435+
log("The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly.");
425436
}
426437

427-
module.exports = CodePush;
438+
module.exports = CodePush;

README.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ When you require `react-native-code-push`, the module object provides the follow
378378

379379
* [checkForUpdate](#codepushcheckforupdate): Asks the CodePush service whether the configured app deployment has an update available.
380380

381-
* [getCurrentPackage](#codepushgetcurrentpackage): Retrieves the metadata about the currently installed update (e.g. description, installation time, size).
381+
* [getUpdateMetadata](#codepushgetupdatemetadata): Retrieves the metadata for an installed update (e.g. description, mandatory).
382382

383383
* [notifyApplicationReady](#codepushnotifyapplicationready): Notifies the CodePush runtime that an installed update is considered successful. If you are manually checking for and installing updates (i.e. not using the [sync](#codepushsync) method to handle it all for you), then this method **MUST** be called; otherwise CodePush will treat the update as failed and rollback to the previous version when the app next restarts.
384384

@@ -417,33 +417,41 @@ codePush.checkForUpdate()
417417
});
418418
```
419419

420-
#### codePush.getCurrentPackage
420+
#### codePush.getUpdateMetadata
421421

422422
```javascript
423-
codePush.getCurrentPackage(): Promise<LocalPackage>;
423+
codePush.getUpdateMetadata(updateState: UpdateState = UpdateState.RUNNING): Promise<LocalPackage>;
424424
```
425425

426-
Retrieves the metadata about the currently installed "package" (e.g. description, installation time). This can be useful for scenarios such as displaying a "what's new?" dialog after an update has been applied or checking whether there is a pending update that is waiting to be applied via a resume or restart.
426+
Retrieves the metadata for an installed update (e.g. description, mandatory) whose state matches the specified `updateState` parameter. This can be useful for scenarios such as displaying a "what's new?" dialog after an update has been applied or checking whether there is a pending update that is waiting to be applied via a resume or restart. For more details about the possible update states, and what they represent, refer to the [UpdateState reference](#updatestate).
427427

428428
This method returns a `Promise` which resolves to one of two possible values:
429429

430-
1. `null` if the app is currently running the JS bundle from the binary and not a CodePush update. This occurs in the following scenarios:
430+
1. `null` if an update with the specified state doesn't currently exist. This occurs in the following scenarios:
431431

432-
1. The end-user installed the app binary and has yet to install a CodePush update
432+
1. The end-user hasn't installed any CodePush updates yet, and therefore, no metadata is available for any updates, regardless what you specify as the `updateState` parameter.
433433
1. The end-user installed an update of the binary (e.g. from the store), which cleared away the old CodePush updates, and gave precedence back to the JS binary in the binary.
434+
1. The `updateState` parameter is set to `UpdateState.RUNNING`, but the app isn't currently running a CodePush update. There may be a pending update, which requires an app restart to become active.
435+
1. The `updateState` parameter is set to `UpdateState.PENDING`, but the app doesn't have any pending updates.
434436

435-
2. A [`LocalPackage`](#localpackage) instance which represents the metadata for the currently running CodePush update.
437+
2. A [`LocalPackage`](#localpackage) instance which represents the metadata for the currently requested CodePush update (either the running or pending).
436438

437439
Example Usage:
438440

439441
```javascript
440-
codePush.getCurrentPackage()
441-
.then((update) => {
442-
// If the current app "session" represents the first time
443-
// this update has run, and it had a description provided
444-
// with it upon release, let's show it to the end user
445-
if (update.isFirstRun && update.description) {
446-
// Display a "what's new?" modal
442+
// Check if there is currently a CodePush update running, and if
443+
// so, register it with the HockeyApp SDK (https://github.com/slowpath/react-native-hockeyapp)
444+
// so that crash reports will correctly display the JS bundle version the user was running.
445+
codePush.getUpdateMetadata().then((update) => {
446+
if (update) {
447+
hockeyApp.addMetadata({ CodePushRelease: update.label });
448+
}
449+
});
450+
451+
// Check to see if there is still an update pending.
452+
codePush.getUpdateMetadata(UpdateState.PENDING).then((update) => {
453+
if (update) {
454+
// There's a pending update, do we want to force a restart?
447455
}
448456
});
449457
```
@@ -486,7 +494,6 @@ This method provides support for two different (but customizable) "modes" to eas
486494

487495
2. **Active mode**, which when an update is available, prompts the end user for permission before downloading it, and then immediately applies the update. If an update was released using the `mandatory` flag, the end user would still be notified about the update, but they wouldn't have the choice to ignore it.
488496

489-
490497
Example Usage:
491498

492499
```javascript
@@ -650,7 +657,7 @@ The CodePush API includes the following enums which can be used to customize the
650657
651658
##### InstallMode
652659
653-
This enum specified when you would like an installed update to actually be applied, and can be passed to either the `sync` or `LocalPackage.install` methods. It includes the following values:
660+
This enum specifies when you would like an installed update to actually be applied, and can be passed to either the `sync` or `LocalPackage.install` methods. It includes the following values:
654661
655662
* __codePush.InstallMode.IMMEDIATE__ *(0)* - Indicates that you want to install the update and restart the app immediately. This value is appropriate for debugging scenarios as well as when displaying an update prompt to the user, since they would expect to see the changes immediately after accepting the installation. Additionally, this mode can be used to enforce mandatory updates, since it removes the potentially undesired latency between the update installation and the next time the end user restarts or resumes the app.
656663
@@ -672,6 +679,16 @@ This enum is provided to the `syncStatusChangedCallback` function that can be pa
672679
* __codePush.SyncStatus.SYNC_IN_PROGRESS__ *(7)* - There is an ongoing `sync` operation running which prevents the current call from being executed.
673680
* __codePush.SyncStatus.UNKNOWN_ERROR__ *(-1)* - The sync operation encountered an unknown error.
674681
682+
##### UpdateState
683+
684+
This enum specifies the state that an update is currently in, and can be specified when calling the `getUpdateMetadata` method. It includes the following values:
685+
686+
* __codePush.UpdateState.RUNNING__ *(0)* - Indicates that an update represents the version of the app that is currently running. This can be useful for identifying attributes about the app, for scenarios such as displaying the release description in a "what's new?" dialog or reporting the latest version to an analytics and/or crash reporting service.
687+
688+
* __codePush.UpdateState.PENDING__ *(1)* - Indicates than an update has been installed, but the app hasn't been restarted yet in order to apply it. This can be useful for determining whether there is a pending update, which you may want to force a programmatic restart (via `restartApp`) in order to apply.
689+
690+
* __codePush.UpdateState.LATEST__ *(2)* - Indicates than an update represents the latest available release, and can be either currently running or pending.
691+
675692
### Objective-C API Reference (iOS)
676693
677694
The Objective-C API is made available by importing the `CodePush.h` header into your `AppDelegate.m` file, and consists of a single public class named `CodePush`.

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,15 @@ public CodePushNativeModule(ReactApplicationContext reactContext) {
353353
@Override
354354
public Map<String, Object> getConstants() {
355355
final Map<String, Object> constants = new HashMap<>();
356+
356357
constants.put("codePushInstallModeImmediate", CodePushInstallMode.IMMEDIATE.getValue());
357358
constants.put("codePushInstallModeOnNextRestart", CodePushInstallMode.ON_NEXT_RESTART.getValue());
358359
constants.put("codePushInstallModeOnNextResume", CodePushInstallMode.ON_NEXT_RESUME.getValue());
360+
361+
constants.put("codePushUpdateStateRunning", CodePushUpdateState.RUNNING.getValue());
362+
constants.put("codePushUpdateStatePending", CodePushUpdateState.PENDING.getValue());
363+
constants.put("codePushUpdateStateLatest", CodePushUpdateState.LATEST.getValue());
364+
359365
return constants;
360366
}
361367

@@ -481,31 +487,51 @@ public void getConfiguration(Promise promise) {
481487

482488
promise.resolve(configMap);
483489
}
484-
490+
485491
@ReactMethod
486-
public void getCurrentPackage(final Promise promise) {
492+
public void getUpdateMetadata(final int updateState, final Promise promise) {
487493
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
488494
@Override
489495
protected Void doInBackground(Void... params) {
490496
WritableMap currentPackage = codePushPackage.getCurrentPackage();
497+
491498
if (currentPackage == null) {
492499
promise.resolve("");
493500
return null;
494501
}
495502

496-
if (isRunningBinaryVersion) {
497-
currentPackage.putBoolean("_isDebugOnly", true);
498-
}
499-
500-
Boolean isPendingUpdate = false;
503+
Boolean currentUpdateIsPending = false;
501504

502505
if (currentPackage.hasKey(PACKAGE_HASH_KEY)) {
503506
String currentHash = currentPackage.getString(PACKAGE_HASH_KEY);
504-
isPendingUpdate = CodePush.this.isPendingUpdate(currentHash);
507+
currentUpdateIsPending = CodePush.this.isPendingUpdate(currentHash);
505508
}
509+
510+
if (updateState == CodePushUpdateState.PENDING.getValue() && !currentUpdateIsPending) {
511+
// The caller wanted a pending update
512+
// but there isn't currently one.
513+
promise.resolve("");
514+
} else if (updateState == CodePushUpdateState.RUNNING.getValue() && currentUpdateIsPending) {
515+
// The caller wants the running update, but the current
516+
// one is pending, so we need to grab the previous.
517+
promise.resolve(codePushPackage.getPreviousPackage());
518+
} else {
519+
// The current package satisfies the request:
520+
// 1) Caller wanted a pending, and there is a pending update
521+
// 2) Caller wanted the running update, and there isn't a pending
522+
// 3) Caller wants the latest update, regardless if it's pending or not
523+
if (isRunningBinaryVersion) {
524+
// This only matters in Debug builds. Since we do not clear "outdated" updates,
525+
// we need to indicate to the JS side that somehow we have a current update on
526+
// disk that is not actually running.
527+
currentPackage.putBoolean("_isDebugOnly", true);
528+
}
506529

507-
currentPackage.putBoolean("isPending", isPendingUpdate);
508-
promise.resolve(currentPackage);
530+
// Enable differentiating pending vs. non-pending updates
531+
currentPackage.putBoolean("isPending", currentUpdateIsPending);
532+
promise.resolve(currentPackage);
533+
}
534+
509535
return null;
510536
}
511537
};

android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.nio.ByteBuffer;
1919

2020
public class CodePushPackage {
21-
2221
private final String CODE_PUSH_FOLDER_PREFIX = "CodePush";
2322
private final String CURRENT_PACKAGE_KEY = "currentPackage";
2423
private final String DIFF_MANIFEST_FILE_NAME = "hotcodepush.json";
@@ -124,18 +123,21 @@ public String getPreviousPackageHash() {
124123
}
125124

126125
public WritableMap getCurrentPackage() {
127-
String folderPath = getCurrentPackageFolderPath();
128-
if (folderPath == null) {
126+
String packageHash = getCurrentPackageHash();
127+
if (packageHash == null) {
129128
return null;
130129
}
131-
132-
String packagePath = CodePushUtils.appendPathComponent(folderPath, PACKAGE_FILE_NAME);
133-
try {
134-
return CodePushUtils.getWritableMapFromFile(packagePath);
135-
} catch (IOException e) {
136-
// Should not happen unless the update metadata was somehow deleted.
130+
131+
return getPackage(packageHash);
132+
}
133+
134+
public WritableMap getPreviousPackage() {
135+
String packageHash = getPreviousPackageHash();
136+
if (packageHash == null) {
137137
return null;
138138
}
139+
140+
return getPackage(packageHash);
139141
}
140142

141143
public WritableMap getPackage(String packageHash) {
@@ -340,8 +342,6 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
340342
}
341343

342344
public void clearUpdates() {
343-
File statusFile = new File(getStatusFilePath());
344-
statusFile.delete();
345345
FileUtils.deleteDirectoryAtPath(getCodePushPath());
346346
}
347-
}
347+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.microsoft.codepush.react;
2+
3+
public enum CodePushUpdateState {
4+
RUNNING(0),
5+
PENDING(1),
6+
LATEST(2);
7+
8+
private final int value;
9+
CodePushUpdateState(int value) {
10+
this.value = value;
11+
}
12+
public int getValue() {
13+
return this.value;
14+
}
15+
}

0 commit comments

Comments
 (0)