Skip to content

Commit

Permalink
Only queue next restore point on PROJECT_CHANGED
Browse files Browse the repository at this point in the history
This fixes the problem where having a project with unsaved changes
open in the background can fill up all the slots.
  • Loading branch information
GarboMuffin committed Jun 14, 2024
1 parent 38aa41d commit 4734dbd
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions src/containers/tw-restore-point-manager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import log from '../lib/log';
/* eslint-disable no-alert */

const SAVE_DELAY = 250;
const MINIMUM_SAVE_TIME = 750;
const MINIMUM_SAVE_TIME = 1000;

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

Expand Down Expand Up @@ -45,6 +45,7 @@ class TWRestorePointManager extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleProjectChanged',
'handleClickCreate',
'handleClickDelete',
'handleClickDeleteAll',
Expand All @@ -56,18 +57,21 @@ class TWRestorePointManager extends React.Component {
totalSize: 0,
restorePoints: [],
error: null,
wasChanged: props.projectChanged,
interval: RestorePointAPI.readInterval()
};
this.timeout = null;
}

componentDidMount () {
if (this.state.wasChanged) {
// This helps reduce problems when people constantly enter and leave the editor which
// causes this component to re-mount. Still not perfect though, ideally we would
// compensate for time already passed.
if (this.props.projectChanged) {
this.queueRestorePoint();
}

RestorePointAPI.deleteLegacyRestorePoint();
this.props.vm.on('PROJECT_CHANGED', this.handleProjectChanged);
}

componentWillReceiveProps (nextProps) {
Expand All @@ -78,32 +82,19 @@ class TWRestorePointManager extends React.Component {
restorePoints: []
});
}
}

if (nextProps.projectChanged && !this.props.projectChanged && !this.state.wasChanged) {
this.setState({
wasChanged: true
});
}

if (!nextProps.isShowingProject && this.props.isShowingProject) {
this.setState({
wasChanged: false
});
}
componentWillUnmount () {
this.cancelQueuedRestorePoint();
this.props.vm.off('PROJECT_CHANGED', this.handleProjectChanged);
}

componentDidUpdate (prevProps, prevState) {
if (this.state.wasChanged && !prevState.wasChanged) {
handleProjectChanged () {
if (!this.timeout) {
this.queueRestorePoint();
} else if (!this.state.wasChanged && prevState.wasChanged) {
this.cancelQueuedRestorePoint();
}
}

componentWillUnmount () {
this.cancelQueuedRestorePoint();
}

handleClickCreate () {
this.createRestorePoint(RestorePointAPI.TYPE_MANUAL)
.catch(error => {
Expand Down Expand Up @@ -187,7 +178,7 @@ class TWRestorePointManager extends React.Component {
this.setState({
interval
}, () => {
if (this.state.wasChanged) {
if (this.timeout) {
this.cancelQueuedRestorePoint();
this.queueRestorePoint();
}
Expand All @@ -201,7 +192,6 @@ class TWRestorePointManager extends React.Component {
this.timeout = setTimeout(() => {
this.createRestorePoint(RestorePointAPI.TYPE_AUTOMATIC).then(() => {
this.timeout = null;
this.queueRestorePoint();
});
}, this.state.interval);
}
Expand Down

0 comments on commit 4734dbd

Please sign in to comment.