@@ -43,11 +43,13 @@ export class Pushy {
4343 lastChecking ?: number ;
4444 lastRespJson ?: Promise < any > ;
4545
46- progressHandlers : Record < string , EmitterSubscription > = { } ;
47- downloadedHash ?: string ;
46+ static progressHandlers : Record < string , EmitterSubscription > = { } ;
47+ static downloadedHash ?: string ;
4848
49- marked = false ;
50- applyingUpdate = false ;
49+ static apkStatus : 'downloading' | 'downloaded' | null = null ;
50+
51+ static marked = false ;
52+ static applyingUpdate = false ;
5153 version = cInfo . pushy ;
5254 loggerPromise = ( ( ) => {
5355 let resolve : ( value ?: unknown ) => void = ( ) => { } ;
@@ -121,21 +123,21 @@ export class Pushy {
121123 getCheckUrl = ( endpoint : string = this . options . server ! . main ) => {
122124 return `${ endpoint } /checkUpdate/${ this . options . appKey } ` ;
123125 } ;
124- assertHash = ( hash : string ) => {
125- if ( ! this . downloadedHash ) {
126+ static assertHash = ( hash : string ) => {
127+ if ( ! Pushy . downloadedHash ) {
126128 return ;
127129 }
128- if ( hash !== this . downloadedHash ) {
129- log ( `use downloaded hash ${ this . downloadedHash } first` ) ;
130+ if ( hash !== Pushy . downloadedHash ) {
131+ log ( `use downloaded hash ${ Pushy . downloadedHash } first` ) ;
130132 return ;
131133 }
132134 return true ;
133135 } ;
134136 markSuccess = ( ) => {
135- if ( this . marked || __DEV__ || ! isFirstTime ) {
137+ if ( Pushy . marked || __DEV__ || ! isFirstTime ) {
136138 return ;
137139 }
138- this . marked = true ;
140+ Pushy . marked = true ;
139141 PushyModule . markSuccess ( ) ;
140142 this . report ( { type : 'markSuccess' } ) ;
141143 } ;
@@ -146,9 +148,9 @@ export class Pushy {
146148 ) ;
147149 return ;
148150 }
149- if ( this . assertHash ( hash ) && ! this . applyingUpdate ) {
151+ if ( Pushy . assertHash ( hash ) && ! Pushy . applyingUpdate ) {
150152 log ( 'switchVersion: ' + hash ) ;
151- this . applyingUpdate = true ;
153+ Pushy . applyingUpdate = true ;
152154 return PushyModule . reloadUpdate ( { hash } ) ;
153155 }
154156 } ;
@@ -160,7 +162,7 @@ export class Pushy {
160162 ) ;
161163 return ;
162164 }
163- if ( this . assertHash ( hash ) ) {
165+ if ( Pushy . assertHash ( hash ) ) {
164166 log ( 'switchVersionLater: ' + hash ) ;
165167 return PushyModule . setNeedUpdate ( { hash } ) ;
166168 }
@@ -314,15 +316,15 @@ export class Pushy {
314316 log ( `rolledback hash ${ rolledBackVersion } , ignored` ) ;
315317 return ;
316318 }
317- if ( this . downloadedHash === hash ) {
318- log ( `duplicated downloaded hash ${ this . downloadedHash } , ignored` ) ;
319- return this . downloadedHash ;
319+ if ( Pushy . downloadedHash === hash ) {
320+ log ( `duplicated downloaded hash ${ Pushy . downloadedHash } , ignored` ) ;
321+ return Pushy . downloadedHash ;
320322 }
321- if ( this . progressHandlers [ hash ] ) {
323+ if ( Pushy . progressHandlers [ hash ] ) {
322324 return ;
323325 }
324326 if ( onDownloadProgress ) {
325- this . progressHandlers [ hash ] = pushyNativeEventEmitter . addListener (
327+ Pushy . progressHandlers [ hash ] = pushyNativeEventEmitter . addListener (
326328 'RCTPushyDownloadProgress' ,
327329 progressData => {
328330 if ( progressData . hash === hash ) {
@@ -389,9 +391,9 @@ export class Pushy {
389391 }
390392 }
391393 }
392- if ( this . progressHandlers [ hash ] ) {
393- this . progressHandlers [ hash ] . remove ( ) ;
394- delete this . progressHandlers [ hash ] ;
394+ if ( Pushy . progressHandlers [ hash ] ) {
395+ Pushy . progressHandlers [ hash ] . remove ( ) ;
396+ delete Pushy . progressHandlers [ hash ] ;
395397 }
396398 if ( __DEV__ ) {
397399 return hash ;
@@ -417,7 +419,7 @@ export class Pushy {
417419 description,
418420 metaInfo,
419421 } ) ;
420- this . downloadedHash = hash ;
422+ Pushy . downloadedHash = hash ;
421423 return hash ;
422424 } ;
423425 downloadAndInstallApk = async (
@@ -427,7 +429,14 @@ export class Pushy {
427429 if ( Platform . OS !== 'android' ) {
428430 return ;
429431 }
430- this . report ( { type : 'downloadingApk' } ) ;
432+ if ( Pushy . apkStatus === 'downloading' ) {
433+ return ;
434+ }
435+ if ( Pushy . apkStatus === 'downloaded' ) {
436+ this . report ( { type : 'errorInstallApk' } ) ;
437+ this . throwIfEnabled ( new Error ( 'errorInstallApk' ) ) ;
438+ return ;
439+ }
431440 if ( Platform . Version <= 23 ) {
432441 try {
433442 const granted = await PermissionsAndroid . request (
@@ -444,12 +453,14 @@ export class Pushy {
444453 return ;
445454 }
446455 }
456+ Pushy . apkStatus = 'downloading' ;
457+ this . report ( { type : 'downloadingApk' } ) ;
447458 const progressKey = 'downloadingApk' ;
448459 if ( onDownloadProgress ) {
449- if ( this . progressHandlers [ progressKey ] ) {
450- this . progressHandlers [ progressKey ] . remove ( ) ;
460+ if ( Pushy . progressHandlers [ progressKey ] ) {
461+ Pushy . progressHandlers [ progressKey ] . remove ( ) ;
451462 }
452- this . progressHandlers [ progressKey ] = pushyNativeEventEmitter . addListener (
463+ Pushy . progressHandlers [ progressKey ] = pushyNativeEventEmitter . addListener (
453464 'RCTPushyDownloadProgress' ,
454465 ( progressData : ProgressData ) => {
455466 if ( progressData . hash === progressKey ) {
@@ -463,12 +474,14 @@ export class Pushy {
463474 target : 'update.apk' ,
464475 hash : progressKey ,
465476 } ) . catch ( ( ) => {
477+ Pushy . apkStatus = null ;
466478 this . report ( { type : 'errorDownloadAndInstallApk' } ) ;
467479 this . throwIfEnabled ( new Error ( 'errorDownloadAndInstallApk' ) ) ;
468480 } ) ;
469- if ( this . progressHandlers [ progressKey ] ) {
470- this . progressHandlers [ progressKey ] . remove ( ) ;
471- delete this . progressHandlers [ progressKey ] ;
481+ Pushy . apkStatus = 'downloaded' ;
482+ if ( Pushy . progressHandlers [ progressKey ] ) {
483+ Pushy . progressHandlers [ progressKey ] . remove ( ) ;
484+ delete Pushy . progressHandlers [ progressKey ] ;
472485 }
473486 } ;
474487}
0 commit comments