@@ -61,20 +61,38 @@ const defaultClientOptions: ClientOptions = {
6161 throwError : false ,
6262} ;
6363
64+ const sharedState : {
65+ progressHandlers : Record < string , EmitterSubscription > ;
66+ downloadedHash ?: string ;
67+ apkStatus : 'downloading' | 'downloaded' | null ;
68+ marked : boolean ;
69+ applyingUpdate : boolean ;
70+ } = {
71+ progressHandlers : { } ,
72+ downloadedHash : undefined ,
73+ apkStatus : null ,
74+ marked : false ,
75+ applyingUpdate : false ,
76+ } ;
77+
78+ const assertHash = ( hash : string ) => {
79+ if ( ! sharedState . downloadedHash ) {
80+ return ;
81+ }
82+ if ( hash !== sharedState . downloadedHash ) {
83+ log ( `use downloaded hash ${ sharedState . downloadedHash } first` ) ;
84+ return ;
85+ }
86+ return true ;
87+ } ;
88+
6489// for China users
6590export class Pushy {
6691 options = defaultClientOptions ;
6792 clientType : 'Pushy' | 'Cresc' = 'Pushy' ;
6893 lastChecking ?: number ;
6994 lastRespJson ?: Promise < any > ;
7095
71- static progressHandlers : Record < string , EmitterSubscription > = { } ;
72- static downloadedHash ?: string ;
73-
74- static apkStatus : 'downloading' | 'downloaded' | null = null ;
75-
76- static marked = false ;
77- static applyingUpdate = false ;
7896 version = cInfo . rnu ;
7997 loggerPromise = ( ( ) => {
8098 let resolve : ( value ?: unknown ) => void = ( ) => { } ;
@@ -152,17 +170,6 @@ export class Pushy {
152170 getCheckUrl = ( endpoint : string = this . options . server ! . main ) => {
153171 return `${ endpoint } /checkUpdate/${ this . options . appKey } ` ;
154172 } ;
155- static assertHash = ( hash : string ) => {
156- if ( ! Pushy . downloadedHash ) {
157- log ( 'no downloaded hash' ) ;
158- return ;
159- }
160- if ( hash !== Pushy . downloadedHash ) {
161- log ( `use downloaded hash ${ Pushy . downloadedHash } first` ) ;
162- return ;
163- }
164- return true ;
165- } ;
166173 assertDebug = ( ) => {
167174 if ( __DEV__ && ! this . options . debug ) {
168175 console . info (
@@ -173,20 +180,20 @@ export class Pushy {
173180 return true ;
174181 } ;
175182 markSuccess = ( ) => {
176- if ( Pushy . marked || __DEV__ || ! isFirstTime ) {
183+ if ( sharedState . marked || __DEV__ || ! isFirstTime ) {
177184 return ;
178185 }
179- Pushy . marked = true ;
186+ sharedState . marked = true ;
180187 PushyModule . markSuccess ( ) ;
181188 this . report ( { type : 'markSuccess' } ) ;
182189 } ;
183190 switchVersion = async ( hash : string ) => {
184191 if ( ! assertDev ( 'switchVersion()' ) ) {
185192 return ;
186193 }
187- if ( Pushy . assertHash ( hash ) && ! Pushy . applyingUpdate ) {
194+ if ( assertHash ( hash ) && ! sharedState . applyingUpdate ) {
188195 log ( 'switchVersion: ' + hash ) ;
189- Pushy . applyingUpdate = true ;
196+ sharedState . applyingUpdate = true ;
190197 return PushyModule . reloadUpdate ( { hash } ) ;
191198 }
192199 } ;
@@ -195,7 +202,7 @@ export class Pushy {
195202 if ( ! assertDev ( 'switchVersionLater()' ) ) {
196203 return ;
197204 }
198- if ( Pushy . assertHash ( hash ) ) {
205+ if ( assertHash ( hash ) ) {
199206 log ( 'switchVersionLater: ' + hash ) ;
200207 return PushyModule . setNeedUpdate ( { hash } ) ;
201208 }
@@ -350,18 +357,18 @@ export class Pushy {
350357 log ( `rolledback hash ${ rolledBackVersion } , ignored` ) ;
351358 return ;
352359 }
353- if ( Pushy . downloadedHash === hash ) {
354- log ( `duplicated downloaded hash ${ Pushy . downloadedHash } , ignored` ) ;
355- return Pushy . downloadedHash ;
360+ if ( sharedState . downloadedHash === hash ) {
361+ log ( `duplicated downloaded hash ${ sharedState . downloadedHash } , ignored` ) ;
362+ return sharedState . downloadedHash ;
356363 }
357- if ( Pushy . progressHandlers [ hash ] ) {
364+ if ( sharedState . progressHandlers [ hash ] ) {
358365 return ;
359366 }
360367 const patchStartTime = Date . now ( ) ;
361368 if ( onDownloadProgress ) {
362369 // @ts -expect-error harmony not in existing platforms
363370 if ( Platform . OS === 'harmony' ) {
364- Pushy . progressHandlers [ hash ] = DeviceEventEmitter . addListener (
371+ sharedState . progressHandlers [ hash ] = DeviceEventEmitter . addListener (
365372 'RCTPushyDownloadProgress' ,
366373 progressData => {
367374 if ( progressData . hash === hash ) {
@@ -370,14 +377,15 @@ export class Pushy {
370377 } ,
371378 ) ;
372379 } else {
373- Pushy . progressHandlers [ hash ] = pushyNativeEventEmitter . addListener (
374- 'RCTPushyDownloadProgress' ,
375- progressData => {
376- if ( progressData . hash === hash ) {
377- onDownloadProgress ( progressData ) ;
378- }
379- } ,
380- ) ;
380+ sharedState . progressHandlers [ hash ] =
381+ pushyNativeEventEmitter . addListener (
382+ 'RCTPushyDownloadProgress' ,
383+ progressData => {
384+ if ( progressData . hash === hash ) {
385+ onDownloadProgress ( progressData ) ;
386+ }
387+ } ,
388+ ) ;
381389 }
382390 }
383391 let succeeded = '' ;
@@ -445,9 +453,9 @@ export class Pushy {
445453 }
446454 }
447455 }
448- if ( Pushy . progressHandlers [ hash ] ) {
449- Pushy . progressHandlers [ hash ] . remove ( ) ;
450- delete Pushy . progressHandlers [ hash ] ;
456+ if ( sharedState . progressHandlers [ hash ] ) {
457+ sharedState . progressHandlers [ hash ] . remove ( ) ;
458+ delete sharedState . progressHandlers [ hash ] ;
451459 }
452460 if ( __DEV__ ) {
453461 return hash ;
@@ -483,7 +491,7 @@ export class Pushy {
483491 description,
484492 metaInfo,
485493 } ) ;
486- Pushy . downloadedHash = hash ;
494+ sharedState . downloadedHash = hash ;
487495 return hash ;
488496 } ;
489497 downloadAndInstallApk = async (
@@ -493,10 +501,10 @@ export class Pushy {
493501 if ( Platform . OS !== 'android' ) {
494502 return ;
495503 }
496- if ( Pushy . apkStatus === 'downloading' ) {
504+ if ( sharedState . apkStatus === 'downloading' ) {
497505 return ;
498506 }
499- if ( Pushy . apkStatus === 'downloaded' ) {
507+ if ( sharedState . apkStatus === 'downloaded' ) {
500508 this . report ( { type : 'errorInstallApk' } ) ;
501509 this . throwIfEnabled ( new Error ( 'errorInstallApk' ) ) ;
502510 return ;
@@ -517,35 +525,36 @@ export class Pushy {
517525 return ;
518526 }
519527 }
520- Pushy . apkStatus = 'downloading' ;
528+ sharedState . apkStatus = 'downloading' ;
521529 this . report ( { type : 'downloadingApk' } ) ;
522530 const progressKey = 'downloadingApk' ;
523531 if ( onDownloadProgress ) {
524- if ( Pushy . progressHandlers [ progressKey ] ) {
525- Pushy . progressHandlers [ progressKey ] . remove ( ) ;
532+ if ( sharedState . progressHandlers [ progressKey ] ) {
533+ sharedState . progressHandlers [ progressKey ] . remove ( ) ;
526534 }
527- Pushy . progressHandlers [ progressKey ] = pushyNativeEventEmitter . addListener (
528- 'RCTPushyDownloadProgress' ,
529- ( progressData : ProgressData ) => {
530- if ( progressData . hash === progressKey ) {
531- onDownloadProgress ( progressData ) ;
532- }
533- } ,
534- ) ;
535+ sharedState . progressHandlers [ progressKey ] =
536+ pushyNativeEventEmitter . addListener (
537+ 'RCTPushyDownloadProgress' ,
538+ ( progressData : ProgressData ) => {
539+ if ( progressData . hash === progressKey ) {
540+ onDownloadProgress ( progressData ) ;
541+ }
542+ } ,
543+ ) ;
535544 }
536545 await PushyModule . downloadAndInstallApk ( {
537546 url,
538547 target : 'update.apk' ,
539548 hash : progressKey ,
540549 } ) . catch ( ( ) => {
541- Pushy . apkStatus = null ;
550+ sharedState . apkStatus = null ;
542551 this . report ( { type : 'errorDownloadAndInstallApk' } ) ;
543552 this . throwIfEnabled ( new Error ( 'errorDownloadAndInstallApk' ) ) ;
544553 } ) ;
545- Pushy . apkStatus = 'downloaded' ;
546- if ( Pushy . progressHandlers [ progressKey ] ) {
547- Pushy . progressHandlers [ progressKey ] . remove ( ) ;
548- delete Pushy . progressHandlers [ progressKey ] ;
554+ sharedState . apkStatus = 'downloaded' ;
555+ if ( sharedState . progressHandlers [ progressKey ] ) {
556+ sharedState . progressHandlers [ progressKey ] . remove ( ) ;
557+ delete sharedState . progressHandlers [ progressKey ] ;
549558 }
550559 } ;
551560 restartApp = async ( ) => {
0 commit comments