From 4734dbd69a172b6eb6caf1e0063da32f58687b3d Mon Sep 17 00:00:00 2001 From: Muffin Date: Fri, 14 Jun 2024 14:11:10 -0500 Subject: [PATCH] Only queue next restore point on PROJECT_CHANGED This fixes the problem where having a project with unsaved changes open in the background can fill up all the slots. --- src/containers/tw-restore-point-manager.jsx | 38 ++++++++------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/containers/tw-restore-point-manager.jsx b/src/containers/tw-restore-point-manager.jsx index fad69cabbb1..7f40e79cfd3 100644 --- a/src/containers/tw-restore-point-manager.jsx +++ b/src/containers/tw-restore-point-manager.jsx @@ -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)); @@ -45,6 +45,7 @@ class TWRestorePointManager extends React.Component { constructor (props) { super(props); bindAll(this, [ + 'handleProjectChanged', 'handleClickCreate', 'handleClickDelete', 'handleClickDeleteAll', @@ -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) { @@ -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 => { @@ -187,7 +178,7 @@ class TWRestorePointManager extends React.Component { this.setState({ interval }, () => { - if (this.state.wasChanged) { + if (this.timeout) { this.cancelQueuedRestorePoint(); this.queueRestorePoint(); } @@ -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); }