You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/mx/module/boot/UpdatePipeline.ts

74 lines
2.6 KiB

3 weeks ago
import { PerfReport } from 'db://assets/script/game/PerfReport';
/** 版本更新最大自动重试 */
const MAX_UPDATE_RETRY = 2;
/** 小游戏 updateManager 最小接口 */
export interface MiniUpdateManager {
onUpdateReady?: (cb: (res: unknown) => void) => void;
onUpdateFailed?: (cb: (err: unknown) => void) => void;
onCheckForUpdate?: (cb?: () => void) => void;
applyUpdate?: () => void;
}
/**
* 版本更新下载:进度语义、失败重试与 PF 打点。
*/
export class UpdatePipeline {
private static _failRetry = 0;
/**
* 绑定平台 updateManager。
* @param updateManager 平台更新管理器
* @param showModal 弹窗(标题、内容、确认回调)
*/
static bind(
updateManager: MiniUpdateManager | null | undefined,
showModal: (opts: {
title: string;
content: string;
success: (res: { confirm: boolean }) => void;
}) => void,
showToast: (opts: { title: string; icon?: string }) => void,
): void {
if (!updateManager) return;
if (updateManager.onUpdateReady) {
updateManager.onUpdateReady(() => {
PerfReport.reportLoadUpdate('ready');
UpdatePipeline._failRetry = 0;
showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启小游戏?',
success: (res) => {
if (res.confirm && updateManager.applyUpdate) {
updateManager.applyUpdate();
}
},
});
});
}
if (updateManager.onUpdateFailed) {
updateManager.onUpdateFailed((err) => {
console.error('[UpdatePipeline] 版本下载失败', String(err));
UpdatePipeline._failRetry++;
PerfReport.reportLoadUpdate(
UpdatePipeline._failRetry <= MAX_UPDATE_RETRY ? 'retry' : 'fail',
`r${UpdatePipeline._failRetry}`,
);
if (UpdatePipeline._failRetry <= MAX_UPDATE_RETRY && updateManager.onCheckForUpdate) {
updateManager.onCheckForUpdate();
showToast({ title: '正在重试下载更新…', icon: 'none' });
return;
}
showToast({ title: '新版本下载失败,请稍后再试', icon: 'none' });
});
}
if (updateManager.onCheckForUpdate) {
updateManager.onCheckForUpdate();
}
}
}