feat: split update into check, download and apply steps
This commit is contained in:
@@ -64,7 +64,7 @@ export function useAppUpdate() {
|
||||
return Boolean(target?.url) && target?.version !== currentVersion.value;
|
||||
}
|
||||
|
||||
/** 下载最新版本并排队等待生效 */
|
||||
/** 下载最新版本,只落盘不激活,等用户确认后再应用 */
|
||||
async function download(): Promise<BundleInfo | null> {
|
||||
const target = latest.value;
|
||||
if (!target?.url) {
|
||||
@@ -84,7 +84,6 @@ export function useAppUpdate() {
|
||||
checksum: target.checksum,
|
||||
sessionKey: target.sessionKey
|
||||
});
|
||||
await CapacitorUpdater.next({ id: bundle.id });
|
||||
ready.value = bundle;
|
||||
return bundle;
|
||||
} catch (e) {
|
||||
@@ -96,19 +95,28 @@ export function useAppUpdate() {
|
||||
}
|
||||
}
|
||||
|
||||
/** 立即重启应用使更新生效 */
|
||||
async function reload(): Promise<void> {
|
||||
await CapacitorUpdater.reload();
|
||||
/** 应用已下载的版本并重启,只有用户明确点了才执行 */
|
||||
async function apply(bundle: BundleInfo): Promise<void> {
|
||||
await CapacitorUpdater.set({ id: bundle.id });
|
||||
}
|
||||
|
||||
/** 检查并下载,供启动时自动更新调用 */
|
||||
async function checkAndDownload(): Promise<boolean> {
|
||||
await loadCurrent();
|
||||
const result = await check();
|
||||
if (!result?.url || result.version === currentVersion.value) {
|
||||
return false;
|
||||
/** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */
|
||||
async function restorePending(): Promise<void> {
|
||||
if (!supported) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]);
|
||||
const currentId = current.bundle?.id;
|
||||
const pending = list.bundles
|
||||
.filter(bundle => bundle.id !== currentId)
|
||||
.sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0];
|
||||
if (pending) {
|
||||
ready.value = pending;
|
||||
}
|
||||
} catch {
|
||||
ready.value = null;
|
||||
}
|
||||
return (await download()) !== null;
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -125,8 +133,8 @@ export function useAppUpdate() {
|
||||
check,
|
||||
hasUpdate,
|
||||
download,
|
||||
reload,
|
||||
checkAndDownload
|
||||
apply,
|
||||
restorePending
|
||||
};
|
||||
}
|
||||
|
||||
@@ -138,11 +146,21 @@ export function isAutoUpdateEnabled(): boolean {
|
||||
/**
|
||||
* 启动时静默检查更新
|
||||
*
|
||||
* 只下载排队,不重启,下次打开 App 自动生效
|
||||
* 只下载不应用,是否切换版本始终由用户决定
|
||||
*/
|
||||
export async function runAutoUpdate(): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) {
|
||||
return;
|
||||
}
|
||||
await useAppUpdate().checkAndDownload();
|
||||
const update = useAppUpdate();
|
||||
await update.loadCurrent();
|
||||
await update.restorePending();
|
||||
if (update.ready.value) {
|
||||
return;
|
||||
}
|
||||
const result = await update.check();
|
||||
if (!result?.url || result.version === update.currentVersion.value) {
|
||||
return;
|
||||
}
|
||||
await update.download();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user