v10.6.0-beta.0
This commit is contained in:
parent
6244bb9af2
commit
e63fa0fdb6
@ -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": {
|
||||
|
@ -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"
|
||||
|
||||
|
@ -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": {
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -43,7 +43,7 @@ export const PushyProvider = ({
|
||||
|
||||
const showAlert = useCallback(
|
||||
(...args: Parameters<typeof Alert.alert>) => {
|
||||
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<typeof setTimeout>;
|
||||
|
@ -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;
|
||||
|
12
src/utils.ts
12
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<false>(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);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user