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

Add an async __unstablePreSavePost hook; resolving with false prevents saving #58022

Merged
merged 12 commits into from
Aug 2, 2024
Merged
49 changes: 36 additions & 13 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,45 @@ export const savePost =
content,
};
dispatch( { type: 'REQUEST_POST_UPDATE_START', options } );
await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
edits,

let error = false;
try {
error = await applyFilters(
'editor.__unstablePreSavePost',
Promise.resolve( false ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not wrong we use this technique (a promise filter) because we don't support async actions yet right? Can we implement async action instead? await doAsyncAction( 'editor.__unstablePreSavePost' )

I have another use-case for this and maybe it's time to fix that once and for all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'm working on this and one little obstacle is that currentFilter()/currentAction() don't make sense any more, because there can be multiple hooks running at the same time.

If you use the promise filter technique with sync hooks this problem is invisible. Because the hook merely sets up a promise chain and returns. Only later the actual data will flow through the chain. This will change when runHook will start waiting for each promise returned from a handler to resolve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a draft PR for async hooks: #64204

options
);
} catch ( err ) {
error = err;
}

let error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
if ( ! error ) {
try {
await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
edits,
options
);
} catch ( err ) {
error =
err.message && err.code !== 'unknown_error'
? err.message
: __( 'An error occurred while updating.' );
}
}

if ( ! error ) {
error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
}

if ( ! error ) {
await applyFilters(
Expand Down
Loading