Skip to content
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

API: Stabilize localAutosave() as autosave( { local: true } ) #20149

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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