1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-17 21:56:11 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Compare commits

..

10 Commits

Author SHA1 Message Date
sunnylqm
a66f354c3b v10.2.1 2024-03-03 08:50:19 +08:00
sunnylqm
592e13b77b return null if error 2024-03-03 08:49:46 +08:00
sunnylqm
2845a4302a v10.2.0 2024-03-02 12:25:11 +08:00
sunnylqm
84ef668102 feat: test urls 2024-03-02 12:24:41 +08:00
sunnylqm
c63e1501fe v10.1.3 2024-02-25 22:24:35 +08:00
sunnylqm
45bfa2560e feat: add getCurrentVersionInfo 2024-02-25 22:23:46 +08:00
sunnylqm
ab9c40bf2e v10.1.2 2024-02-25 00:05:14 +08:00
sunnylqm
d184beabaf fix: type 2024-02-25 00:04:55 +08:00
sunnylqm
68a6235145 v10.1.1 2024-02-25 00:03:58 +08:00
sunnylqm
a98a48d665 fix: type 2024-02-25 00:03:35 +08:00
6 changed files with 59 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "react-native-update",
"version": "10.1.0",
"version": "10.2.1",
"description": "react-native hot update",
"main": "src/index.ts",
"scripts": {

View File

@@ -1,5 +1,5 @@
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { assertRelease, log } from './utils';
import { assertRelease, log, testUrls } from './utils';
import {
EmitterSubscription,
PermissionsAndroid,
@@ -225,8 +225,18 @@ export class Pushy {
onDownloadProgress?: (data: ProgressData) => void,
) => {
assertRelease();
const { hash, diffUrl, pdiffUrl, updateUrl, name, description, metaInfo } =
info;
const {
hash,
diffUrl: _diffUrl,
diffUrls,
pdiffUrl: _pdiffUrl,
pdiffUrls,
updateUrl: _updateUrl,
updateUrls,
name,
description,
metaInfo,
} = info;
if (!info.update || !hash) {
return;
}
@@ -253,6 +263,7 @@ export class Pushy {
}
let succeeded = false;
this.report({ type: 'downloading' });
const diffUrl = (await testUrls(diffUrls)) || _diffUrl;
if (diffUrl) {
log('downloading diff');
try {
@@ -266,6 +277,7 @@ export class Pushy {
log(`diff error: ${e.message}, try pdiff`);
}
}
const pdiffUrl = (await testUrls(pdiffUrls)) || _pdiffUrl;
if (!succeeded && pdiffUrl) {
log('downloading pdiff');
try {
@@ -278,6 +290,7 @@ export class Pushy {
log(`pdiff error: ${e.message}, try full patch`);
}
}
const updateUrl = (await testUrls(updateUrls)) || _updateUrl;
if (!succeeded && updateUrl) {
log('downloading full patch');
try {

View File

@@ -2,29 +2,35 @@ import { createContext, useContext } from 'react';
import { CheckResult, ProgressData } from './type';
import { Pushy } from './client';
const empty = {};
const noop = () => {};
const asyncNoop = () => Promise.resolve();
export const defaultContext = {
checkUpdate: () => Promise.resolve(empty),
checkUpdate: asyncNoop,
switchVersion: noop,
switchVersionLater: noop,
markSuccess: noop,
dismissError: noop,
downloadUpdate: noop,
downloadAndInstallApk: noop,
downloadUpdate: asyncNoop,
downloadAndInstallApk: asyncNoop,
getCurrentVersionInfo: () => Promise.resolve({}),
currentHash: '',
packageVersion: '',
};
export const PushyContext = createContext<{
checkUpdate: () => void;
checkUpdate: () => Promise<void>;
switchVersion: () => void;
switchVersionLater: () => void;
markSuccess: () => void;
dismissError: () => void;
downloadUpdate: () => void;
downloadAndInstallApk: (url: string) => void;
downloadUpdate: () => Promise<void>;
downloadAndInstallApk: (url: string) => Promise<void>;
getCurrentVersionInfo: () => Promise<{
name?: string;
description?: string;
metaInfo?: string;
}>;
currentHash: string;
packageVersion: string;
client?: Pushy;

View File

@@ -13,7 +13,12 @@ import {
Linking,
} from 'react-native';
import { Pushy } from './client';
import { currentVersion, isFirstTime, packageVersion } from './core';
import {
currentVersion,
isFirstTime,
packageVersion,
getCurrentVersionInfo,
} from './core';
import { CheckResult, ProgressData } from './type';
import { PushyContext } from './context';
@@ -88,9 +93,9 @@ export const PushyProvider = ({
}, [client, showAlert, updateInfo]);
const downloadAndInstallApk = useCallback(
(downloadUrl: string) => {
async (downloadUrl: string) => {
if (Platform.OS === 'android' && downloadUrl) {
client.downloadAndInstallApk(downloadUrl, setProgress);
await client.downloadAndInstallApk(downloadUrl, setProgress);
}
},
[client],
@@ -188,6 +193,7 @@ export const PushyProvider = ({
currentHash: currentVersion,
progress,
downloadAndInstallApk,
getCurrentVersionInfo,
}}
>
{children}

View File

@@ -8,8 +8,11 @@ export interface CheckResult {
description?: string;
metaInfo?: string;
pdiffUrl?: string;
pdiffUrls?: string[];
diffUrl?: string;
diffUrls?: string[];
updateUrl?: string;
updateUrls?: string[];
paused?: 'app' | 'package';
message?: string;
}

View File

@@ -7,3 +7,20 @@ export function assertRelease() {
throw new Error('react-native-update 只能在 RELEASE 版本中运行.');
}
}
const ping = async (url: string) =>
fetch(url, {
method: 'HEAD',
redirect: 'follow',
}).then(({ status }) => status === 200);
const canUseGoogle = ping('https://www.google.com');
export const testUrls = async (urls?: string[]) => {
if (!urls?.length || (await canUseGoogle)) {
return null;
}
return Promise.race(urls.map((url) => ping(url).then(() => url))).catch(
() => null,
);
};