mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-18 02:06:09 +08:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1614a21968 |
@@ -1,4 +1,4 @@
|
|||||||
import { logger } from './utils';
|
import { logger, promiseAny } from './utils';
|
||||||
|
|
||||||
let currentEndpoint = 'https://update.react-native.cn/api';
|
let currentEndpoint = 'https://update.react-native.cn/api';
|
||||||
let backupEndpoints: string[] = [
|
let backupEndpoints: string[] = [
|
||||||
@@ -13,8 +13,8 @@ let backupEndpointsQueryUrls = [
|
|||||||
export async function updateBackupEndpoints() {
|
export async function updateBackupEndpoints() {
|
||||||
if (backupEndpointsQueryUrls) {
|
if (backupEndpointsQueryUrls) {
|
||||||
try {
|
try {
|
||||||
const resp = await Promise.race(
|
const resp = await promiseAny(
|
||||||
backupEndpointsQueryUrls.map((queryUrl) => fetch(queryUrl)),
|
backupEndpointsQueryUrls.map(queryUrl => fetch(queryUrl)),
|
||||||
);
|
);
|
||||||
const remoteEndpoints = await resp.json();
|
const remoteEndpoints = await resp.json();
|
||||||
if (Array.isArray(remoteEndpoints)) {
|
if (Array.isArray(remoteEndpoints)) {
|
||||||
|
10
lib/main.ts
10
lib/main.ts
@@ -16,7 +16,7 @@ import {
|
|||||||
UpdateAvailableResult,
|
UpdateAvailableResult,
|
||||||
UpdateEventsListener,
|
UpdateEventsListener,
|
||||||
} from './type';
|
} from './type';
|
||||||
import { assertRelease, logger, testUrls } from './utils';
|
import { assertRelease, logger, promiseAny, testUrls } from './utils';
|
||||||
export { setCustomEndpoints };
|
export { setCustomEndpoints };
|
||||||
const {
|
const {
|
||||||
version: v,
|
version: v,
|
||||||
@@ -167,8 +167,8 @@ export async function checkUpdate(APPKEY: string) {
|
|||||||
const backupEndpoints = await updateBackupEndpoints();
|
const backupEndpoints = await updateBackupEndpoints();
|
||||||
if (backupEndpoints) {
|
if (backupEndpoints) {
|
||||||
try {
|
try {
|
||||||
resp = await Promise.race(
|
resp = await promiseAny(
|
||||||
backupEndpoints.map((endpoint) =>
|
backupEndpoints.map(endpoint =>
|
||||||
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
fetch(getCheckUrl(APPKEY, endpoint), fetchPayload),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -205,7 +205,7 @@ function checkOperation(
|
|||||||
if (!Array.isArray(op)) {
|
if (!Array.isArray(op)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
op.forEach((action) => {
|
op.forEach(action => {
|
||||||
if (action.type === 'block') {
|
if (action.type === 'block') {
|
||||||
blockUpdate = {
|
blockUpdate = {
|
||||||
reason: action.reason,
|
reason: action.reason,
|
||||||
@@ -250,7 +250,7 @@ export async function downloadUpdate(
|
|||||||
const downloadCallback = eventListeners.onDownloadProgress;
|
const downloadCallback = eventListeners.onDownloadProgress;
|
||||||
progressHandler = eventEmitter.addListener(
|
progressHandler = eventEmitter.addListener(
|
||||||
'RCTPushyDownloadProgress',
|
'RCTPushyDownloadProgress',
|
||||||
(progressData) => {
|
progressData => {
|
||||||
if (progressData.hash === options.hash) {
|
if (progressData.hash === options.hash) {
|
||||||
downloadCallback(progressData);
|
downloadCallback(progressData);
|
||||||
}
|
}
|
||||||
|
26
lib/utils.ts
26
lib/utils.ts
@@ -1,9 +1,26 @@
|
|||||||
import { Platform } from "react-native";
|
import { Platform } from 'react-native';
|
||||||
|
|
||||||
export function logger(...args: any[]) {
|
export function logger(...args: any[]) {
|
||||||
console.log('Pushy: ', ...args);
|
console.log('Pushy: ', ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 assertRelease() {
|
export function assertRelease() {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
|
||||||
@@ -17,16 +34,13 @@ 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)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
export const testUrls = async (urls?: string[]) => {
|
export const testUrls = async (urls?: string[]) => {
|
||||||
if (!urls?.length) {
|
if (!urls?.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return Promise.race(urls.map(ping)).catch(() => null);
|
return promiseAny(urls.map(ping)).catch(() => null);
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update",
|
"name": "react-native-update",
|
||||||
"version": "9.2.1",
|
"version": "9.2.2",
|
||||||
"description": "react-native hot update",
|
"description": "react-native hot update",
|
||||||
"main": "lib/index.ts",
|
"main": "lib/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -67,5 +67,6 @@
|
|||||||
"react-native": "^0.72.6",
|
"react-native": "^0.72.6",
|
||||||
"ts-jest": "^29.0.3",
|
"ts-jest": "^29.0.3",
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2"
|
||||||
}
|
},
|
||||||
|
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user