1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

feat: extra param for checkupdate

This commit is contained in:
sunnylqm 2024-07-25 22:04:52 +08:00
parent 54036c0f16
commit a5893d8022
No known key found for this signature in database
2 changed files with 76 additions and 72 deletions

View File

@ -150,7 +150,7 @@ export class Pushy {
PushyModule.setNeedUpdate({ hash }); PushyModule.setNeedUpdate({ hash });
} }
}; };
checkUpdate = async () => { checkUpdate = async (extra?: Record<string, any>) => {
if (__DEV__ && !this.options.debug) { if (__DEV__ && !this.options.debug) {
console.info( console.info(
'您当前处于开发环境且未启用 debug不会进行热更检查。如需在开发环境中调试热更请在 client 中设置 debug 为 true', '您当前处于开发环境且未启用 debug不会进行热更检查。如需在开发环境中调试热更请在 client 中设置 debug 为 true',
@ -176,6 +176,7 @@ export class Pushy {
hash: currentVersion, hash: currentVersion,
buildTime, buildTime,
cInfo, cInfo,
...extra,
}; };
if (__DEV__) { if (__DEV__) {
delete fetchBody.buildTime; delete fetchBody.buildTime;

View File

@ -145,81 +145,84 @@ export const PushyProvider = ({
[client], [client],
); );
const checkUpdate = useCallback(async () => { const checkUpdate = useCallback(
const now = Date.now(); async (extra?: Record<string, any>) => {
if (lastChecking.current && now - lastChecking.current < 1000) { const now = Date.now();
return; if (lastChecking.current && now - lastChecking.current < 1000) {
} return;
lastChecking.current = now; }
let info: CheckResult; lastChecking.current = now;
try { let info: CheckResult;
info = await client.checkUpdate(); try {
} catch (e: any) { info = await client.checkUpdate(extra);
setLastError(e); } catch (e: any) {
alertError('更新检查失败', e.message); setLastError(e);
throwErrorIfEnabled(e); alertError('更新检查失败', e.message);
return; throwErrorIfEnabled(e);
} return;
if (!info) { }
return; if (!info) {
} return;
updateInfoRef.current = info; }
setUpdateInfo(info); updateInfoRef.current = info;
if (info.expired) { setUpdateInfo(info);
const { downloadUrl } = info; if (info.expired) {
if (downloadUrl) { const { downloadUrl } = info;
if (options.updateStrategy === 'silentAndNow') { if (downloadUrl) {
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { if (options.updateStrategy === 'silentAndNow') {
downloadAndInstallApk(downloadUrl); if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
} else { downloadAndInstallApk(downloadUrl);
Linking.openURL(downloadUrl); } else {
Linking.openURL(downloadUrl);
}
return;
} }
return; alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [
{
text: '更新',
onPress: () => {
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
downloadAndInstallApk(downloadUrl);
} else {
Linking.openURL(downloadUrl);
}
},
},
]);
} }
alertUpdate('提示', '您的应用版本已更新,点击更新下载安装新版本', [ } else if (info.update) {
{ if (
text: '更新', options.updateStrategy === 'silentAndNow' ||
onPress: () => { options.updateStrategy === 'silentAndLater'
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { ) {
downloadAndInstallApk(downloadUrl); return downloadUpdate(info);
} else { }
Linking.openURL(downloadUrl); alertUpdate(
} '提示',
'检查到新的版本' + info.name + ',是否下载?\n' + info.description,
[
{ text: '取消', style: 'cancel' },
{
text: '确定',
style: 'default',
onPress: () => {
downloadUpdate();
},
}, },
}, ],
]); );
} }
} else if (info.update) { },
if ( [
options.updateStrategy === 'silentAndNow' || client,
options.updateStrategy === 'silentAndLater' alertError,
) { throwErrorIfEnabled,
return downloadUpdate(info); options.updateStrategy,
} alertUpdate,
alertUpdate( downloadAndInstallApk,
'提示', downloadUpdate,
'检查到新的版本' + info.name + ',是否下载?\n' + info.description, ],
[ );
{ text: '取消', style: 'cancel' },
{
text: '确定',
style: 'default',
onPress: () => {
downloadUpdate();
},
},
],
);
}
}, [
client,
alertError,
throwErrorIfEnabled,
options.updateStrategy,
alertUpdate,
downloadAndInstallApk,
downloadUpdate,
]);
const markSuccess = client.markSuccess; const markSuccess = client.markSuccess;