Skip to content

Commit d2f23ad

Browse files
author
sunny.luo
committed
fix testurl
1 parent 45dd8b2 commit d2f23ad

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/client.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
2-
import { joinUrls, log, testUrls } from './utils';
2+
import { emptyObj, joinUrls, log, noop, promiseAny, testUrls } from './utils';
33
import { EmitterSubscription, Platform } from 'react-native';
44
import { PermissionsAndroid } from './permissions';
55
import {
@@ -24,9 +24,6 @@ const defaultServer = {
2424
],
2525
};
2626

27-
const empty = {};
28-
const noop = () => {};
29-
3027
if (Platform.OS === 'web') {
3128
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
3229
}
@@ -230,7 +227,7 @@ export class Pushy {
230227
const backupEndpoints = await this.getBackupEndpoints();
231228
if (backupEndpoints) {
232229
try {
233-
resp = await Promise.race(
230+
resp = await promiseAny(
234231
backupEndpoints.map(endpoint =>
235232
fetch(this.getCheckUrl(endpoint), fetchPayload),
236233
),
@@ -248,7 +245,7 @@ export class Pushy {
248245
message: 'Can not connect to update server. Please check your network.',
249246
});
250247
this.throwIfEnabled(new Error('errorChecking'));
251-
return this.lastRespJson ? await this.lastRespJson : empty;
248+
return this.lastRespJson ? await this.lastRespJson : emptyObj;
252249
}
253250
this.lastRespJson = resp.json();
254251

@@ -273,7 +270,7 @@ export class Pushy {
273270
}
274271
if (server.queryUrls) {
275272
try {
276-
const resp = await Promise.race(
273+
const resp = await promiseAny(
277274
server.queryUrls.map(queryUrl => fetch(queryUrl)),
278275
);
279276
const remoteEndpoints = await resp.json();

src/utils.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@ export function log(...args: any[]) {
44
console.log('pushy: ', ...args);
55
}
66

7-
const noop = () => {};
7+
export function promiseAny<T>(promises: Promise<T>[]) {
8+
return new Promise<T>((resolve, reject) => {
9+
let count = 0;
10+
11+
promises.forEach(promise => {
12+
Promise.resolve(promise)
13+
.then(resolve)
14+
.catch(() => {
15+
count++;
16+
if (count === promises.length) {
17+
reject(new Error('All promises were rejected'));
18+
}
19+
});
20+
});
21+
});
22+
}
23+
24+
export const emptyObj = {};
25+
export const noop = () => {};
826
class EmptyModule {
927
constructor() {
1028
return new Proxy(this, {
@@ -23,9 +41,7 @@ const ping =
2341
Promise.race([
2442
fetch(url, {
2543
method: 'HEAD',
26-
})
27-
.then(({ status }) => (status === 200 ? url : null))
28-
.catch(() => null),
44+
}).then(({ status }) => (status === 200 ? url : null)),
2945
new Promise(r => setTimeout(() => r(null), 2000)),
3046
]);
3147

@@ -44,5 +60,5 @@ export const testUrls = async (urls?: string[]) => {
4460
if (await canUseGoogle) {
4561
return urls[0];
4662
}
47-
return Promise.race(urls.map(ping)).catch(() => null);
63+
return promiseAny(urls.map(ping)).catch(() => null);
4864
};

0 commit comments

Comments
 (0)