Skip to content

Commit

Permalink
API: Stabilize localAutosave() as autosave( { local: true } ) (#20149)
Browse files Browse the repository at this point in the history
* API: Stabilize localAutosave()

* API: Incorporate local autosave in autosave action
  • Loading branch information
mcsf authored Oct 12, 2020
1 parent 9674a2f commit d4a74cc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,10 @@ _Related_

<a name="autosave" href="#autosave">#</a> **autosave**

Action generator used in signalling that the post should autosave.
Action generator used in signalling that the post should autosave. This
includes server-side autosaving (default) and client-side (a.k.a. local)
autosaving (e.g. on the Web, the post might be committed to Session
Storage).

_Parameters_

Expand Down
24 changes: 6 additions & 18 deletions packages/e2e-tests/specs/editor/various/autosave.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ describe( 'autosave', () => {

// Trigger local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
// Reload without saving on the server
await page.reload();
Expand Down Expand Up @@ -207,9 +205,7 @@ describe( 'autosave', () => {

// Trigger local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
expect(
await page.evaluate( () => window.sessionStorage.length )
Expand All @@ -233,9 +229,7 @@ describe( 'autosave', () => {

// Trigger local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
expect(
await page.evaluate( () => window.sessionStorage.length )
Expand All @@ -260,9 +254,7 @@ describe( 'autosave', () => {

// Trigger local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
expect(
await page.evaluate( () => window.sessionStorage.length )
Expand All @@ -283,9 +275,7 @@ describe( 'autosave', () => {

// Trigger local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
expect(
await page.evaluate( () => window.sessionStorage.length )
Expand Down Expand Up @@ -314,9 +304,7 @@ describe( 'autosave', () => {

// Force conflicting local autosave
await page.evaluate( () =>
window.wp.data
.dispatch( 'core/editor' )
.__experimentalLocalAutosave()
window.wp.data.dispatch( 'core/editor' ).autosave( { local: true } )
);
expect(
await page.evaluate( () => window.sessionStorage.length )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ function useAutosavePurge() {
}

function LocalAutosaveMonitor() {
const { __experimentalLocalAutosave } = useDispatch( 'core/editor' );
const autosave = useCallback( () => {
requestIdleCallback( __experimentalLocalAutosave );
const { autosave } = useDispatch( 'core/editor' );
const deferedAutosave = useCallback( () => {
requestIdleCallback( () => autosave( { local: true } ) );
}, [] );
useAutosaveNotice();
useAutosavePurge();
Expand All @@ -187,7 +187,7 @@ function LocalAutosaveMonitor() {
return (
<AutosaveMonitor
interval={ localAutosaveInterval }
autosave={ autosave }
autosave={ deferedAutosave }
/>
);
}
Expand Down
64 changes: 37 additions & 27 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,36 +342,46 @@ export function* trashPost() {
}

/**
* Action generator used in signalling that the post should autosave.
* Action generator used in signalling that the post should autosave. This
* includes server-side autosaving (default) and client-side (a.k.a. local)
* autosaving (e.g. on the Web, the post might be committed to Session
* Storage).
*
* @param {Object?} options Extra flags to identify the autosave.
*/
export function* autosave( options ) {
yield dispatch( STORE_KEY, 'savePost', { isAutosave: true, ...options } );
}

export function* __experimentalLocalAutosave() {
const post = yield select( STORE_KEY, 'getCurrentPost' );
const isPostNew = yield select( STORE_KEY, 'isEditedPostNew' );
const title = yield select( STORE_KEY, 'getEditedPostAttribute', 'title' );
const content = yield select(
STORE_KEY,
'getEditedPostAttribute',
'content'
);
const excerpt = yield select(
STORE_KEY,
'getEditedPostAttribute',
'excerpt'
);
yield {
type: 'LOCAL_AUTOSAVE_SET',
postId: post.id,
isPostNew,
title,
content,
excerpt,
};
export function* autosave( { local = false, ...options } = {} ) {
if ( local ) {
const post = yield select( STORE_KEY, 'getCurrentPost' );
const isPostNew = yield select( STORE_KEY, 'isEditedPostNew' );
const title = yield select(
STORE_KEY,
'getEditedPostAttribute',
'title'
);
const content = yield select(
STORE_KEY,
'getEditedPostAttribute',
'content'
);
const excerpt = yield select(
STORE_KEY,
'getEditedPostAttribute',
'excerpt'
);
yield {
type: 'LOCAL_AUTOSAVE_SET',
postId: post.id,
isPostNew,
title,
content,
excerpt,
};
} else {
yield dispatch( STORE_KEY, 'savePost', {
isAutosave: true,
...options,
} );
}
}

/**
Expand Down

0 comments on commit d4a74cc

Please sign in to comment.