Skip to content

Commit a7e4ce5

Browse files
authored
Merge pull request #58 from Soomgo-Mobile/v9-telemetry
feat(Runtime): add download start, success, and sync error callbacks
2 parents f44da7e + bdad45f commit a7e4ce5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

CodePush.js

+40
Original file line numberDiff line numberDiff line change
@@ -466,16 +466,22 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
466466
}
467467
};
468468

469+
let remotePackageLabel;
469470
try {
470471
await CodePush.notifyApplicationReady();
471472

472473
syncStatusChangeCallback(CodePush.SyncStatus.CHECKING_FOR_UPDATE);
473474
const remotePackage = await checkForUpdate(handleBinaryVersionMismatchCallback);
475+
remotePackageLabel = remotePackage.label;
474476

475477
const doDownloadAndInstall = async () => {
476478
syncStatusChangeCallback(CodePush.SyncStatus.DOWNLOADING_PACKAGE);
479+
sharedCodePushOptions.onDownloadStart?.(remotePackageLabel);
480+
477481
const localPackage = await remotePackage.download(downloadProgressCallback);
478482

483+
sharedCodePushOptions.onDownloadSuccess?.(remotePackageLabel);
484+
479485
// Determine the correct install mode based on whether the update is mandatory or not.
480486
resolvedInstallMode = localPackage.isMandatory ? syncOptions.mandatoryInstallMode : syncOptions.installMode;
481487

@@ -558,6 +564,7 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
558564
}
559565
} catch (error) {
560566
syncStatusChangeCallback(CodePush.SyncStatus.UNKNOWN_ERROR);
567+
sharedCodePushOptions?.onSyncError(remotePackageLabel ?? 'unknown', error);
561568
log(error.message);
562569
throw error;
563570
}
@@ -584,12 +591,24 @@ let CodePush;
584591
* @type {{
585592
* releaseHistoryFetcher: releaseHistoryFetcher | undefined,
586593
* setReleaseHistoryFetcher(releaseHistoryFetcherFunction: releaseHistoryFetcher | undefined): void,
594+
*
587595
* updateChecker: updateChecker | undefined,
588596
* setUpdateChecker(updateCheckerFunction: updateChecker | undefined): void,
597+
*
589598
* onUpdateSuccess: (label: string) => void | undefined,
590599
* setOnUpdateSuccess(onUpdateSuccessFunction: (label: string) => void | undefined): void,
600+
*
591601
* onUpdateRollback: (label: string) => void | undefined,
592602
* setOnUpdateRollback(onUpdateRollbackFunction: (label: string) => void | undefined): void,
603+
*
604+
* onDownloadStart: (label: string) => void | undefined,
605+
* setOnDownloadStart(onDownloadStartFunction: (label: string) => void | undefined): void,
606+
*
607+
* onDownloadSuccess: (label: string) => void | undefined,
608+
* setOnDownloadSuccess(onDownloadSuccessFunction: (label: string) => void | undefined): void,
609+
*
610+
* onSyncError: (label: string, error: Error) => void | undefined,
611+
* setOnSyncError(onSyncErrorFunction: (label: string, error: Error) => void | undefined): void,
593612
* }}
594613
*/
595614
const sharedCodePushOptions = {
@@ -616,6 +635,24 @@ const sharedCodePushOptions = {
616635
if (typeof onUpdateRollbackFunction !== 'function') throw new Error('Please pass a function to onUpdateRollback');
617636
this.onUpdateRollback = onUpdateRollbackFunction;
618637
},
638+
onDownloadStart: undefined,
639+
setOnDownloadStart(onDownloadStartFunction) {
640+
if (!onDownloadStartFunction) return;
641+
if (typeof onDownloadStartFunction !== 'function') throw new Error('Please pass a function to onDownloadStart');
642+
this.onDownloadStart = onDownloadStartFunction;
643+
},
644+
onDownloadSuccess: undefined,
645+
setOnDownloadSuccess(onDownloadSuccessFunction) {
646+
if (!onDownloadSuccessFunction) return;
647+
if (typeof onDownloadSuccessFunction !== 'function') throw new Error('Please pass a function to onDownloadSuccess');
648+
this.onDownloadSuccess = onDownloadSuccessFunction;
649+
},
650+
onSyncError: undefined,
651+
setOnSyncError(onSyncErrorFunction) {
652+
if (!onSyncErrorFunction) return;
653+
if (typeof onSyncErrorFunction !== 'function') throw new Error('Please pass a function to onSyncError');
654+
this.onSyncError = onSyncErrorFunction;
655+
},
619656
}
620657

621658
function codePushify(options = {}) {
@@ -648,6 +685,9 @@ function codePushify(options = {}) {
648685
// set telemetry callbacks
649686
sharedCodePushOptions.setOnUpdateSuccess(options.onUpdateSuccess);
650687
sharedCodePushOptions.setOnUpdateRollback(options.onUpdateRollback);
688+
sharedCodePushOptions.setOnDownloadStart(options.onDownloadStart);
689+
sharedCodePushOptions.setOnDownloadSuccess(options.onDownloadSuccess);
690+
sharedCodePushOptions.setOnSyncError(options.onSyncError);
651691

652692
const decorator = (RootComponent) => {
653693
class CodePushComponent extends React.Component {

typings/react-native-code-push.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ export interface CodePushOptions extends SyncOptions {
8181
* Callback function that is called when the update rolled back.
8282
*/
8383
onUpdateRollback?: (label: string) => void;
84+
/**
85+
* Callback function that is called when download starts.
86+
*/
87+
onDownloadStart?: (label: string) => void;
88+
/**
89+
* Callback function that is called when download finished successfully.
90+
*/
91+
onDownloadSuccess?: (label: string) => void;
92+
/**
93+
* Callback function that is called when sync process failed.
94+
*/
95+
onSyncError?: (label: string, error: Error) => void;
8496
}
8597

8698
export interface DownloadProgress {

0 commit comments

Comments
 (0)