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

Rename yieldToMain to splitTask and export from @wordpress/interactivity #62665

Merged
merged 3 commits into from
Jun 21, 2024
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
5 changes: 3 additions & 2 deletions docs/reference-guides/interactivity-api/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Fantastic, thank you.

function splitTask() {
return new Promise(resolve => {
setTimeout(resolve, 0);
});
Expand All @@ -823,7 +824,7 @@ store("myPlugin", {
actions: {
handleClick: function* (event) {
event.preventDefault();
yield toMainThread();
yield splitTask();
doTheWork();
},
},
Expand Down
12 changes: 3 additions & 9 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { deepSignal, peek, type DeepSignal } from 'deepsignal';
/**
* Internal dependencies
*/
import {
useWatch,
useInit,
kebabToCamelCase,
warn,
yieldToMain,
} from './utils';
import { useWatch, useInit, kebabToCamelCase, warn, splitTask } from './utils';
import type { DirectiveEntry } from './hooks';
import { directive, getScope, getEvaluate } from './hooks';

Expand Down Expand Up @@ -246,7 +240,7 @@ const getGlobalAsyncEventDirective = ( type: 'window' | 'document' ) => {
const eventName = entry.suffix.split( '--', 1 )[ 0 ];
useInit( () => {
const cb = async ( event: Event ) => {
await yieldToMain();
await splitTask();
evaluate( entry, event );
};
const globalVar = type === 'window' ? window : document;
Expand Down Expand Up @@ -361,7 +355,7 @@ export default () => {
existingHandler( event );
}
entries.forEach( async ( entry ) => {
await yieldToMain();
await splitTask();
evaluate( entry, event );
} );
};
Expand Down
1 change: 1 addition & 0 deletions packages/interactivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
useLayoutEffect,
useCallback,
useMemo,
splitTask,
} from './utils';

export { useState, useRef } from 'preact/hooks';
Expand Down
6 changes: 3 additions & 3 deletions packages/interactivity/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { hydrate, type ContainerNode, type ComponentChild } from 'preact';
* Internal dependencies
*/
import { toVdom, hydratedIslands } from './vdom';
import { createRootFragment, yieldToMain } from './utils';
import { createRootFragment, splitTask } from './utils';
import { directivePrefix } from './constants';

// Keep the same root fragment for each interactive region node.
Expand Down Expand Up @@ -35,11 +35,11 @@ export const init = async () => {

for ( const node of nodes ) {
if ( ! hydratedIslands.has( node ) ) {
await yieldToMain();
await splitTask();
const fragment = getRegionRootFragment( node );
const vdom = toVdom( node );
initialVdom.set( node, vdom );
await yieldToMain();
await splitTask();
hydrate( vdom, fragment );
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const afterNextFrame = ( callback: () => void ) => {
*
* @return Promise
*/
export const yieldToMain = () => {
export const splitTask = () => {
return new Promise( ( resolve ) => {
// TODO: Use scheduler.yield() when available.
setTimeout( resolve, 0 );
Expand Down
Loading