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

Compare commits

..

9 Commits

Author SHA1 Message Date
sunnylqm
5100cd6915 v10.7.0 2024-06-25 11:45:10 +08:00
sunnylqm
b62e6d64cf v10.6.0 2024-06-11 12:25:45 +08:00
sunnylqm
9af0538a2a update example 2024-06-11 10:59:27 +08:00
sunnylqm
e63fa0fdb6 v10.6.0-beta.0 2024-06-11 10:44:18 +08:00
sunnylqm
6244bb9af2 v10.5.4 2024-04-23 09:31:16 +08:00
sunnylqm
9fd6293037 emptymodule 2024-04-23 00:24:32 +08:00
sunnylqm
e9e67b011c v10.5.3 2024-04-23 00:15:38 +08:00
sunnylqm
5996a7aa75 v10.5.2 2024-04-23 00:09:07 +08:00
sunnylqm
ad9b0778ba v10.5.1 2024-04-22 23:15:34 +08:00
13 changed files with 102 additions and 57 deletions

View File

@@ -18,7 +18,7 @@
"react-native": "0.69.8",
"react-native-paper": "^5.12.1",
"react-native-safe-area-context": "^4.8.2",
"react-native-update": "^10.2.6",
"react-native-update": "^10.6.0-beta.0",
"react-native-vector-icons": "^10.0.3"
},
"devDependencies": {

View File

@@ -47,7 +47,7 @@ function App() {
onValueChange={v => {
setUseDefaultAlert(v);
client?.setOptions({
useAlert: v,
updateStrategy: v ? null : 'alwaysAlert',
});
setShowUpdateSnackbar(!v);
}}

View File

@@ -6231,10 +6231,10 @@ react-native-safe-area-context@^4.8.2:
resolved "https://registry.yarnpkg.com/react-native-safe-area-context/-/react-native-safe-area-context-4.8.2.tgz#e6b3d8acf3c6afcb4b5db03a97f9c37df7668f65"
integrity sha512-ffUOv8BJQ6RqO3nLml5gxJ6ab3EestPiyWekxdzO/1MQ7NF8fW1Mzh1C5QE9yq573Xefnc7FuzGXjtesZGv7cQ==
react-native-update@^10.2.6:
version "10.2.6"
resolved "https://registry.yarnpkg.com/react-native-update/-/react-native-update-10.2.6.tgz#976c1d1b48ac3522677b5ecbfe3e64fa04df1839"
integrity sha512-x72ga106wEWsinHSN/ANk+YygvGq6hyTEVOCibakps7Wb+5JgTiMeE0p2+GOOPhuxMavEHiil5WdhYOipI3/Qg==
react-native-update@^10.6.0-beta.0:
version "10.6.0-beta.0"
resolved "https://registry.yarnpkg.com/react-native-update/-/react-native-update-10.6.0-beta.0.tgz#5414989a44f7bc94c97a8c29661c3fc4f43521d8"
integrity sha512-p/nTO/0qZhgxcmUXoiBkRvBa1yo3MocbJL4dMHboqOtK3TtUYhilP9O6fZQ9FBev4BjTyHBeXDjwT6eutw9gXg==
dependencies:
nanoid "^3.3.3"

View File

@@ -1,8 +1,8 @@
{
"name": "react-native-update",
"version": "10.5.0",
"version": "10.7.0",
"description": "react-native hot update",
"main": "src/index.ts",
"main": "src/index",
"scripts": {
"prepack": "yarn submodule && yarn lint",
"lint": "eslint \"src/*.@(ts|tsx|js|jsx)\" && tsc --noEmit",

View File

@@ -1,10 +1,7 @@
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { log, testUrls } from './utils';
import {
EmitterSubscription,
PermissionsAndroid,
Platform,
} from 'react-native';
import { EmitterSubscription, Platform } from 'react-native';
import { PermissionsAndroid } from './permissions';
import {
PushyModule,
buildTime,
@@ -21,19 +18,23 @@ const defaultServer = {
main: 'https://update.react-native.cn/api',
backups: ['https://update.reactnative.cn/api'],
queryUrl:
'https://raw.githubusercontent.com/reactnativecn/react-native-pushy/master/endpoints.json',
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
};
const empty = {};
const noop = () => {};
if (Platform.OS === 'web') {
console.warn('react-native-update 不支持 web 端热更,不会执行操作');
}
export class Pushy {
options: PushyOptions = {
appKey: '',
server: defaultServer,
autoMarkSuccess: true,
useAlert: true,
strategy: 'both',
updateStrategy: 'alwaysAlert',
checkStrategy: 'both',
logger: noop,
debug: false,
};
@@ -49,8 +50,10 @@ export class Pushy {
version = cInfo.pushy;
constructor(options: PushyOptions) {
if (!options.appKey) {
throw new Error('appKey is required');
if (Platform.OS === 'ios' || Platform.OS === 'android') {
if (!options.appKey) {
throw new Error('appKey is required');
}
}
this.setOptions(options);
}
@@ -149,10 +152,14 @@ export class Pushy {
checkUpdate = async () => {
if (__DEV__ && !this.options.debug) {
console.info(
'您当前处于开发环境且未启用debug不会进行热更检查。如需在开发环境中调试热更请在client中设置debugtrue',
'您当前处于开发环境且未启用 debug不会进行热更检查。如需在开发环境中调试热更请在 client 中设置 debugtrue',
);
return;
}
if (Platform.OS === 'web') {
console.warn('web 端不支持热更新检查');
return;
}
const now = Date.now();
if (
this.lastRespJson &&

View File

@@ -1,13 +0,0 @@
const noop = () => {};
export class Pushy {
constructor() {
console.warn(
'react-native-update is not supported and will do nothing on web.',
);
return new Proxy(this, {
get() {
return noop;
},
});
}
}

View File

@@ -1,5 +1,5 @@
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { log } from './utils';
import { emptyModule, log } from './utils';
const {
version: v,
} = require('react-native/Libraries/Core/ReactNativeVersion');
@@ -8,9 +8,12 @@ const isTurboModuleEnabled =
// @ts-expect-error
global.__turboModuleProxy != null;
export const PushyModule = isTurboModuleEnabled
? require('./NativePushy').default
: NativeModules.Pushy;
export const PushyModule =
Platform.OS === 'web'
? emptyModule
: isTurboModuleEnabled
? require('./NativePushy').default
: NativeModules.Pushy;
if (!PushyModule) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');

View File

@@ -0,0 +1 @@
export { PermissionsAndroid } from 'react-native';

4
src/permissions.ts Normal file
View File

@@ -0,0 +1,4 @@
import type { PermissionsAndroidStatic } from 'react-native';
import { emptyModule } from './utils';
export const PermissionsAndroid = emptyModule as PermissionsAndroidStatic;

View File

@@ -43,11 +43,11 @@ export const PushyProvider = ({
const showAlert = useCallback(
(...args: Parameters<typeof Alert.alert>) => {
if (options.useAlert) {
if (options.updateStrategy === 'alwaysAlert') {
Alert.alert(...args);
}
},
[options],
[options.updateStrategy],
);
const switchVersion = useCallback(() => {
@@ -73,6 +73,11 @@ export const PushyProvider = ({
return;
}
stateListener.current && stateListener.current.remove();
if (options.updateStrategy === 'silentAndNow') {
return client.switchVersion(hash);
} else if (options.updateStrategy === 'silentAndLater') {
return client.switchVersionLater(hash);
}
showAlert('提示', '下载完毕,是否立即更新?', [
{
text: '下次再说',
@@ -94,7 +99,7 @@ export const PushyProvider = ({
showAlert('更新失败', e.message);
}
},
[client, showAlert],
[client, options.updateStrategy, showAlert],
);
const downloadAndInstallApk = useCallback(
@@ -120,11 +125,22 @@ export const PushyProvider = ({
showAlert('更新检查失败', e.message);
return;
}
if (!info) {
return;
}
updateInfoRef.current = info;
setUpdateInfo(info);
if (info.expired) {
const { downloadUrl } = info;
if (downloadUrl) {
if (options.updateStrategy === 'silentAndNow') {
if (Platform.OS === 'android' && downloadUrl.endsWith('.apk')) {
downloadAndInstallApk(downloadUrl);
} else {
Linking.openURL(downloadUrl);
}
return;
}
showAlert('提示', '您的应用版本已更新,点击更新下载安装新版本', [
{
text: '更新',
@@ -139,6 +155,12 @@ export const PushyProvider = ({
]);
}
} else if (info.update) {
if (
options.updateStrategy === 'silentAndNow' ||
options.updateStrategy === 'silentAndLater'
) {
return downloadUpdate(info);
}
showAlert(
'提示',
'检查到新的版本' + info.name + ',是否下载?\n' + info.description,
@@ -154,7 +176,13 @@ export const PushyProvider = ({
],
);
}
}, [client, downloadAndInstallApk, downloadUpdate, showAlert]);
}, [
client,
downloadAndInstallApk,
downloadUpdate,
options.updateStrategy,
showAlert,
]);
const markSuccess = client.markSuccess;
@@ -165,11 +193,11 @@ export const PushyProvider = ({
);
return;
}
const { strategy, dismissErrorAfter, autoMarkSuccess } = options;
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
if (isFirstTime && autoMarkSuccess) {
markSuccess();
}
if (strategy === 'both' || strategy === 'onAppResume') {
if (checkStrategy === 'both' || checkStrategy === 'onAppResume') {
stateListener.current = AppState.addEventListener(
'change',
nextAppState => {
@@ -179,7 +207,7 @@ export const PushyProvider = ({
},
);
}
if (strategy === 'both' || strategy === 'onAppStart') {
if (checkStrategy === 'both' || checkStrategy === 'onAppStart') {
checkUpdate();
}
let dismissErrorTimer: ReturnType<typeof setTimeout>;

View File

@@ -1,2 +0,0 @@
import { Fragment } from 'react';
export const PushyProvider = Fragment;

View File

@@ -69,8 +69,8 @@ export interface PushyOptions {
appKey: string;
server?: PushyServerConfig;
logger?: UpdateEventsLogger;
useAlert?: boolean;
strategy?: 'onAppStart' | 'onAppResume' | 'both' | null;
updateStrategy?: 'alwaysAlert' | 'silentAndNow' | 'silentAndLater' | null;
checkStrategy?: 'onAppStart' | 'onAppResume' | 'both' | null;
autoMarkSuccess?: boolean;
dismissErrorAfter?: number;
debug?: boolean;

View File

@@ -1,14 +1,33 @@
import { Platform } from 'react-native';
export function log(...args: any[]) {
console.log('pushy: ', ...args);
}
const ping = async (url: string) =>
Promise.race([
fetch(url, {
method: 'HEAD',
}).then(({ status }) => status === 200),
new Promise<false>(r => setTimeout(() => r(false), 2000)),
]);
const noop = () => {};
class EmptyModule {
constructor() {
return new Proxy(this, {
get() {
return noop;
},
});
}
}
export const emptyModule = new EmptyModule();
const ping =
Platform.OS === 'web'
? Promise.resolve
: async (url: string) =>
Promise.race([
fetch(url, {
method: 'HEAD',
})
.then(({ status }) => (status === 200 ? url : null))
.catch(() => null),
new Promise(r => setTimeout(() => r(null), 2000)),
]);
const canUseGoogle = ping('https://www.google.com');
@@ -16,7 +35,5 @@ 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,
);
return Promise.race(urls.map(ping)).catch(() => null);
};