mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-11-06 16:13:40 +08:00
Refactor assertRelease function
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
||||||
import { assertRelease, log, testUrls } from './utils';
|
import { log, testUrls } from './utils';
|
||||||
import {
|
import {
|
||||||
EmitterSubscription,
|
EmitterSubscription,
|
||||||
PermissionsAndroid,
|
PermissionsAndroid,
|
||||||
@@ -112,8 +112,7 @@ export class Pushy {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
markSuccess = () => {
|
markSuccess = () => {
|
||||||
assertRelease();
|
if (this.marked || __DEV__) {
|
||||||
if (this.marked) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.marked = true;
|
this.marked = true;
|
||||||
@@ -121,7 +120,12 @@ export class Pushy {
|
|||||||
this.report({ type: 'markSuccess' });
|
this.report({ type: 'markSuccess' });
|
||||||
};
|
};
|
||||||
switchVersion = (hash: string) => {
|
switchVersion = (hash: string) => {
|
||||||
assertRelease();
|
if (__DEV__) {
|
||||||
|
console.warn(
|
||||||
|
'您调用了switchVersion方法,但是当前是开发环境,不会进行任何操作。',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.assertHash(hash) && !this.applyingUpdate) {
|
if (this.assertHash(hash) && !this.applyingUpdate) {
|
||||||
log('switchVersion: ' + hash);
|
log('switchVersion: ' + hash);
|
||||||
this.applyingUpdate = true;
|
this.applyingUpdate = true;
|
||||||
@@ -130,14 +134,18 @@ export class Pushy {
|
|||||||
};
|
};
|
||||||
|
|
||||||
switchVersionLater = (hash: string) => {
|
switchVersionLater = (hash: string) => {
|
||||||
assertRelease();
|
if (__DEV__) {
|
||||||
|
console.warn(
|
||||||
|
'您调用了switchVersionLater方法,但是当前是开发环境,不会进行任何操作。',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.assertHash(hash)) {
|
if (this.assertHash(hash)) {
|
||||||
log('switchVersionLater: ' + hash);
|
log('switchVersionLater: ' + hash);
|
||||||
PushyModule.setNeedUpdate({ hash });
|
PushyModule.setNeedUpdate({ hash });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
checkUpdate = async () => {
|
checkUpdate = async () => {
|
||||||
assertRelease();
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (
|
if (
|
||||||
this.lastRespJson &&
|
this.lastRespJson &&
|
||||||
@@ -148,18 +156,22 @@ export class Pushy {
|
|||||||
}
|
}
|
||||||
this.lastChecking = now;
|
this.lastChecking = now;
|
||||||
this.report({ type: 'checking' });
|
this.report({ type: 'checking' });
|
||||||
|
const fetchBody = {
|
||||||
|
packageVersion,
|
||||||
|
hash: currentVersion,
|
||||||
|
buildTime,
|
||||||
|
cInfo,
|
||||||
|
};
|
||||||
|
if (__DEV__) {
|
||||||
|
delete fetchBody.buildTime;
|
||||||
|
}
|
||||||
const fetchPayload = {
|
const fetchPayload = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(fetchBody),
|
||||||
packageVersion,
|
|
||||||
hash: currentVersion,
|
|
||||||
buildTime,
|
|
||||||
cInfo,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
@@ -225,7 +237,6 @@ export class Pushy {
|
|||||||
info: CheckResult,
|
info: CheckResult,
|
||||||
onDownloadProgress?: (data: ProgressData) => void,
|
onDownloadProgress?: (data: ProgressData) => void,
|
||||||
) => {
|
) => {
|
||||||
assertRelease();
|
|
||||||
const {
|
const {
|
||||||
hash,
|
hash,
|
||||||
diffUrl: _diffUrl,
|
diffUrl: _diffUrl,
|
||||||
@@ -275,9 +286,13 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
if (__DEV__) {
|
||||||
|
succeeded = true;
|
||||||
|
} else {
|
||||||
log(`diff error: ${e.message}, try pdiff`);
|
log(`diff error: ${e.message}, try pdiff`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
|
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
|
||||||
if (!succeeded && pdiffUrl) {
|
if (!succeeded && pdiffUrl) {
|
||||||
log('downloading pdiff');
|
log('downloading pdiff');
|
||||||
@@ -288,9 +303,13 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
if (__DEV__) {
|
||||||
|
succeeded = true;
|
||||||
|
} else {
|
||||||
log(`pdiff error: ${e.message}, try full patch`);
|
log(`pdiff error: ${e.message}, try full patch`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
|
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
|
||||||
if (!succeeded && updateUrl) {
|
if (!succeeded && updateUrl) {
|
||||||
log('downloading full patch');
|
log('downloading full patch');
|
||||||
@@ -301,13 +320,20 @@ export class Pushy {
|
|||||||
});
|
});
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
if (__DEV__) {
|
||||||
|
succeeded = true;
|
||||||
|
} else {
|
||||||
log(`full patch error: ${e.message}`);
|
log(`full patch error: ${e.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (this.progressHandlers[hash]) {
|
if (this.progressHandlers[hash]) {
|
||||||
this.progressHandlers[hash].remove();
|
this.progressHandlers[hash].remove();
|
||||||
delete this.progressHandlers[hash];
|
delete this.progressHandlers[hash];
|
||||||
}
|
}
|
||||||
|
if (__DEV__) {
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
if (!succeeded) {
|
if (!succeeded) {
|
||||||
return this.report({
|
return this.report({
|
||||||
type: 'errorUpdate',
|
type: 'errorUpdate',
|
||||||
|
|||||||
@@ -2,11 +2,6 @@ export function log(...args: any[]) {
|
|||||||
console.log('pushy: ', ...args);
|
console.log('pushy: ', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertRelease() {
|
|
||||||
if (__DEV__) {
|
|
||||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const ping = async (url: string) =>
|
const ping = async (url: string) =>
|
||||||
Promise.race([
|
Promise.race([
|
||||||
|
|||||||
Reference in New Issue
Block a user