-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Rename yieldToMain to splitTask and export from @wordpress/interactivity #62665
Conversation
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. |
6158bd4
to
e6cc8b2
Compare
e6cc8b2
to
7c17304
Compare
Humm. In a generator context, the yield yieldToMain(); |
For now I've manually copied the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Humm. In a generator context, the
yieldToMain()
name doesn't look so good:yield yieldToMain();
I don't think it's that bad, but feel free to rename 🙂
By the way, shouldn't we also update this section of the documentation? Or do you want to make those changes in another pull request?
gutenberg/docs/reference-guides/interactivity-api/api-reference.md
Lines 813 to 831 in d86ecdb
As mentioned above with [`wp-on`](#wp-on), [`wp-on-window`](#wp-on-window), and [`wp-on-document`](#wp-on-document), an async action should be used whenever the `async` versions of the aforementioned directives cannot be used due to the action requiring synchronous access to the `event` object. Synchronous access is reqired whenever the action needs to call `event.preventDefault()`, `event.stopPropagation()`, or `event.stopImmediatePropagation()`. To ensure that the action code does not contribute to a long task, you may manually yield to the main thread after calling the synchronous event API. For example: | |
```js | |
function toMainThread() { | |
return new Promise(resolve => { | |
setTimeout(resolve, 0); | |
}); | |
} | |
store("myPlugin", { | |
actions: { | |
handleClick: function* (event) { | |
event.preventDefault(); | |
yield toMainThread(); | |
doTheWork(); | |
}, | |
}, | |
}); | |
``` |
Docs have been merged with |
Up to you, folks 🙂 |
Or simpler, what about just await mainThread();
yield mainThread(); I like this because the preposition "to" doesn't work with "await" in English (one does not just "await to something") although "to" is used with "yield" here. By removing the preposition entirely, then this awkwardness is removed. Also, by omitting a verb from the function name itself it implies that the function doesn't actually do anything on its own. Indeed, it must be used with |
I checked with @tunetheweb and he suggested |
Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org>
677ad2a
to
93d83c3
Compare
@@ -813,7 +813,8 @@ const { state } = store("myPlugin", { | |||
As mentioned above with [`wp-on`](#wp-on), [`wp-on-window`](#wp-on-window), and [`wp-on-document`](#wp-on-document), an async action should be used whenever the `async` versions of the aforementioned directives cannot be used due to the action requiring synchronous access to the `event` object. Synchronous access is reqired whenever the action needs to call `event.preventDefault()`, `event.stopPropagation()`, or `event.stopImmediatePropagation()`. To ensure that the action code does not contribute to a long task, you may manually yield to the main thread after calling the synchronous event API. For example: | |||
|
|||
```js | |||
function toMainThread() { | |||
// Note: In WordPress 6.6 this splitTask function is exported by @wordpress/interactivity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This note assumes this change will be backported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, shouldn't we also update this section of the documentation? Or do you want to make those changes in another pull request?
@luisherranz This comment in the docs here I believe updates the section accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic, thank you.
@cbravobernal I'll let you merge if you are satisfied. |
Nice addition. 😄 Not sure, but maybe we need to add an entry log for the |
Oh, yes please! |
Add changelog entry for #62665
@DAreRodz @luisherranz Like #62805? |
Merged. Thanks Weston! |
…ity (#62665) * Export yieldToMain from @wordpress/interactivity * Rename yieldToMain to splitTask Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org> * Update docs to note splitTask being exported --------- Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org> Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
I just cherry-picked this PR to the wp/6.6-rc-1 branch to get it included in the next release: f37faf7 |
…ity (#62665) * Export yieldToMain from @wordpress/interactivity * Rename yieldToMain to splitTask Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org> * Update docs to note splitTask being exported --------- Co-authored-by: tunetheweb <tunetheweb@git.wordpress.org> Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
…y (#62805) * Add changelog entry for splitTask Add changelog entry for WordPress/gutenberg#62665 * Move changelog entry to Unreleased section Co-authored-by: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> --------- Co-authored-by: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Source: WordPress/gutenberg@1ba8018
…y (#62805) * Add changelog entry for splitTask Add changelog entry for WordPress/gutenberg#62665 * Move changelog entry to Unreleased section Co-authored-by: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> --------- Co-authored-by: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Source: WordPress/gutenberg@1ba8018
Follow up to #61885
I just realized that the new
yieldToMain
function isn't currently exported by@wordpress/interactivity
. This should be exported because it allows for directives to reuse this logic to break up their own long tasks when they can't make use of thewp-async
directive (or even if they do and still are doing a lot of work).For example, consider this synchronous
wp-on
directive action which callsevent.preventDefault()
but then does work.(Granted: A developer could just re-implement
yieldToMain
on their own.)If approved and backported, this will need to be mentioned in the doc updates in #62663, in particular under Async Actions.