From d4f21a39f5318a9536e4ba287dfd7f8ba1e40500 Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Tue, 26 Nov 2024 00:53:11 +0800 Subject: [PATCH] v10.17.0 --- package.json | 5 ++-- src/client.ts | 71 ++++++++++++++++++++++++++++++--------------------- src/type.ts | 3 ++- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 3d6693f..6854f91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "10.16.0", + "version": "10.17.0", "description": "react-native hot update", "main": "src/index", "scripts": { @@ -74,5 +74,6 @@ "react-native": "0.73", "ts-jest": "^29.2.5", "typescript": "^5.6.3" - } + }, + "packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72" } diff --git a/src/client.ts b/src/client.ts index 37065d7..cbd1c16 100644 --- a/src/client.ts +++ b/src/client.ts @@ -43,11 +43,13 @@ export class Pushy { lastChecking?: number; lastRespJson?: Promise; - progressHandlers: Record = {}; - downloadedHash?: string; + static progressHandlers: Record = {}; + static downloadedHash?: string; - marked = false; - applyingUpdate = false; + static apkStatus: 'downloading' | 'downloaded' | null = null; + + static marked = false; + static applyingUpdate = false; version = cInfo.pushy; loggerPromise = (() => { let resolve: (value?: unknown) => void = () => {}; @@ -121,21 +123,21 @@ export class Pushy { getCheckUrl = (endpoint: string = this.options.server!.main) => { return `${endpoint}/checkUpdate/${this.options.appKey}`; }; - assertHash = (hash: string) => { - if (!this.downloadedHash) { + static assertHash = (hash: string) => { + if (!Pushy.downloadedHash) { return; } - if (hash !== this.downloadedHash) { - log(`use downloaded hash ${this.downloadedHash} first`); + if (hash !== Pushy.downloadedHash) { + log(`use downloaded hash ${Pushy.downloadedHash} first`); return; } return true; }; markSuccess = () => { - if (this.marked || __DEV__ || !isFirstTime) { + if (Pushy.marked || __DEV__ || !isFirstTime) { return; } - this.marked = true; + Pushy.marked = true; PushyModule.markSuccess(); this.report({ type: 'markSuccess' }); }; @@ -146,9 +148,9 @@ export class Pushy { ); return; } - if (this.assertHash(hash) && !this.applyingUpdate) { + if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) { log('switchVersion: ' + hash); - this.applyingUpdate = true; + Pushy.applyingUpdate = true; return PushyModule.reloadUpdate({ hash }); } }; @@ -160,7 +162,7 @@ export class Pushy { ); return; } - if (this.assertHash(hash)) { + if (Pushy.assertHash(hash)) { log('switchVersionLater: ' + hash); return PushyModule.setNeedUpdate({ hash }); } @@ -314,15 +316,15 @@ export class Pushy { log(`rolledback hash ${rolledBackVersion}, ignored`); return; } - if (this.downloadedHash === hash) { - log(`duplicated downloaded hash ${this.downloadedHash}, ignored`); - return this.downloadedHash; + if (Pushy.downloadedHash === hash) { + log(`duplicated downloaded hash ${Pushy.downloadedHash}, ignored`); + return Pushy.downloadedHash; } - if (this.progressHandlers[hash]) { + if (Pushy.progressHandlers[hash]) { return; } if (onDownloadProgress) { - this.progressHandlers[hash] = pushyNativeEventEmitter.addListener( + Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener( 'RCTPushyDownloadProgress', progressData => { if (progressData.hash === hash) { @@ -389,9 +391,9 @@ export class Pushy { } } } - if (this.progressHandlers[hash]) { - this.progressHandlers[hash].remove(); - delete this.progressHandlers[hash]; + if (Pushy.progressHandlers[hash]) { + Pushy.progressHandlers[hash].remove(); + delete Pushy.progressHandlers[hash]; } if (__DEV__) { return hash; @@ -417,7 +419,7 @@ export class Pushy { description, metaInfo, }); - this.downloadedHash = hash; + Pushy.downloadedHash = hash; return hash; }; downloadAndInstallApk = async ( @@ -427,7 +429,14 @@ export class Pushy { if (Platform.OS !== 'android') { return; } - this.report({ type: 'downloadingApk' }); + if (Pushy.apkStatus === 'downloading') { + return; + } + if (Pushy.apkStatus === 'downloaded') { + this.report({ type: 'errorInstallApk' }); + this.throwIfEnabled(new Error('errorInstallApk')); + return; + } if (Platform.Version <= 23) { try { const granted = await PermissionsAndroid.request( @@ -444,12 +453,14 @@ export class Pushy { return; } } + Pushy.apkStatus = 'downloading'; + this.report({ type: 'downloadingApk' }); const progressKey = 'downloadingApk'; if (onDownloadProgress) { - if (this.progressHandlers[progressKey]) { - this.progressHandlers[progressKey].remove(); + if (Pushy.progressHandlers[progressKey]) { + Pushy.progressHandlers[progressKey].remove(); } - this.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener( + Pushy.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener( 'RCTPushyDownloadProgress', (progressData: ProgressData) => { if (progressData.hash === progressKey) { @@ -463,12 +474,14 @@ export class Pushy { target: 'update.apk', hash: progressKey, }).catch(() => { + Pushy.apkStatus = null; this.report({ type: 'errorDownloadAndInstallApk' }); this.throwIfEnabled(new Error('errorDownloadAndInstallApk')); }); - if (this.progressHandlers[progressKey]) { - this.progressHandlers[progressKey].remove(); - delete this.progressHandlers[progressKey]; + Pushy.apkStatus = 'downloaded'; + if (Pushy.progressHandlers[progressKey]) { + Pushy.progressHandlers[progressKey].remove(); + delete Pushy.progressHandlers[progressKey]; } }; } diff --git a/src/type.ts b/src/type.ts index 466cddc..3cd3bf6 100644 --- a/src/type.ts +++ b/src/type.ts @@ -38,7 +38,8 @@ export type EventType = | 'downloadingApk' | 'rejectStoragePermission' | 'errorStoragePermission' - | 'errorDownloadAndInstallApk'; + | 'errorDownloadAndInstallApk' + | 'errorInstallApk'; export interface EventData { currentVersion: string;