mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 10:31:39 +08:00
fix testurl
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { logger } from './utils';
|
||||
import { logger, promiseAny } from './utils';
|
||||
|
||||
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||
let backupEndpoints: string[] = [
|
||||
@@ -13,7 +13,7 @@ let backupEndpointsQueryUrls = [
|
||||
export async function updateBackupEndpoints() {
|
||||
if (backupEndpointsQueryUrls) {
|
||||
try {
|
||||
const resp = await Promise.race(
|
||||
const resp = await promiseAny(
|
||||
backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
|
||||
);
|
||||
const remoteEndpoints = await resp.json();
|
||||
|
@@ -16,7 +16,7 @@ import {
|
||||
UpdateAvailableResult,
|
||||
UpdateEventsListener,
|
||||
} from './type';
|
||||
import { assertRelease, logger, testUrls } from './utils';
|
||||
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
||||
export { setCustomEndpoints };
|
||||
const {
|
||||
version: v,
|
||||
@@ -162,7 +162,7 @@ export async function checkUpdate(APPKEY: string) {
|
||||
const backupEndpoints = await updateBackupEndpoints();
|
||||
if (backupEndpoints) {
|
||||
try {
|
||||
resp = await Promise.race(
|
||||
resp = await promiseAny(
|
||||
backupEndpoints.map((endpoint) =>
|
||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||
),
|
||||
|
26
lib/utils.ts
26
lib/utils.ts
@@ -1,4 +1,21 @@
|
||||
import { Platform } from "react-native";
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
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 function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
@@ -17,16 +34,13 @@ const ping =
|
||||
Promise.race([
|
||||
fetch(url, {
|
||||
method: 'HEAD',
|
||||
})
|
||||
.then(({ status }) => (status === 200 ? url : null))
|
||||
.catch(() => null),
|
||||
}).then(({ status }) => (status === 200 ? url : null)),
|
||||
new Promise(r => setTimeout(() => r(null), 2000)),
|
||||
]);
|
||||
|
||||
|
||||
export const testUrls = async (urls?: string[]) => {
|
||||
if (!urls?.length) {
|
||||
return null;
|
||||
}
|
||||
return Promise.race(urls.map(ping)).catch(() => null);
|
||||
return promiseAny(urls.map(ping)).catch(() => null);
|
||||
};
|
||||
|
Reference in New Issue
Block a user