From e63fa0fdb675b8019bbfbc2c31ee31723bb9ae4d Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Tue, 11 Jun 2024 10:44:18 +0800 Subject: [PATCH] v10.6.0-beta.0 --- Example/testHotUpdate/package.json | 2 +- Example/testHotUpdate/yarn.lock | 8 +++---- package.json | 2 +- src/client.ts | 6 ++--- src/provider.tsx | 37 +++++++++++++++++++++++++----- src/type.ts | 4 ++-- src/utils.ts | 12 +++++----- 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/Example/testHotUpdate/package.json b/Example/testHotUpdate/package.json index c0dddf2..63fdf80 100644 --- a/Example/testHotUpdate/package.json +++ b/Example/testHotUpdate/package.json @@ -18,7 +18,7 @@ "react-native": "0.69.8", "react-native-paper": "^5.12.1", "react-native-safe-area-context": "^4.8.2", - "react-native-update": "^10.5.3", + "react-native-update": "^10.5.4", "react-native-vector-icons": "^10.0.3" }, "devDependencies": { diff --git a/Example/testHotUpdate/yarn.lock b/Example/testHotUpdate/yarn.lock index 21a5e8e..06941dd 100644 --- a/Example/testHotUpdate/yarn.lock +++ b/Example/testHotUpdate/yarn.lock @@ -6231,10 +6231,10 @@ react-native-safe-area-context@^4.8.2: resolved "https://registry.yarnpkg.com/react-native-safe-area-context/-/react-native-safe-area-context-4.8.2.tgz#e6b3d8acf3c6afcb4b5db03a97f9c37df7668f65" integrity sha512-ffUOv8BJQ6RqO3nLml5gxJ6ab3EestPiyWekxdzO/1MQ7NF8fW1Mzh1C5QE9yq573Xefnc7FuzGXjtesZGv7cQ== -react-native-update@^10.2.6: - version "10.2.6" - resolved "https://registry.yarnpkg.com/react-native-update/-/react-native-update-10.2.6.tgz#976c1d1b48ac3522677b5ecbfe3e64fa04df1839" - integrity sha512-x72ga106wEWsinHSN/ANk+YygvGq6hyTEVOCibakps7Wb+5JgTiMeE0p2+GOOPhuxMavEHiil5WdhYOipI3/Qg== +react-native-update@^10.5.4: + version "10.5.4" + resolved "https://registry.yarnpkg.com/react-native-update/-/react-native-update-10.5.4.tgz#00bb26ad021bb85b12f0daa435fb118259c816e1" + integrity sha512-6/AmsS8lOy5dJC4urWtFvhGmnqGJbabN2xnMVkZz5thLiMjcrNXipzpAN6FlHezdyPq/7wrsDCzpcgBnQKQRRg== dependencies: nanoid "^3.3.3" diff --git a/package.json b/package.json index 7bde1f9..92040de 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "10.5.4", + "version": "10.6.0-beta.0", "description": "react-native hot update", "main": "src/index", "scripts": { diff --git a/src/client.ts b/src/client.ts index 4810f40..d09e323 100644 --- a/src/client.ts +++ b/src/client.ts @@ -18,7 +18,7 @@ const defaultServer = { main: 'https://update.react-native.cn/api', backups: ['https://update.reactnative.cn/api'], queryUrl: - 'https://raw.githubusercontent.com/reactnativecn/react-native-pushy/master/endpoints.json', + 'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json', }; const empty = {}; @@ -33,8 +33,8 @@ export class Pushy { appKey: '', server: defaultServer, autoMarkSuccess: true, - useAlert: true, - strategy: 'both', + updateStrategy: 'alwaysAsk', + checkStrategy: 'both', logger: noop, debug: false, }; diff --git a/src/provider.tsx b/src/provider.tsx index 5389c8f..f75e5b8 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -43,7 +43,7 @@ export const PushyProvider = ({ const showAlert = useCallback( (...args: Parameters) => { - if (options.useAlert) { + if (options.updateStrategy === 'alwaysAlert') { Alert.alert(...args); } }, @@ -73,6 +73,11 @@ export const PushyProvider = ({ return; } stateListener.current && stateListener.current.remove(); + if (options.updateStrategy === 'silentAndNow') { + return client.switchVersion(hash); + } else if (options.updateStrategy === 'silentAndLater') { + return client.switchVersionLater(hash); + } showAlert('提示', '下载完毕,是否立即更新?', [ { text: '下次再说', @@ -94,7 +99,7 @@ export const PushyProvider = ({ showAlert('更新失败', e.message); } }, - [client, showAlert], + [client, options.updateStrategy, showAlert], ); const downloadAndInstallApk = useCallback( @@ -128,6 +133,14 @@ export const PushyProvider = ({ if (info.expired) { const { downloadUrl } = info; if (downloadUrl) { + if (options.updateStrategy === 'silentAndNow') { + if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { + downloadAndInstallApk(downloadUrl); + } else { + Linking.openURL(downloadUrl); + } + return; + } showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [ { text: '更新', @@ -142,6 +155,12 @@ export const PushyProvider = ({ ]); } } else if (info.update) { + if ( + options.updateStrategy === 'silentAndNow' || + options.updateStrategy === 'silentAndLater' + ) { + return downloadUpdate(info); + } showAlert( '提示', '检查到新的版本' + info.name + ',是否下载?\n' + info.description, @@ -157,7 +176,13 @@ export const PushyProvider = ({ ], ); } - }, [client, downloadAndInstallApk, downloadUpdate, showAlert]); + }, [ + client, + downloadAndInstallApk, + downloadUpdate, + options.updateStrategy, + showAlert, + ]); const markSuccess = client.markSuccess; @@ -168,11 +193,11 @@ export const PushyProvider = ({ ); return; } - const { strategy, dismissErrorAfter, autoMarkSuccess } = options; + const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options; if (isFirstTime && autoMarkSuccess) { markSuccess(); } - if (strategy === 'both' || strategy === 'onAppResume') { + if (checkStrategy === 'both' || checkStrategy === 'onAppResume') { stateListener.current = AppState.addEventListener( 'change', nextAppState => { @@ -182,7 +207,7 @@ export const PushyProvider = ({ }, ); } - if (strategy === 'both' || strategy === 'onAppStart') { + if (checkStrategy === 'both' || checkStrategy === 'onAppStart') { checkUpdate(); } let dismissErrorTimer: ReturnType; diff --git a/src/type.ts b/src/type.ts index fef845e..9573d5e 100644 --- a/src/type.ts +++ b/src/type.ts @@ -69,8 +69,8 @@ export interface PushyOptions { appKey: string; server?: PushyServerConfig; logger?: UpdateEventsLogger; - useAlert?: boolean; - strategy?: 'onAppStart' | 'onAppResume' | 'both' | null; + updateStrategy?: 'alwaysAlert' | 'silentAndNow' | 'silentAndLater' | null; + checkStrategy?: 'onAppStart' | 'onAppResume' | 'both' | null; autoMarkSuccess?: boolean; dismissErrorAfter?: number; debug?: boolean; diff --git a/src/utils.ts b/src/utils.ts index 97623ea..e93515c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -18,13 +18,15 @@ export const emptyModule = new EmptyModule(); const ping = Platform.OS === 'web' - ? () => Promise.resolve(true) + ? Promise.resolve : async (url: string) => Promise.race([ fetch(url, { method: 'HEAD', - }).then(({ status }) => status === 200), - new Promise(r => setTimeout(() => r(false), 2000)), + }) + .then(({ status }) => (status === 200 ? url : null)) + .catch(() => null), + new Promise(r => setTimeout(() => r(null), 2000)), ]); const canUseGoogle = ping('https://www.google.com'); @@ -33,7 +35,5 @@ export const testUrls = async (urls?: string[]) => { if (!urls?.length || (await canUseGoogle)) { return null; } - return Promise.race(urls.map(url => ping(url).then(() => url))).catch( - () => null, - ); + return Promise.race(urls.map(ping)).catch(() => null); };