-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 a “waitForTransition” data API #41169
base: trunk
Are you sure you want to change the base?
Add a “waitForTransition” data API #41169
Conversation
Hi, is this still being considered or are there any things to help with? |
@gaambo I haven't had a chance to revisit, see the last comment on the thread (#41169 (comment)) for what needs work next here if you want to give it a try. |
* Rename helper from subscribeOnChange to waitForTransition * Move helper to factory.js and rename -> utils.js + update references * Update readme example * Handle cancelled promise, unsubscribing
Hey @gaambo thanks for the offer of help! One way to help would be to test the approach to see if it works for you; another would be to add some tests that covered this function. Do you want to try to work on one of those? |
Hi, sorry I never got to do more with this issue. I tried a few things, but that stuff was above my head 😅 In the project I initially needed this I solved it like this: window.wp.data.subscribe(() => {
const isSavingMetaBoxes = window.wp.data
.select("core/edit-post")
.isSavingMetaBoxes();
const isDoneSaving = wasSaving && !isSavingMetaBoxes;
if (isSavingMetaBoxes && !hasChangedInputs) {
// Do something while/before saving
}
if (isDoneSaving) {
// Do something after saving
}
wasSaving = isSavingMetaBoxes;
}); Which comes down to window.wp.data.subscribe(() => {
const isSavingMetaBoxes = window.wp.data
.select("core/edit-post")
.isSavingMetaBoxes();
}); And I'm not sure if the solution after this PR is so much better/shorter. waitForTransition( () => wp.data.select( 'core/editor' ).isSavingPost() ).then( () => {
// Do something when the post is done saving.
console.log( 'Post saved!' );
} ); I think my main concern with my current implementation was, that I'm afraid the subscribe callback would be called on each and every (=any) change in the datastore. But it seems the implementation in PR also has this "problem"? I see in this PR, it unsubscribes after triggering = it will only get triggered once. In my case I needed it to run on every save again. |
In that case you can run while ( true ) {
await waitForTransition( ... );
console.log( 'Post saved!' )
} The downside of this is that transitions that happen outside the We'd need to start using
Yes, this will be happening and it's not a problem. There's no other way to detect "relevant changes" other than inspecting "all changes". |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
What?
Add a new "subscribeOnChange" API.
Why?
Based on feedback from @youknowriad - #17632 (comment) this API helps address a common request for a "Post Published Hook". (or really a "post anything hook").
How?
Developers can now call
subscribeOnChange
with a callback function and a test they want to trigger "After", for example saving a post would be monitored with a callback usingisSavingPost
.Testing Instructions
wp.data.subscribeOnChange( () => { console.log( "Post saved." ); }, () => wp.data.select( 'core/editor' ).isSavingPost() );
Open questions