1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-18 01:16:09 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

..

5 Commits

Author SHA1 Message Date
sunnylqm
0fdb33ab10 v10.9.0 2024-07-25 22:05:18 +08:00
sunnylqm
a5893d8022 feat: extra param for checkupdate 2024-07-25 22:04:52 +08:00
sunnylqm
54036c0f16 v10.8.0 2024-07-24 17:07:14 +08:00
sunnylqm
454fc28c08 throw error 2024-07-24 17:06:49 +08:00
sunnylqm
3355751bd5 server response message 2024-07-24 14:58:59 +08:00
5 changed files with 106 additions and 74 deletions

View File

@@ -75,7 +75,7 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
.build(); .build();
Response response = client.newCall(request).execute(); Response response = client.newCall(request).execute();
if (response.code() > 299) { if (response.code() > 299) {
throw new Error("Server return code " + response.code()); throw new Error("Server error:" + response.code() + " " + response.message());
} }
ResponseBody body = response.body(); ResponseBody body = response.body();
long contentLength = body.contentLength(); long contentLength = body.contentLength();

View File

@@ -1,6 +1,6 @@
{ {
"name": "react-native-update", "name": "react-native-update",
"version": "10.7.4", "version": "10.9.0",
"description": "react-native hot update", "description": "react-native hot update",
"main": "src/index", "main": "src/index",
"scripts": { "scripts": {

View File

@@ -37,6 +37,7 @@ export class Pushy {
checkStrategy: 'both', checkStrategy: 'both',
logger: noop, logger: noop,
debug: false, debug: false,
throwError: false,
}; };
lastChecking?: number; lastChecking?: number;
@@ -149,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',
@@ -175,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;
@@ -289,6 +291,7 @@ export class Pushy {
} }
let succeeded = false; let succeeded = false;
this.report({ type: 'downloading' }); this.report({ type: 'downloading' });
let lastError: any;
const diffUrl = (await testUrls(diffUrls)) || _diffUrl; const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) { if (diffUrl) {
log('downloading diff'); log('downloading diff');
@@ -300,6 +303,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@@ -317,6 +321,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@@ -334,6 +339,7 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
lastError = e;
if (__DEV__) { if (__DEV__) {
succeeded = true; succeeded = true;
} else { } else {
@@ -349,10 +355,14 @@ export class Pushy {
return hash; return hash;
} }
if (!succeeded) { if (!succeeded) {
return this.report({ this.report({
type: 'errorUpdate', type: 'errorUpdate',
data: { newVersion: hash }, data: { newVersion: hash },
}); });
if (lastError) {
throw lastError;
}
return;
} }
log('downloaded hash:', hash); log('downloaded hash:', hash);
setLocalHashInfo(hash, { setLocalHashInfo(hash, {

View File

@@ -37,6 +37,15 @@ export const PushyProvider = ({
const [lastError, setLastError] = useState<Error>(); const [lastError, setLastError] = useState<Error>();
const lastChecking = useRef(0); const lastChecking = useRef(0);
const throwErrorIfEnabled = useCallback(
(e: Error) => {
if (options.throwError) {
throw e;
}
},
[options.throwError],
);
const dismissError = useCallback(() => { const dismissError = useCallback(() => {
setLastError(undefined); setLastError(undefined);
}, []); }, []);
@@ -115,9 +124,16 @@ export const PushyProvider = ({
} catch (e: any) { } catch (e: any) {
setLastError(e); setLastError(e);
alertError('更新失败', e.message); alertError('更新失败', e.message);
throwErrorIfEnabled(e);
} }
}, },
[alertError, client, options.updateStrategy, alertUpdate], [
client,
options.updateStrategy,
alertUpdate,
alertError,
throwErrorIfEnabled,
],
); );
const downloadAndInstallApk = useCallback( const downloadAndInstallApk = useCallback(
@@ -129,79 +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);
return; alertError('更新检查失败', e.message);
} throwErrorIfEnabled(e);
if (!info) { return;
return; }
} if (!info) {
updateInfoRef.current = info; return;
setUpdateInfo(info); }
if (info.expired) { updateInfoRef.current = info;
const { downloadUrl } = info; setUpdateInfo(info);
if (downloadUrl) { if (info.expired) {
if (options.updateStrategy === 'silentAndNow') { const { downloadUrl } = info;
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) { if (downloadUrl) {
downloadAndInstallApk(downloadUrl); if (options.updateStrategy === 'silentAndNow') {
} else { if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
Linking.openURL(downloadUrl); downloadAndInstallApk(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();
},
},
],
);
}
}, [
alertError,
client,
downloadAndInstallApk,
downloadUpdate,
options.updateStrategy,
alertUpdate,
]);
const markSuccess = client.markSuccess; const markSuccess = client.markSuccess;

View File

@@ -79,4 +79,5 @@ export interface PushyOptions {
autoMarkSuccess?: boolean; autoMarkSuccess?: boolean;
dismissErrorAfter?: number; dismissErrorAfter?: number;
debug?: boolean; debug?: boolean;
throwError?: boolean;
} }