mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-17 22:16:10 +08:00
Implement blockupdate on android
This commit is contained in:
40
lib/index.js
40
lib/index.js
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user