This commit is contained in:
Geoffrey Goh
2015-11-12 22:14:49 -08:00
parent e42a3c5a7d
commit 78238071fe
2 changed files with 11 additions and 8 deletions

View File

@@ -265,7 +265,7 @@ The `RemotePackage` inherits all of the same properties as the `LocalPackage`, b
- __downloadUrl__: The URL at which the package is available for download. (String). This property is only needed for advanced usage, since the `download` method will automatically handle the acquisition of updates for you.
##### Methods
- __download(progressHandler): Promise<LocalPackage>__: Downloads the package update from the CodePush service. If a `progressHandler` is specified, it will be called periodically with an object (`{ totalBytes: Number, receivedBytes: Number }`) that reports the progress of the download till the download completes. Returns a Promise that resolves with the `LocalPackage`.
- __download(progressHandler?: Function): Promise<LocalPackage>__: Downloads the package update from the CodePush service. If a `progressHandler` is specified, it will be called periodically with an object (`{ totalBytes: Number, receivedBytes: Number }`) that reports the progress of the download till the download completes. Returns a Promise that resolves with the `LocalPackage`.
---

View File

@@ -11,21 +11,24 @@ module.exports = (NativeCodePush) => {
return Promise.reject(new Error("Cannot download an update without a download url"));
}
// Use event subscription to obtain download progress.
var downloadProgressSubscription = NativeAppEventEmitter.addListener(
'CodePushDownloadProgress',
progressHandler
);
var downloadProgressSubscription;
if (progressHandler) {
// Use event subscription to obtain download progress.
downloadProgressSubscription = NativeAppEventEmitter.addListener(
'CodePushDownloadProgress',
progressHandler
);
}
// Use the downloaded package info. Native code will save the package info
// so that the client knows what the current package version is.
return NativeCodePush.downloadUpdate(this)
.then((downloadedPackage) => {
downloadProgressSubscription.remove();
downloadProgressSubscription && downloadProgressSubscription.remove();
return extend({}, downloadedPackage, local);
})
.catch((error) => {
downloadProgressSubscription.remove();
downloadProgressSubscription && downloadProgressSubscription.remove();
// Rethrow the error for subsequent handlers down the promise chain.
throw error;
});