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

Compare commits

..

1 Commits

Author SHA1 Message Date
sunnylqm
b66b4dea06 v10.5.2 2024-04-23 00:08:53 +08:00
11 changed files with 43 additions and 78 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.6.0-beta.0",
"react-native-update": "^10.2.6",
"react-native-vector-icons": "^10.0.3"
},
"devDependencies": {

View File

@@ -47,7 +47,7 @@ function App() {
onValueChange={v => {
setUseDefaultAlert(v);
client?.setOptions({
updateStrategy: v ? null : 'alwaysAlert',
useAlert: v,
});
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.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==
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==
dependencies:
nanoid "^3.3.3"

View File

@@ -1,6 +1,6 @@
{
"name": "react-native-update",
"version": "10.6.0",
"version": "10.5.2",
"description": "react-native hot update",
"main": "src/index",
"scripts": {

View File

@@ -1,7 +1,7 @@
import { CheckResult, PushyOptions, ProgressData, EventType } from './type';
import { log, testUrls } from './utils';
import { EmitterSubscription, Platform } from 'react-native';
import { PermissionsAndroid } from './permissions';
import type { PermissionsAndroidStatic } from 'react-native';
import {
PushyModule,
buildTime,
@@ -18,7 +18,7 @@ const defaultServer = {
main: 'https://update.react-native.cn/api',
backups: ['https://update.reactnative.cn/api'],
queryUrl:
'https://cdn.jsdelivr.net/gh/reactnativecn/react-native-pushy@master/endpoints.json',
'https://raw.githubusercontent.com/reactnativecn/react-native-pushy/master/endpoints.json',
};
const empty = {};
@@ -33,8 +33,8 @@ export class Pushy {
appKey: '',
server: defaultServer,
autoMarkSuccess: true,
updateStrategy: 'alwaysAlert',
checkStrategy: 'both',
useAlert: true,
strategy: 'both',
logger: noop,
debug: false,
};
@@ -50,10 +50,8 @@ export class Pushy {
version = cInfo.pushy;
constructor(options: PushyOptions) {
if (Platform.OS === 'ios' || Platform.OS === 'android') {
if (!options.appKey) {
throw new Error('appKey is required');
}
if (!options.appKey) {
throw new Error('appKey is required');
}
this.setOptions(options);
}
@@ -373,6 +371,8 @@ export class Pushy {
this.report({ type: 'downloadingApk' });
if (Platform.Version <= 23) {
try {
const PermissionsAndroid =
require('react-native/Libraries/PermissionsAndroid/PermissionsAndroid') as PermissionsAndroidStatic;
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
);

View File

@@ -1,5 +1,5 @@
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { emptyModule, log } from './utils';
import { log } from './utils';
const {
version: v,
} = require('react-native/Libraries/Core/ReactNativeVersion');
@@ -8,9 +8,20 @@ const isTurboModuleEnabled =
// @ts-expect-error
global.__turboModuleProxy != null;
const noop = () => {};
class EmptyModule {
constructor() {
return new Proxy(this, {
get() {
return noop;
},
});
}
}
export const PushyModule =
Platform.OS === 'web'
? emptyModule
? new EmptyModule()
: isTurboModuleEnabled
? require('./NativePushy').default
: NativeModules.Pushy;

View File

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

View File

@@ -1,4 +0,0 @@
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.updateStrategy === 'alwaysAlert') {
if (options.useAlert) {
Alert.alert(...args);
}
},
[options.updateStrategy],
[options],
);
const switchVersion = useCallback(() => {
@@ -73,11 +73,6 @@ 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: '下次再说',
@@ -99,7 +94,7 @@ export const PushyProvider = ({
showAlert('更新失败', e.message);
}
},
[client, options.updateStrategy, showAlert],
[client, showAlert],
);
const downloadAndInstallApk = useCallback(
@@ -125,22 +120,11 @@ 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: '更新',
@@ -155,12 +139,6 @@ export const PushyProvider = ({
]);
}
} else if (info.update) {
if (
options.updateStrategy === 'silentAndNow' ||
options.updateStrategy === 'silentAndLater'
) {
return downloadUpdate(info);
}
showAlert(
'提示',
'检查到新的版本' + info.name + ',是否下载?\n' + info.description,
@@ -176,13 +154,7 @@ export const PushyProvider = ({
],
);
}
}, [
client,
downloadAndInstallApk,
downloadUpdate,
options.updateStrategy,
showAlert,
]);
}, [client, downloadAndInstallApk, downloadUpdate, showAlert]);
const markSuccess = client.markSuccess;
@@ -193,11 +165,11 @@ export const PushyProvider = ({
);
return;
}
const { checkStrategy, dismissErrorAfter, autoMarkSuccess } = options;
const { strategy, dismissErrorAfter, autoMarkSuccess } = options;
if (isFirstTime && autoMarkSuccess) {
markSuccess();
}
if (checkStrategy === 'both' || checkStrategy === 'onAppResume') {
if (strategy === 'both' || strategy === 'onAppResume') {
stateListener.current = AppState.addEventListener(
'change',
nextAppState => {
@@ -207,7 +179,7 @@ export const PushyProvider = ({
},
);
}
if (checkStrategy === 'both' || checkStrategy === 'onAppStart') {
if (strategy === 'both' || strategy === 'onAppStart') {
checkUpdate();
}
let dismissErrorTimer: ReturnType<typeof setTimeout>;

View File

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

View File

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