1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-17 23:06:10 +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();
Response response = client.newCall(request).execute();
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();
long contentLength = body.contentLength();

View File

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

View File

@@ -37,6 +37,7 @@ export class Pushy {
checkStrategy: 'both',
logger: noop,
debug: false,
throwError: false,
};
lastChecking?: number;
@@ -149,7 +150,7 @@ export class Pushy {
PushyModule.setNeedUpdate({ hash });
}
};
checkUpdate = async () => {
checkUpdate = async (extra?: Record<string, any>) => {
if (__DEV__ && !this.options.debug) {
console.info(
'您当前处于开发环境且未启用 debug不会进行热更检查。如需在开发环境中调试热更请在 client 中设置 debug 为 true',
@@ -175,6 +176,7 @@ export class Pushy {
hash: currentVersion,
buildTime,
cInfo,
...extra,
};
if (__DEV__) {
delete fetchBody.buildTime;
@@ -289,6 +291,7 @@ export class Pushy {
}
let succeeded = false;
this.report({ type: 'downloading' });
let lastError: any;
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) {
log('downloading diff');
@@ -300,6 +303,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@@ -317,6 +321,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@@ -334,6 +339,7 @@ export class Pushy {
});
succeeded = true;
} catch (e: any) {
lastError = e;
if (__DEV__) {
succeeded = true;
} else {
@@ -349,10 +355,14 @@ export class Pushy {
return hash;
}
if (!succeeded) {
return this.report({
this.report({
type: 'errorUpdate',
data: { newVersion: hash },
});
if (lastError) {
throw lastError;
}
return;
}
log('downloaded hash:', hash);
setLocalHashInfo(hash, {

View File

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

View File

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