11import type { Package } from 'code-push/script/acquisition-sdk' ;
22import { NativeEventEmitter } from 'react-native' ;
3- import type { DownloadProgressCallback , RemotePackage } from '../types' ;
3+ import type { DownloadProgressCallback , LocalPackage , RemotePackage } from '../types' ;
44import { LocalPackageImplementation } from './LocalPackageImplementation' ;
55import { NativeRNAppZungCodePushModule } from './NativeRNAppZungCodePushModule' ;
6- import { cloneWithoutFunctions } from './utils/cloneWithoutFunctions' ;
76import { log } from './utils/log' ;
87
98export class RemotePackageImpl implements RemotePackage {
109 constructor (
1110 remotePackageData : Omit < RemotePackage , 'download' > ,
12- private readonly reportStatusDownload ?: ( downloadedPackage : Package ) => Promise < void > ,
11+ reportStatusDownload ?: ( downloadedPackage : Package ) => Promise < void > ,
1312 ) {
1413 Object . assign ( this , remotePackageData ) ;
14+
15+ this . download = async ( downloadProgressCallback ?: DownloadProgressCallback ) => {
16+ if ( ! this . downloadUrl ) {
17+ throw new Error ( 'Cannot download an update without a download url' ) ;
18+ }
19+
20+ let downloadProgressSubscription ;
21+
22+ if ( downloadProgressCallback ) {
23+ const codePushEventEmitter = new NativeEventEmitter ( NativeRNAppZungCodePushModule ) ;
24+ // Use event subscription to obtain download progress.
25+ downloadProgressSubscription = codePushEventEmitter . addListener (
26+ 'CodePushDownloadProgress' ,
27+ downloadProgressCallback ,
28+ ) ;
29+ }
30+
31+ // Use the downloaded package info. Native code will save the package info
32+ // so that the client knows what the current package version is.
33+ try {
34+ const { ...updatePackageCopy } = remotePackageData ; // In dev mode, React Native deep freezes any object queued over the bridge
35+ const downloadedPackage = await NativeRNAppZungCodePushModule . downloadUpdate (
36+ updatePackageCopy ,
37+ ! ! downloadProgressCallback ,
38+ ) ;
39+
40+ if ( reportStatusDownload ) {
41+ reportStatusDownload ( {
42+ ...this ,
43+ deploymentKey : this . releaseChannelPublicId ,
44+ } ) . catch ( ( err ) => {
45+ log ( `Report download status failed: ${ err } ` ) ;
46+ } ) ;
47+ }
48+
49+ return new LocalPackageImplementation ( downloadedPackage ) ;
50+ } finally {
51+ downloadProgressSubscription && downloadProgressSubscription . remove ( ) ;
52+ }
53+ } ;
1554 }
1655
56+ download : ( downloadProgressCallback ?: DownloadProgressCallback ) => Promise < LocalPackage > ;
57+
1758 appVersion ! : string ;
1859 description ! : string ;
1960 downloadUrl ! : string ;
@@ -25,44 +66,4 @@ export class RemotePackageImpl implements RemotePackage {
2566 packageHash ! : string ;
2667 packageSize ! : number ;
2768 releaseChannelPublicId ! : string ;
28-
29- async download ( downloadProgressCallback : DownloadProgressCallback ) {
30- if ( ! this . downloadUrl ) {
31- throw new Error ( 'Cannot download an update without a download url' ) ;
32- }
33-
34- let downloadProgressSubscription ;
35-
36- if ( downloadProgressCallback ) {
37- const codePushEventEmitter = new NativeEventEmitter ( NativeRNAppZungCodePushModule ) ;
38- // Use event subscription to obtain download progress.
39- downloadProgressSubscription = codePushEventEmitter . addListener (
40- 'CodePushDownloadProgress' ,
41- downloadProgressCallback ,
42- ) ;
43- }
44-
45- // Use the downloaded package info. Native code will save the package info
46- // so that the client knows what the current package version is.
47- try {
48- const updatePackageCopy = cloneWithoutFunctions ( this ) ; // In dev mode, React Native deep freezes any object queued over the bridge
49- const downloadedPackage = await NativeRNAppZungCodePushModule . downloadUpdate (
50- updatePackageCopy ,
51- ! ! downloadProgressCallback ,
52- ) ;
53-
54- if ( this . reportStatusDownload ) {
55- this . reportStatusDownload ( {
56- ...this ,
57- deploymentKey : this . releaseChannelPublicId ,
58- } ) . catch ( ( err ) => {
59- log ( `Report download status failed: ${ err } ` ) ;
60- } ) ;
61- }
62-
63- return new LocalPackageImplementation ( downloadedPackage ) ;
64- } finally {
65- downloadProgressSubscription && downloadProgressSubscription . remove ( ) ;
66- }
67- }
6869}
0 commit comments