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

fix: new version

This commit is contained in:
sunnylqm
2024-01-21 14:41:06 +08:00
parent e5405b4977
commit 36533d43c4
7 changed files with 244 additions and 207 deletions

View File

@@ -41,17 +41,22 @@ export class Pushy {
marked = false;
applyingUpdate = false;
version = cInfo.pushy;
constructor(options: PushyOptions) {
if (!options.appKey) {
throw new Error('appKey is required');
}
this.setOptions(options);
}
setOptions = (options: Partial<PushyOptions>) => {
for (const [key, value] of Object.entries(options)) {
if (value !== undefined) {
this.options[key] = value;
}
}
}
};
getCheckUrl = (endpoint: string = this.options.server!.main) => {
return `${endpoint}/checkUpdate/${this.options.appKey}`;

View File

@@ -1,5 +1,6 @@
import { createContext, useContext } from 'react';
import { CheckResult, ProgressData } from './type';
import { Pushy } from './client';
const empty = {};
const noop = () => {};
@@ -10,6 +11,7 @@ export const defaultContext = {
switchVersionLater: noop,
markSuccess: noop,
dismissError: noop,
downloadUpdate: noop,
};
export const PushyContext = createContext<{
@@ -21,6 +23,8 @@ export const PushyContext = createContext<{
updateInfo?: CheckResult;
lastError?: Error;
dismissError: () => void;
client?: Pushy;
downloadUpdate: () => void;
}>(defaultContext);
export const usePushy = () => useContext(PushyContext);

View File

@@ -14,7 +14,7 @@ import {
} from 'react-native';
import { Pushy } from './client';
import { isFirstTime } from './core';
import { UpdateAvailableResult, CheckResult } from './type';
import { CheckResult } from './type';
import { PushyContext } from './context';
export const PushyProvider = ({
@@ -56,38 +56,37 @@ export const PushyProvider = ({
}
}, [client, updateInfo]);
const doUpdate = useCallback(
async (info: UpdateAvailableResult) => {
try {
const hash = await client.downloadUpdate(info);
if (!hash) {
return;
}
setUpdateInfo(info);
stateListener.current && stateListener.current.remove();
showAlert('Download complete', 'Do you want to apply the update now?', [
{
text: '下次再说',
style: 'cancel',
onPress: () => {
client.switchVersionLater(hash);
},
},
{
text: '立即更新',
style: 'default',
onPress: () => {
client.switchVersion(hash);
},
},
]);
} catch (err) {
setLastError(err);
showAlert('Failed to update', err.message);
const downloadUpdate = useCallback(async () => {
if (!updateInfo || !('update' in updateInfo)) {
return;
}
try {
const hash = await client.downloadUpdate(updateInfo);
if (!hash) {
return;
}
},
[client, showAlert],
);
stateListener.current && stateListener.current.remove();
showAlert('Download complete', 'Do you want to apply the update now?', [
{
text: '下次再说',
style: 'cancel',
onPress: () => {
client.switchVersionLater(hash);
},
},
{
text: '立即更新',
style: 'default',
onPress: () => {
client.switchVersion(hash);
},
},
]);
} catch (err) {
setLastError(err);
showAlert('Failed to update', err.message);
}
}, [client, showAlert, updateInfo]);
const checkUpdate = useCallback(async () => {
let info: CheckResult;
@@ -98,9 +97,9 @@ export const PushyProvider = ({
showAlert('Failed to check update', err.message);
return;
}
setUpdateInfo(info);
if ('expired' in info) {
const { downloadUrl } = info;
setUpdateInfo(info);
showAlert(
'Major update',
'A full update is required to download and install to continue.',
@@ -129,13 +128,13 @@ export const PushyProvider = ({
text: '确定',
style: 'default',
onPress: () => {
doUpdate(info as UpdateAvailableResult);
downloadUpdate();
},
},
],
);
}
}, [client, doUpdate, showAlert]);
}, [client, downloadUpdate, showAlert]);
const markSuccess = client.markSuccess;
@@ -179,6 +178,8 @@ export const PushyProvider = ({
updateInfo,
lastError,
markSuccess,
client,
downloadUpdate,
}}
>
{children}