1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-11-06 08:13:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Refactor assertRelease function

This commit is contained in:
sunnylqm
2024-04-18 22:39:03 +08:00
parent de332c1796
commit 1f75688b92
2 changed files with 42 additions and 21 deletions

View File

@@ -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,7 +286,11 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
log(`diff error: ${e.message}, try pdiff`); if (__DEV__) {
succeeded = true;
} else {
log(`diff error: ${e.message}, try pdiff`);
}
} }
} }
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl; const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
@@ -288,7 +303,11 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
log(`pdiff error: ${e.message}, try full patch`); if (__DEV__) {
succeeded = true;
} else {
log(`pdiff error: ${e.message}, try full patch`);
}
} }
} }
const updateUrl = (await testUrls(updateUrls)) || _updateUrl; const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
@@ -301,13 +320,20 @@ export class Pushy {
}); });
succeeded = true; succeeded = true;
} catch (e: any) { } catch (e: any) {
log(`full patch error: ${e.message}`); if (__DEV__) {
succeeded = true;
} else {
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',

View File

@@ -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([