Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/ui/LiveSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export class LiveSplit extends React.Component<Props, State> {
private scrollEvent: Option<EventListenerObject>;
private rightClickEvent: Option<EventListenerObject>;
private resizeEvent: Option<EventListenerObject>;
private autoSaveInterval: Option<ReturnType<typeof setInterval>> = null;
private autoSaveInProgress = false;
private autoSaveAfterCurrent = false;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -353,6 +356,7 @@ export class LiveSplit extends React.Component<Props, State> {
this.isDesktopQuery.addEventListener("change", this.mediaQueryChanged);

this.handleAutomaticResize();
this.updateAutoSaveInterval();

const { serviceWorker } = navigator;
if (serviceWorker && serviceWorker.controller) {
Expand All @@ -368,6 +372,7 @@ export class LiveSplit extends React.Component<Props, State> {
}

public componentWillUnmount() {
this.stopAutoSaveInterval();
window.removeEventListener(
"wheel",
expect(
Expand Down Expand Up @@ -1068,6 +1073,66 @@ export class LiveSplit extends React.Component<Props, State> {
}
}

private shouldAutoSaveSplits(): boolean {
const phase = this.state.commandSink.currentPhase();
return (
phase === TimerPhase.Running ||
phase === TimerPhase.Paused ||
phase === TimerPhase.Ended
);
}

private updateAutoSaveInterval(): void {
if (!this.shouldAutoSaveSplits()) {
this.stopAutoSaveInterval();
return;
}

if (this.autoSaveInterval !== null) {
return;
}

// The serialized timer snapshot contains the in-progress attempt, so
// periodically saving the splits keeps browser restarts from wiping the
// current run's progress.
this.autoSaveInterval = setInterval(() => {
void this.autoSaveSplits();
}, 3000);
}

private stopAutoSaveInterval(): void {
if (this.autoSaveInterval === null) {
return;
}

clearInterval(this.autoSaveInterval);
this.autoSaveInterval = null;
}

private async autoSaveSplits(): Promise<void> {
if (!this.shouldAutoSaveSplits()) {
this.stopAutoSaveInterval();
return;
}

if (this.autoSaveInProgress) {
this.autoSaveAfterCurrent = true;
return;
}

this.autoSaveInProgress = true;
try {
await this.saveSplits();
} finally {
this.autoSaveInProgress = false;
}

if (this.autoSaveAfterCurrent) {
this.autoSaveAfterCurrent = false;
void this.autoSaveSplits();
}
}

onServerConnectionOpened(serverConnection: LiveSplitServer): void {
this.setState({ serverConnection });
}
Expand Down Expand Up @@ -1135,6 +1200,11 @@ export class LiveSplit extends React.Component<Props, State> {
if (this.state.serverConnection != null) {
this.state.serverConnection.sendEvent(event);
}

this.updateAutoSaveInterval();
if (this.shouldAutoSaveSplits()) {
void this.autoSaveSplits();
}
}

runChanged(): void {
Expand Down