-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor: Avoid scheduling autosave if same edit reference as last completion #12624
Conversation
10ee732
to
fdd44b2
Compare
fdd44b2
to
93d3027
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda confusing logic to follow and I don't quite understand the test, but maybe if explained a bit more I would. The code seems fine but I don't grok the test.
// Once edit occurs after autosave, resume scheduling. | ||
wrapper.setProps( { isDirty: true, isAutosaving: false, isAutosaveable: true, editsReference: afterReference } ); | ||
|
||
expect( toggleTimer ).toHaveBeenCalledWith( false ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm mis-reading the test, but I would expect this to test for something different. Now the post is dirty, isn't autosaving, is autosavable, and has a different editsReference
, but the expectation is the same.
I don't quite follow 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm mis-reading the test, but I would expect this to test for something different.
You're totally right. We expect toggleTimer
to have been called with true
, which it is, but as written toHaveBeenCalledWith
would consider both this and the prior invocation (since we didn't clear mock calls between). So while it's true that it had been called with false
, this was not what we're testing.
I've updated the test so that it explicitly checks that the second time around, it's called with true
.
93d3027
to
6a91586
Compare
6a91586
to
ad36824
Compare
In case it has an impact on how people prioritize reviewing this: Without this change, published posts will currently autosave infinitely after an initial change is made to a block. |
@@ -53,6 +73,7 @@ export default compose( [ | |||
isDirty: isEditedPostDirty(), | |||
isAutosaveable: isEditedPostAutosaveable(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It definitely does feel like this selector is useless or doesn't have the same meaning as its name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix.
Fixes #12318
This pull request seeks to resolve an issue where autosaves will repeatedly occur when an edit is made to a published post. As explained in #12318 (comment) , this is a consequence of a published post's autosave occurring as a revision, not a proper save. The post is still in an "unsaved" state, in the sense that the user has not yet Updated the post.
With these changes, an autosave will not be scheduled after a prior autosave had completed until an explicit edit occurs, even if the post is dirty or autosaveable.
Implementation notes:
For posterity's sake, there are some specific intricacies of the autosave flow which may appear to be redundant with the logic included here, but which still serve an independent role. Namely, the re-dirtying of an autosaved published post performed as a consequence of:
gutenberg/packages/editor/src/store/effects/posts.js
Lines 143 to 150 in 8743217
gutenberg/packages/editor/src/store/selectors.js
Lines 124 to 150 in 8743217
To reiterate, it is correct that a post is considered dirty after a published post is autosaved. The user should be prompted when attempting to navigate away. It's because of this distinction that an additional "has autosaved for edits reference" is included here.
It may, however, be worth considering for future refactoring to remove the
isEditedPostAutosaveable
selector in favor of dirtiness conditions, or reflecting the last autosave which had occurred. Currently, the recurring autosave occurs becausehasChangedContent
will returntrue
due to the re-dirtying behavior described above.Testing instructions:
Repeat steps to reproduce from #12318, verifying by Network DevTools activity that only a single autosave occurs for a single edit to a published post.
Verify that after an autosave occurs, continued edits will still autosave.
Pending:
This is worth testing via an end-to-end test.