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

Implement blockupdate on android

This commit is contained in:
sunnylqm
2020-08-31 18:39:03 +08:00
parent c6f9bb20a1
commit 59fcba15ee
5 changed files with 80 additions and 20 deletions

View File

@@ -9,7 +9,7 @@ const {
version: v,
} = require('react-native/Libraries/Core/ReactNativeVersion');
const RNVersion = `${v.major}.${v.minor}.${v.patch}`;
import { v4 as uuidv4 } from 'uuid';
const uuidv4 = require('uuid/v4');
let Pushy = NativeModules.Pushy;
@@ -70,16 +70,26 @@ function assertRelease() {
export async function checkUpdate(APPKEY, isRetry) {
assertRelease();
if (blockUpdate && blockUpdate.until > Date.now()) {
console.warn(
`Pushy update is blocked until ${new Date(
blockUpdate.until,
).toLocaleString()}. Reason: ${blockUpdate.reason}`,
if (blockUpdate && blockUpdate.until > Date.now() / 1000) {
throw new Error(
`热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date(
blockUpdate.until * 1000,
).toLocaleString()}"之后重试。`,
);
return;
}
let resp;
try {
console.warn({
packageVersion,
hash: currentVersion,
buildTime,
cInfo: {
pushy: require('../package.json').version,
rn: RNVersion,
os: Platform.OS + ' ' + Platform.Version,
uuid,
},
});
resp = await fetch(getCheckUrl(APPKEY), {
method: 'POST',
headers: {
@@ -105,25 +115,25 @@ export async function checkUpdate(APPKEY, isRetry) {
await tryBackupEndpoints(APPKEY);
return checkUpdate(APPKEY, true);
}
checkOperation(resp);
const result = await resp.json();
checkOperation(result.op);
if (resp.status !== 200) {
throw new Error((await resp.json()).message);
throw new Error(result.message);
}
return resp.json();
return result;
}
function checkOperation(resp) {
if (!Array.isArray(resp.op)) {
function checkOperation(op) {
if (!Array.isArray(op)) {
return;
}
resp.op.forEach((action) => {
op.forEach((action) => {
if (action.type === 'block') {
blockUpdate = {
reason: action.reason,
until: Date.now() + action.duration,
until: (Date.now() + action.duration) / 1000,
};
Pushy.setBlockUpdate(blockUpdate);
}