Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions CodePush.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,22 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
}
};

let remotePackageLabel;
try {
await CodePush.notifyApplicationReady();

syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
const remotePackage = await checkForUpdate(handleBinaryVersionMismatchCallback);
remotePackageLabel = remotePackage.label;

const doDownloadAndInstall = async () => {
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
sharedCodePushOptions.onDownloadStart?.(remotePackageLabel);

const localPackage = await remotePackage.download(downloadProgressCallback);

sharedCodePushOptions.onDownloadSuccess?.(remotePackageLabel);

// Determine the correct install mode based on whether the update is mandatory or not.
resolvedInstallMode = localPackage.isMandatory ? syncOptions.mandatoryInstallMode : syncOptions.installMode;

Expand Down Expand Up @@ -558,6 +564,7 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
}
} catch (error) {
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
sharedCodePushOptions?.onSyncError(remotePackageLabel ?? 'unknown', error);
log(error.message);
throw error;
}
Expand All @@ -584,12 +591,24 @@ let CodePush;
* @type {{
* releaseHistoryFetcher: releaseHistoryFetcher | undefined,
* setReleaseHistoryFetcher(releaseHistoryFetcherFunction: releaseHistoryFetcher | undefined): void,
*
* updateChecker: updateChecker | undefined,
* setUpdateChecker(updateCheckerFunction: updateChecker | undefined): void,
*
* onUpdateSuccess: (label: string) => void | undefined,
* setOnUpdateSuccess(onUpdateSuccessFunction: (label: string) => void | undefined): void,
*
* onUpdateRollback: (label: string) => void | undefined,
* setOnUpdateRollback(onUpdateRollbackFunction: (label: string) => void | undefined): void,
*
* onDownloadStart: (label: string) => void | undefined,
* setOnDownloadStart(onDownloadStartFunction: (label: string) => void | undefined): void,
*
* onDownloadSuccess: (label: string) => void | undefined,
* setOnDownloadSuccess(onDownloadSuccessFunction: (label: string) => void | undefined): void,
*
* onSyncError: (label: string, error: Error) => void | undefined,
* setOnSyncError(onSyncErrorFunction: (label: string, error: Error) => void | undefined): void,
* }}
*/
const sharedCodePushOptions = {
Expand All @@ -616,6 +635,24 @@ const sharedCodePushOptions = {
if (typeof onUpdateRollbackFunction !== 'function') throw new Error('Please pass a function to onUpdateRollback');
this.onUpdateRollback = onUpdateRollbackFunction;
},
onDownloadStart: undefined,
setOnDownloadStart(onDownloadStartFunction) {
if (!onDownloadStartFunction) return;
if (typeof onDownloadStartFunction !== 'function') throw new Error('Please pass a function to onDownloadStart');
this.onDownloadStart = onDownloadStartFunction;
},
onDownloadSuccess: undefined,
setOnDownloadSuccess(onDownloadSuccessFunction) {
if (!onDownloadSuccessFunction) return;
if (typeof onDownloadSuccessFunction !== 'function') throw new Error('Please pass a function to onDownloadSuccess');
this.onDownloadSuccess = onDownloadSuccessFunction;
},
onSyncError: undefined,
setOnSyncError(onSyncErrorFunction) {
if (!onSyncErrorFunction) return;
if (typeof onSyncErrorFunction !== 'function') throw new Error('Please pass a function to onSyncError');
this.onSyncError = onSyncErrorFunction;
},
}

function codePushify(options = {}) {
Expand Down Expand Up @@ -648,6 +685,9 @@ function codePushify(options = {}) {
// set telemetry callbacks
sharedCodePushOptions.setOnUpdateSuccess(options.onUpdateSuccess);
sharedCodePushOptions.setOnUpdateRollback(options.onUpdateRollback);
sharedCodePushOptions.setOnDownloadStart(options.onDownloadStart);
sharedCodePushOptions.setOnDownloadSuccess(options.onDownloadSuccess);
sharedCodePushOptions.setOnSyncError(options.onSyncError);

const decorator = (RootComponent) => {
class CodePushComponent extends React.Component {
Expand Down
12 changes: 12 additions & 0 deletions typings/react-native-code-push.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export interface CodePushOptions extends SyncOptions {
* Callback function that is called when the update rolled back.
*/
onUpdateRollback?: (label: string) => void;
/**
* Callback function that is called when download starts.
*/
onDownloadStart?: (label: string) => void;
/**
* Callback function that is called when download finished successfully.
*/
onDownloadSuccess?: (label: string) => void;
/**
* Callback function that is called when sync process failed.
*/
onSyncError?: (label: string, error: Error) => void;
}

export interface DownloadProgress {
Expand Down