Skip to content

Commit

Permalink
Rename yieldToMain to splitTask and export from @wordpress/interactiv…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
5 people authored and ellatrix committed Jun 25, 2024
1 parent c13e2b4 commit 115b5d8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
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.
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

0 comments on commit 115b5d8

Please sign in to comment.