mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-29 20:43:09 +08:00
fix testurl
This commit is contained in:
26
src/utils.ts
26
src/utils.ts
@@ -4,7 +4,25 @@ export function log(...args: any[]) {
|
||||
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 {
|
||||
constructor() {
|
||||
return new Proxy(this, {
|
||||
@@ -23,9 +41,7 @@ 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)),
|
||||
]);
|
||||
|
||||
@@ -44,5 +60,5 @@ export const testUrls = async (urls?: string[]) => {
|
||||
if (await canUseGoogle) {
|
||||
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