Skip to content

Commit

Permalink
Reset autosave timer when interval changes (#35639)
Browse files Browse the repository at this point in the history
Previously, there was no way to disable the autosave timer created
within `AutosaveMonitor`'s `componentDidMount` hook. This resulted in
unexpected remote drafts to be saved from within the native editor's
Unsupported Block Editor (UBE) after the timer's 60 seconds elapsed.
The UBE loads the web-based editor within a web view and leverages
`autosaveInterval` to prevent autosave creating remote drafts.

This change enables clearing the initial autosave timer by modifying
`interval` via `autosaveInterval`. Now, when the `interval` changes the
previous timer is cleared and a new timer is created with the new
`interval`.
  • Loading branch information
dcalhoun authored Oct 25, 2021
1 parent 59efb7c commit 0f08b2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/editor/src/components/autosave-monitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class AutosaveMonitor extends Component {
return;
}

if ( this.props.interval !== prevProps.interval ) {
clearTimeout( this.timerId );
this.setAutosaveTimer();
}

if ( ! this.props.isDirty ) {
this.needsAutosave = false;
return;
Expand Down
14 changes: 10 additions & 4 deletions packages/editor/src/components/autosave-monitor/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe( 'AutosaveMonitor', () => {
} );

describe( '#componentDidUpdate()', () => {
it( 'should clear and restart autosave timer when the interval changes', () => {
wrapper.setProps( { interval: 999 } );
expect( clearTimeout ).toHaveBeenCalled();
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 );
} );

it( 'should set needsAutosave=true when editReference changes', () => {
expect( wrapper.instance().needsAutosave ).toBe( false );
wrapper.setProps( {
Expand Down Expand Up @@ -95,9 +101,9 @@ describe( 'AutosaveMonitor', () => {
isAutosaveable: true,
interval: 5,
} );
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 1 );
wrapper.instance().autosaveTimerHandler();
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 );
wrapper.instance().autosaveTimerHandler();
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 3 );
expect( setTimeout ).lastCalledWith( expect.any( Function ), 5000 );
} );

Expand All @@ -106,9 +112,9 @@ describe( 'AutosaveMonitor', () => {
isAutosaveable: false,
interval: 5,
} );
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 1 );
wrapper.instance().autosaveTimerHandler();
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 2 );
wrapper.instance().autosaveTimerHandler();
expect( setAutosaveTimerSpy ).toHaveBeenCalledTimes( 3 );
expect( setTimeout ).lastCalledWith( expect.any( Function ), 1000 );
} );

Expand Down

0 comments on commit 0f08b2e

Please sign in to comment.