Added support for download progress notifications. Renamed SyncStatus.APPLY_SUCCESS to SyncStatus.UPDATE_INSTALLED.

This commit is contained in:
unknown 2015-11-19 09:51:54 -08:00
Родитель f2ea35405a
Коммит 9965901f49
13 изменённых файлов: 49 добавлений и 26 удалений

Просмотреть файл

@ -175,7 +175,7 @@ Contains details about an update package that is available for download.
Defines the possible result statuses of the [sync](#codepushsync) operation. Defines the possible result statuses of the [sync](#codepushsync) operation.
### Properties ### Properties
- __UP_TO_DATE__: The application is up to date. (number) - __UP_TO_DATE__: The application is up to date. (number)
- __APPLY_SUCCESS__: An update is available, it has been downloaded, unzipped and copied to the deployment folder. After the completion of the callback invoked with SyncStatus.APPLY_SUCCESS, the application will be reloaded with the updated code and resources. (number) - __UPDATE_INSTALLED__: An update is available, it has been downloaded, unzipped and copied to the deployment folder. After the completion of the callback invoked with SyncStatus.UPDATE_INSTALLED, the application will be reloaded with the updated code and resources. (number)
- __UPDATE_IGNORED__: An optional update is available, but the user declined to install it. The update was not downloaded. (number) - __UPDATE_IGNORED__: An optional update is available, but the user declined to install it. The update was not downloaded. (number)
- __ERROR__: An error happened during the sync operation. This might be an error while communicating with the server, downloading or unziping the update. The console logs should contain more information about what happened. No update has been applied in this case. (number) - __ERROR__: An error happened during the sync operation. This might be an error while communicating with the server, downloading or unziping the update. The console logs should contain more information about what happened. No update has been applied in this case. (number)
@ -327,7 +327,7 @@ Interface defining several options for customizing the [sync](#codepushsync) ope
window.codePush.sync(function (syncStatus) { window.codePush.sync(function (syncStatus) {
switch (syncStatus) { switch (syncStatus) {
case SyncStatus.APPLY_SUCCESS: case SyncStatus.UPDATE_INSTALLED:
console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content."); console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content.");
/* Don't continue app initialization, the application will refresh after this return. */ /* Don't continue app initialization, the application will refresh after this return. */
return; return;
@ -362,7 +362,7 @@ var syncOptions = {
window.codePush.sync(function (syncStatus) { window.codePush.sync(function (syncStatus) {
switch (syncStatus) { switch (syncStatus) {
case SyncStatus.APPLY_SUCCESS: case SyncStatus.UPDATE_INSTALLED:
console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content."); console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content.");
/* Don't continue app initialization, the application will refresh after this return. */ /* Don't continue app initialization, the application will refresh after this return. */
return; return;

Просмотреть файл

@ -109,7 +109,7 @@ var CodePush = (function () {
syncCallback && syncCallback(SyncStatus.ERROR); syncCallback && syncCallback(SyncStatus.ERROR);
}; };
var onApplySuccess = function () { var onApplySuccess = function () {
syncCallback && syncCallback(SyncStatus.APPLY_SUCCESS); syncCallback && syncCallback(SyncStatus.UPDATE_INSTALLED);
}; };
var onDownloadSuccess = function (localPackage) { var onDownloadSuccess = function (localPackage) {
localPackage.apply(onApplySuccess, onError, syncOptions.rollbackTimeout); localPackage.apply(onApplySuccess, onError, syncOptions.rollbackTimeout);

Просмотреть файл

@ -25,7 +25,7 @@ var RemotePackage = (function (_super) {
function RemotePackage() { function RemotePackage() {
_super.apply(this, arguments); _super.apply(this, arguments);
} }
RemotePackage.prototype.download = function (successCallback, errorCallback) { RemotePackage.prototype.download = function (successCallback, errorCallback, downloadProgress) {
var _this = this; var _this = this;
try { try {
CodePushUtil.logMessage("Downloading update package ..."); CodePushUtil.logMessage("Downloading update package ...");
@ -59,6 +59,12 @@ var RemotePackage = (function (_super) {
_this.currentFileTransfer = null; _this.currentFileTransfer = null;
CodePushUtil.invokeErrorCallback(new Error(error.body), errorCallback); CodePushUtil.invokeErrorCallback(new Error(error.body), errorCallback);
}; };
this.currentFileTransfer.onprogress = function (progressEvent) {
if (downloadProgress) {
var dp = { receivedBytes: progressEvent.loaded, totalBytes: progressEvent.total };
downloadProgress(dp);
}
};
this.currentFileTransfer.download(this.downloadUrl, cordova.file.dataDirectory + LocalPackage.DownloadDir + "/" + LocalPackage.PackageUpdateFileName, downloadSuccess, downloadError, true); this.currentFileTransfer.download(this.downloadUrl, cordova.file.dataDirectory + LocalPackage.DownloadDir + "/" + LocalPackage.PackageUpdateFileName, downloadSuccess, downloadError, true);
} }
} }

Просмотреть файл

@ -13,7 +13,7 @@
var SyncStatus; var SyncStatus;
(function (SyncStatus) { (function (SyncStatus) {
SyncStatus[SyncStatus["UP_TO_DATE"] = 0] = "UP_TO_DATE"; SyncStatus[SyncStatus["UP_TO_DATE"] = 0] = "UP_TO_DATE";
SyncStatus[SyncStatus["APPLY_SUCCESS"] = 1] = "APPLY_SUCCESS"; SyncStatus[SyncStatus["UPDATE_INSTALLED"] = 1] = "UPDATE_INSTALLED";
SyncStatus[SyncStatus["UPDATE_IGNORED"] = 2] = "UPDATE_IGNORED"; SyncStatus[SyncStatus["UPDATE_IGNORED"] = 2] = "UPDATE_IGNORED";
SyncStatus[SyncStatus["ERROR"] = 3] = "ERROR"; SyncStatus[SyncStatus["ERROR"] = 3] = "ERROR";
})(SyncStatus || (SyncStatus = {})); })(SyncStatus || (SyncStatus = {}));

Просмотреть файл

@ -37,7 +37,7 @@ var app = {
For customizing the sync behavior, see SyncOptions in the CodePush documentation. */ For customizing the sync behavior, see SyncOptions in the CodePush documentation. */
window.codePush.sync(function (syncStatus) { window.codePush.sync(function (syncStatus) {
switch (syncStatus) { switch (syncStatus) {
case SyncStatus.APPLY_SUCCESS: case SyncStatus.UPDATE_INSTALLED:
console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content."); console.log("The update was applied successfully. This is the last callback before the application is reloaded with the updated content.");
/* Don't continue app initialization, the application will refresh after this return. */ /* Don't continue app initialization, the application will refresh after this return. */
return; return;

Просмотреть файл

@ -24,7 +24,7 @@ export class TestMessage {
public static CHECK_ERROR = "CHECK_ERROR"; public static CHECK_ERROR = "CHECK_ERROR";
public static DOWNLOAD_SUCCEEDED = "DOWNLOAD_SUCCEEDED"; public static DOWNLOAD_SUCCEEDED = "DOWNLOAD_SUCCEEDED";
public static DOWNLOAD_ERROR = "DOWNLOAD_ERROR"; public static DOWNLOAD_ERROR = "DOWNLOAD_ERROR";
public static APPLY_SUCCESS = "APPLY_SUCCESS"; public static UPDATE_INSTALLED = "UPDATE_INSTALLED";
public static APPLY_ERROR = "APPLY_ERROR"; public static APPLY_ERROR = "APPLY_ERROR";
public static DEVICE_READY_AFTER_UPDATE = "DEVICE_READY_AFTER_UPDATE"; public static DEVICE_READY_AFTER_UPDATE = "DEVICE_READY_AFTER_UPDATE";
public static UPDATE_FAILED_PREVIOUSLY = "UPDATE_FAILED_PREVIOUSLY"; public static UPDATE_FAILED_PREVIOUSLY = "UPDATE_FAILED_PREVIOUSLY";
@ -34,7 +34,7 @@ export class TestMessage {
public static SYNC_STATUS = "SYNC_STATUS"; public static SYNC_STATUS = "SYNC_STATUS";
public static SYNC_UP_TO_DATE = 0; public static SYNC_UP_TO_DATE = 0;
public static SYNC_APPLY_SUCCESS = 1; public static SYNC_UPDATE_INSTALLED = 1;
public static SYNC_UPDATE_IGNORED = 2; public static SYNC_UPDATE_IGNORED = 2;
public static SYNC_ERROR = 3; public static SYNC_ERROR = 3;
} }

Просмотреть файл

@ -44,8 +44,8 @@ var app = {
app.sendTestMessage("DOWNLOAD_ERROR"); app.sendTestMessage("DOWNLOAD_ERROR");
}, },
applySuccess: function () { applySuccess: function () {
console.log("Apply success."); console.log("Update installed.");
app.sendTestMessage("APPLY_SUCCESS"); app.sendTestMessage("UPDATE_INSTALLED");
}, },
applyError: function (error) { applyError: function (error) {
console.log("Apply error."); console.log("Apply error.");

Просмотреть файл

@ -50,7 +50,7 @@ var app = {
}, },
applySuccess: function () { applySuccess: function () {
console.log("Apply success."); console.log("Apply success.");
app.sendTestMessage("APPLY_SUCCESS"); app.sendTestMessage("UPDATE_INSTALLED");
}, },
applyError: function (error) { applyError: function (error) {
console.log("Apply error."); console.log("Apply error.");

Просмотреть файл

@ -366,7 +366,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([su.TestMessage.APPLY_SUCCESS, su.TestMessage.DEVICE_READY_AFTER_UPDATE], deferred); testMessageCallback = verifyMessages([su.TestMessage.UPDATE_INSTALLED, su.TestMessage.DEVICE_READY_AFTER_UPDATE], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -395,7 +395,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([su.TestMessage.APPLY_SUCCESS, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.UPDATE_FAILED_PREVIOUSLY], deferred); testMessageCallback = verifyMessages([su.TestMessage.UPDATE_INSTALLED, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.UPDATE_FAILED_PREVIOUSLY], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -405,7 +405,7 @@ describe("window.codePush", function() {
console.log("Creating a second failed update."); console.log("Creating a second failed update.");
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockResponse = { updateInfo: getMockResponse(true) }; mockResponse = { updateInfo: getMockResponse(true) };
testMessageCallback = verifyMessages([su.TestMessage.APPLY_SUCCESS, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.UPDATE_FAILED_PREVIOUSLY], deferred); testMessageCallback = verifyMessages([su.TestMessage.UPDATE_INSTALLED, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.UPDATE_FAILED_PREVIOUSLY], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -423,7 +423,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([su.TestMessage.APPLY_SUCCESS, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.NOTIFY_APP_READY_SUCCESS, su.TestMessage.APPLICATION_NOT_REVERTED], deferred); testMessageCallback = verifyMessages([su.TestMessage.UPDATE_INSTALLED, su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.NOTIFY_APP_READY_SUCCESS, su.TestMessage.APPLICATION_NOT_REVERTED], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -505,7 +505,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_APPLY_SUCCESS]), su.TestMessage.DEVICE_READY_AFTER_UPDATE], deferred); testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_UPDATE_INSTALLED]), su.TestMessage.DEVICE_READY_AFTER_UPDATE], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -534,7 +534,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_APPLY_SUCCESS]), su.TestMessage.DEVICE_READY_AFTER_UPDATE, new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_UP_TO_DATE])], deferred); testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_UPDATE_INSTALLED]), su.TestMessage.DEVICE_READY_AFTER_UPDATE, new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_UP_TO_DATE])], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;
@ -552,7 +552,7 @@ describe("window.codePush", function() {
.then<void>((updatePath: string) => { .then<void>((updatePath: string) => {
var deferred = Q.defer<void>(); var deferred = Q.defer<void>();
mockUpdatePackagePath = updatePath; mockUpdatePackagePath = updatePath;
testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_APPLY_SUCCESS]), su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.APPLICATION_NOT_REVERTED], deferred); testMessageCallback = verifyMessages([new su.AppMessage(su.TestMessage.SYNC_STATUS, [su.TestMessage.SYNC_UPDATE_INSTALLED]), su.TestMessage.DEVICE_READY_AFTER_UPDATE, su.TestMessage.APPLICATION_NOT_REVERTED], deferred);
console.log("Running project..."); console.log("Running project...");
projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator); projectManager.runPlatform(testRunDirectory, targetPlatform, true, targetEmulator);
return deferred.promise; return deferred.promise;

15
typings/codePush.d.ts поставляемый
Просмотреть файл

@ -56,8 +56,9 @@ interface IRemotePackage extends IPackage {
* *
* @param downloadSuccess Called with one parameter, the downloaded package information, once the download completed successfully. * @param downloadSuccess Called with one parameter, the downloaded package information, once the download completed successfully.
* @param downloadError Optional callback invoked in case of an error. * @param downloadError Optional callback invoked in case of an error.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
*/ */
download(downloadSuccess: SuccessCallback<ILocalPackage>, downloadError?: ErrorCallback): void; download(downloadSuccess: SuccessCallback<ILocalPackage>, downloadError?: ErrorCallback, downloadProgress?: SuccessCallback<DownloadProgress>): void;
/** /**
* Aborts the current download session, previously started with download(). * Aborts the current download session, previously started with download().
@ -216,9 +217,9 @@ declare enum SyncStatus {
/** /**
* An update is available, it has been downloaded, unzipped and copied to the deployment folder. * An update is available, it has been downloaded, unzipped and copied to the deployment folder.
* After the completion of the callback invoked with SyncStatus.APPLY_SUCCESS, the application will be reloaded with the updated code and resources. * After the completion of the callback invoked with SyncStatus.UPDATE_INSTALLED, the application will be reloaded with the updated code and resources.
*/ */
APPLY_SUCCESS, UPDATE_INSTALLED,
/** /**
* An optional update is available, but the user declined to install it. The update was not downloaded. * An optional update is available, but the user declined to install it. The update was not downloaded.
@ -296,4 +297,12 @@ interface SyncOptions {
*/ */
interface IDiffManifest { interface IDiffManifest {
deletedFiles: string[]; deletedFiles: string[];
}
/**
* Defines the format of the DownloadProgress object, used to send periodical update notifications on the progress of the update download.
*/
interface DownloadProgress {
totalBytes: number;
receivedBytes: number;
} }

Просмотреть файл

@ -169,7 +169,7 @@ class CodePush implements CodePushCordovaPlugin {
}; };
var onApplySuccess = () => { var onApplySuccess = () => {
syncCallback && syncCallback(SyncStatus.APPLY_SUCCESS); syncCallback && syncCallback(SyncStatus.UPDATE_INSTALLED);
}; };
var onDownloadSuccess = (localPackage: ILocalPackage) => { var onDownloadSuccess = (localPackage: ILocalPackage) => {

Просмотреть файл

@ -27,8 +27,9 @@ class RemotePackage extends Package implements IRemotePackage {
* *
* @param downloadSuccess Called with one parameter, the downloaded package information, once the download completed successfully. * @param downloadSuccess Called with one parameter, the downloaded package information, once the download completed successfully.
* @param downloadError Optional callback invoked in case of an error. * @param downloadError Optional callback invoked in case of an error.
* @param downloadProgress Optional callback invoked during the download process. It is called several times with one DownloadProgress parameter.
*/ */
public download(successCallback: SuccessCallback<LocalPackage>, errorCallback?: ErrorCallback): void { public download(successCallback: SuccessCallback<LocalPackage>, errorCallback?: ErrorCallback, downloadProgress?: SuccessCallback<DownloadProgress>): void {
try { try {
CodePushUtil.logMessage("Downloading update package ..."); CodePushUtil.logMessage("Downloading update package ...");
if (!this.downloadUrl) { if (!this.downloadUrl) {
@ -65,6 +66,13 @@ class RemotePackage extends Package implements IRemotePackage {
this.currentFileTransfer = null; this.currentFileTransfer = null;
CodePushUtil.invokeErrorCallback(new Error(error.body), errorCallback); CodePushUtil.invokeErrorCallback(new Error(error.body), errorCallback);
}; };
this.currentFileTransfer.onprogress = (progressEvent: ProgressEvent) => {
if (downloadProgress) {
var dp: DownloadProgress = { receivedBytes: progressEvent.loaded, totalBytes: progressEvent.total };
downloadProgress(dp);
}
};
this.currentFileTransfer.download(this.downloadUrl, cordova.file.dataDirectory + LocalPackage.DownloadDir + "/" + LocalPackage.PackageUpdateFileName, downloadSuccess, downloadError, true); this.currentFileTransfer.download(this.downloadUrl, cordova.file.dataDirectory + LocalPackage.DownloadDir + "/" + LocalPackage.PackageUpdateFileName, downloadSuccess, downloadError, true);
} }

Просмотреть файл

@ -13,9 +13,9 @@ enum SyncStatus {
/** /**
* An update is available, it has been downloaded, unzipped and copied to the deployment folder. * An update is available, it has been downloaded, unzipped and copied to the deployment folder.
* After the completion of the callback invoked with SyncStatus.APPLY_SUCCESS, the application will be reloaded with the updated code and resources. * After the completion of the callback invoked with SyncStatus.UPDATE_INSTALLED, the application will be reloaded with the updated code and resources.
*/ */
APPLY_SUCCESS, UPDATE_INSTALLED,
/** /**
* An optional update is available, but the user declined to install it. The update was not downloaded. * An optional update is available, but the user declined to install it. The update was not downloaded.