Files
2026-11-20 16:29:14 +08:00

31 lines
1.4 KiB
TypeScript

export type PreparationPhase = 'idle' | 'loading' | 'finishing' | 'complete';
/** 未知耗时只估算到上限,真实完成才允许满格。 / Estimated waiting progress is capped until actual completion. */
export class StartupProgress {
phase: PreparationPhase = 'idle';
value = 0;
private elapsed = 0;
private finishElapsed = 0;
private finishFrom = 0;
begin(): void { this.reset(); this.phase = 'loading'; this.value = .03; }
reset(): void { this.phase = 'idle'; this.value = 0; this.elapsed = 0; this.finishElapsed = 0; this.finishFrom = 0; }
finish(): void {
if (this.phase !== 'loading') return;
this.phase = 'finishing'; this.finishFrom = this.value;
}
tick(seconds: number): number {
const dt = Number.isFinite(seconds) ? Math.min(.1, Math.max(0, seconds)) : 0;
if (this.phase === 'loading') {
this.elapsed += dt;
const target = .08 + .82 * (1 - Math.exp(-this.elapsed / 8));
this.value = Math.min(.9, this.value + Math.max(0, target - this.value) * (1 - Math.exp(-dt * 7)));
} else if (this.phase === 'finishing') {
this.finishElapsed += dt;
const t = Math.min(1, this.finishElapsed / .24);
this.value = this.finishFrom + (1 - this.finishFrom) * (1 - (1 - t) ** 3);
if (t === 1) { this.value = 1; this.phase = 'complete'; }
}
return this.value;
}
}