mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 01:16:09 +08:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0fdb33ab10 | ||
![]() |
a5893d8022 | ||
![]() |
54036c0f16 | ||
![]() |
454fc28c08 | ||
![]() |
3355751bd5 |
@@ -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();
|
||||
|
@@ -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": {
|
||||
|
@@ -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, {
|
||||
|
@@ -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,7 +145,8 @@ export const PushyProvider = ({
|
||||
[client],
|
||||
);
|
||||
|
||||
const checkUpdate = useCallback(async () => {
|
||||
const checkUpdate = useCallback(
|
||||
async (extra?: Record<string, any>) => {
|
||||
const now = Date.now();
|
||||
if (lastChecking.current && now - lastChecking.current < 1000) {
|
||||
return;
|
||||
@@ -137,10 +154,11 @@ export const PushyProvider = ({
|
||||
lastChecking.current = now;
|
||||
let info: CheckResult;
|
||||
try {
|
||||
info = await client.checkUpdate();
|
||||
info = await client.checkUpdate(extra);
|
||||
} catch (e: any) {
|
||||
setLastError(e);
|
||||
alertError('更新检查失败', e.message);
|
||||
throwErrorIfEnabled(e);
|
||||
return;
|
||||
}
|
||||
if (!info) {
|
||||
@@ -194,14 +212,17 @@ export const PushyProvider = ({
|
||||
],
|
||||
);
|
||||
}
|
||||
}, [
|
||||
alertError,
|
||||
},
|
||||
[
|
||||
client,
|
||||
downloadAndInstallApk,
|
||||
downloadUpdate,
|
||||
alertError,
|
||||
throwErrorIfEnabled,
|
||||
options.updateStrategy,
|
||||
alertUpdate,
|
||||
]);
|
||||
downloadAndInstallApk,
|
||||
downloadUpdate,
|
||||
],
|
||||
);
|
||||
|
||||
const markSuccess = client.markSuccess;
|
||||
|
||||
|
@@ -79,4 +79,5 @@ export interface PushyOptions {
|
||||
autoMarkSuccess?: boolean;
|
||||
dismissErrorAfter?: number;
|
||||
debug?: boolean;
|
||||
throwError?: boolean;
|
||||
}
|
||||
|
Reference in New Issue
Block a user