mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 12:11:39 +08:00
fix testurl
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
|
||||||
import { joinUrls, log, testUrls } from './utils';
|
import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
|
||||||
import { EmitterSubscription, Platform } from 'react-native';
|
import { EmitterSubscription, Platform } from 'react-native';
|
||||||
import { PermissionsAndroid } from './permissions';
|
import { PermissionsAndroid } from './permissions';
|
||||||
import {
|
import {
|
||||||
@@ -24,9 +24,6 @@ const defaultServer = {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const empty = {};
|
|
||||||
const noop = () => {};
|
|
||||||
|
|
||||||
if (Platform.OS === 'web') {
|
if (Platform.OS === 'web') {
|
||||||
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
|
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
|
||||||
}
|
}
|
||||||
@@ -230,7 +227,7 @@ export class Pushy {
|
|||||||
const backupEndpoints = await this.getBackupEndpoints();
|
const backupEndpoints = await this.getBackupEndpoints();
|
||||||
if (backupEndpoints) {
|
if (backupEndpoints) {
|
||||||
try {
|
try {
|
||||||
resp = await Promise.race(
|
resp = await promiseAny(
|
||||||
backupEndpoints.map(endpoint =>
|
backupEndpoints.map(endpoint =>
|
||||||
fetch(this.getCheckUrl(endpoint), fetchPayload),
|
fetch(this.getCheckUrl(endpoint), fetchPayload),
|
||||||
),
|
),
|
||||||
@@ -248,7 +245,7 @@ export class Pushy {
|
|||||||
message: 'Can not connect to update server. Please check your network.',
|
message: 'Can not connect to update server. Please check your network.',
|
||||||
});
|
});
|
||||||
this.throwIfEnabled(new Error('errorChecking'));
|
this.throwIfEnabled(new Error('errorChecking'));
|
||||||
return this.lastRespJson ? await this.lastRespJson : empty;
|
return this.lastRespJson ? await this.lastRespJson : emptyObj;
|
||||||
}
|
}
|
||||||
this.lastRespJson = resp.json();
|
this.lastRespJson = resp.json();
|
||||||
|
|
||||||
@@ -273,7 +270,7 @@ export class Pushy {
|
|||||||
}
|
}
|
||||||
if (server.queryUrls) {
|
if (server.queryUrls) {
|
||||||
try {
|
try {
|
||||||
const resp = await Promise.race(
|
const resp = await promiseAny(
|
||||||
server.queryUrls.map(queryUrl => fetch(queryUrl)),
|
server.queryUrls.map(queryUrl => fetch(queryUrl)),
|
||||||
);
|
);
|
||||||
const remoteEndpoints = await resp.json();
|
const remoteEndpoints = await resp.json();
|
||||||
|
26
src/utils.ts
26
src/utils.ts
@@ -4,7 +4,25 @@ export function log(...args: any[]) {
|
|||||||
console.log('pushy: ', ...args);
|
console.log('pushy: ', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
const noop = () => {};
|
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
promises.forEach(promise => {
|
||||||
|
Promise.resolve(promise)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(() => {
|
||||||
|
count++;
|
||||||
|
if (count === promises.length) {
|
||||||
|
reject(new Error('All promises were rejected'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const emptyObj = {};
|
||||||
|
export const noop = () => {};
|
||||||
class EmptyModule {
|
class EmptyModule {
|
||||||
constructor() {
|
constructor() {
|
||||||
return new Proxy(this, {
|
return new Proxy(this, {
|
||||||
@@ -23,9 +41,7 @@ const ping =
|
|||||||
Promise.race([
|
Promise.race([
|
||||||
fetch(url, {
|
fetch(url, {
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
})
|
}).then(({ status }) => (status === 200 ? url : null)),
|
||||||
.then(({ status }) => (status === 200 ? url : null))
|
|
||||||
.catch(() => null),
|
|
||||||
new Promise(r => setTimeout(() => r(null), 2000)),
|
new Promise(r => setTimeout(() => r(null), 2000)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -44,5 +60,5 @@ export const testUrls = async (urls?: string[]) => {
|
|||||||
if (await canUseGoogle) {
|
if (await canUseGoogle) {
|
||||||
return urls[0];
|
return urls[0];
|
||||||
}
|
}
|
||||||
return Promise.race(urls.map(ping)).catch(() => null);
|
return promiseAny(urls.map(ping)).catch(() => null);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user