From 4a7bb19ca129a99a8c666649f333b34fdd92251e Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Fri, 11 Apr 2025 18:14:57 +0800 Subject: [PATCH] do not use static properties --- src/client.ts | 125 +++++++++++++++++++++++++++----------------------- src/utils.ts | 3 +- 2 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/client.ts b/src/client.ts index fa431df..24e57e1 100644 --- a/src/client.ts +++ b/src/client.ts @@ -61,6 +61,31 @@ const defaultClientOptions: ClientOptions = { throwError: false, }; +const sharedState: { + progressHandlers: Record; + downloadedHash?: string; + apkStatus: 'downloading' | 'downloaded' | null; + marked: boolean; + applyingUpdate: boolean; +} = { + progressHandlers: {}, + downloadedHash: undefined, + apkStatus: null, + marked: false, + applyingUpdate: false, +}; + +const assertHash = (hash: string) => { + if (!sharedState.downloadedHash) { + return; + } + if (hash !== sharedState.downloadedHash) { + log(`use downloaded hash ${sharedState.downloadedHash} first`); + return; + } + return true; +}; + // for China users export class Pushy { options = defaultClientOptions; @@ -68,13 +93,6 @@ export class Pushy { lastChecking?: number; lastRespJson?: Promise; - static progressHandlers: Record = {}; - static downloadedHash?: string; - - static apkStatus: 'downloading' | 'downloaded' | null = null; - - static marked = false; - static applyingUpdate = false; version = cInfo.rnu; loggerPromise = (() => { let resolve: (value?: unknown) => void = () => {}; @@ -152,17 +170,6 @@ export class Pushy { getCheckUrl = (endpoint: string = this.options.server!.main) => { return `${endpoint}/checkUpdate/${this.options.appKey}`; }; - static assertHash = (hash: string) => { - if (!Pushy.downloadedHash) { - log('no downloaded hash'); - return; - } - if (hash !== Pushy.downloadedHash) { - log(`use downloaded hash ${Pushy.downloadedHash} first`); - return; - } - return true; - }; assertDebug = () => { if (__DEV__ && !this.options.debug) { console.info( @@ -173,10 +180,10 @@ export class Pushy { return true; }; markSuccess = () => { - if (Pushy.marked || __DEV__ || !isFirstTime) { + if (sharedState.marked || __DEV__ || !isFirstTime) { return; } - Pushy.marked = true; + sharedState.marked = true; PushyModule.markSuccess(); this.report({ type: 'markSuccess' }); }; @@ -184,9 +191,9 @@ export class Pushy { if (!assertDev('switchVersion()')) { return; } - if (Pushy.assertHash(hash) && !Pushy.applyingUpdate) { + if (assertHash(hash) && !sharedState.applyingUpdate) { log('switchVersion: ' + hash); - Pushy.applyingUpdate = true; + sharedState.applyingUpdate = true; return PushyModule.reloadUpdate({ hash }); } }; @@ -195,7 +202,7 @@ export class Pushy { if (!assertDev('switchVersionLater()')) { return; } - if (Pushy.assertHash(hash)) { + if (assertHash(hash)) { log('switchVersionLater: ' + hash); return PushyModule.setNeedUpdate({ hash }); } @@ -350,18 +357,18 @@ export class Pushy { log(`rolledback hash ${rolledBackVersion}, ignored`); return; } - if (Pushy.downloadedHash === hash) { - log(`duplicated downloaded hash ${Pushy.downloadedHash}, ignored`); - return Pushy.downloadedHash; + if (sharedState.downloadedHash === hash) { + log(`duplicated downloaded hash ${sharedState.downloadedHash}, ignored`); + return sharedState.downloadedHash; } - if (Pushy.progressHandlers[hash]) { + if (sharedState.progressHandlers[hash]) { return; } const patchStartTime = Date.now(); if (onDownloadProgress) { // @ts-expect-error harmony not in existing platforms if (Platform.OS === 'harmony') { - Pushy.progressHandlers[hash] = DeviceEventEmitter.addListener( + sharedState.progressHandlers[hash] = DeviceEventEmitter.addListener( 'RCTPushyDownloadProgress', progressData => { if (progressData.hash === hash) { @@ -370,14 +377,15 @@ export class Pushy { }, ); } else { - Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener( - 'RCTPushyDownloadProgress', - progressData => { - if (progressData.hash === hash) { - onDownloadProgress(progressData); - } - }, - ); + sharedState.progressHandlers[hash] = + pushyNativeEventEmitter.addListener( + 'RCTPushyDownloadProgress', + progressData => { + if (progressData.hash === hash) { + onDownloadProgress(progressData); + } + }, + ); } } let succeeded = ''; @@ -445,9 +453,9 @@ export class Pushy { } } } - if (Pushy.progressHandlers[hash]) { - Pushy.progressHandlers[hash].remove(); - delete Pushy.progressHandlers[hash]; + if (sharedState.progressHandlers[hash]) { + sharedState.progressHandlers[hash].remove(); + delete sharedState.progressHandlers[hash]; } if (__DEV__) { return hash; @@ -483,7 +491,7 @@ export class Pushy { description, metaInfo, }); - Pushy.downloadedHash = hash; + sharedState.downloadedHash = hash; return hash; }; downloadAndInstallApk = async ( @@ -493,10 +501,10 @@ export class Pushy { if (Platform.OS !== 'android') { return; } - if (Pushy.apkStatus === 'downloading') { + if (sharedState.apkStatus === 'downloading') { return; } - if (Pushy.apkStatus === 'downloaded') { + if (sharedState.apkStatus === 'downloaded') { this.report({ type: 'errorInstallApk' }); this.throwIfEnabled(new Error('errorInstallApk')); return; @@ -517,35 +525,36 @@ export class Pushy { return; } } - Pushy.apkStatus = 'downloading'; + sharedState.apkStatus = 'downloading'; this.report({ type: 'downloadingApk' }); const progressKey = 'downloadingApk'; if (onDownloadProgress) { - if (Pushy.progressHandlers[progressKey]) { - Pushy.progressHandlers[progressKey].remove(); + if (sharedState.progressHandlers[progressKey]) { + sharedState.progressHandlers[progressKey].remove(); } - Pushy.progressHandlers[progressKey] = pushyNativeEventEmitter.addListener( - 'RCTPushyDownloadProgress', - (progressData: ProgressData) => { - if (progressData.hash === progressKey) { - onDownloadProgress(progressData); - } - }, - ); + sharedState.progressHandlers[progressKey] = + pushyNativeEventEmitter.addListener( + 'RCTPushyDownloadProgress', + (progressData: ProgressData) => { + if (progressData.hash === progressKey) { + onDownloadProgress(progressData); + } + }, + ); } await PushyModule.downloadAndInstallApk({ url, target: 'update.apk', hash: progressKey, }).catch(() => { - Pushy.apkStatus = null; + sharedState.apkStatus = null; this.report({ type: 'errorDownloadAndInstallApk' }); this.throwIfEnabled(new Error('errorDownloadAndInstallApk')); }); - Pushy.apkStatus = 'downloaded'; - if (Pushy.progressHandlers[progressKey]) { - Pushy.progressHandlers[progressKey].remove(); - delete Pushy.progressHandlers[progressKey]; + sharedState.apkStatus = 'downloaded'; + if (sharedState.progressHandlers[progressKey]) { + sharedState.progressHandlers[progressKey].remove(); + delete sharedState.progressHandlers[progressKey]; } }; restartApp = async () => { diff --git a/src/utils.ts b/src/utils.ts index 648aceb..91c9d73 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -62,7 +62,7 @@ const ping = if (!pingFinished) { log('ping timeout', url); } - }, 2000), + }, 5000), ), ]); }; @@ -81,6 +81,7 @@ export const testUrls = async (urls?: string[]) => { try { const ret = await promiseAny(urls.map(ping)); if (ret) { + log('ping success, use url:', ret); return ret; } } catch {}