Skip to content

Commit f37faf7

Browse files
westonrutertunethewebluisherranzcbravobernal
authored andcommitted
Rename yieldToMain to splitTask and export from @wordpress/interactivity (#62665)
* Export yieldToMain from @wordpress/interactivity * Rename yieldToMain to splitTask Co-authored-by: tunetheweb <[email protected]> * Update docs to note splitTask being exported --------- Co-authored-by: tunetheweb <[email protected]> Co-authored-by: westonruter <[email protected]> Co-authored-by: luisherranz <[email protected]> Co-authored-by: cbravobernal <[email protected]>
1 parent c8372ed commit f37faf7

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

docs/reference-guides/interactivity-api/api-reference.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ const { state } = store("myPlugin", {
813813
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:
814814

815815
```js
816-
function toMainThread() {
816+
// Note: In WordPress 6.6 this splitTask function is exported by @wordpress/interactivity.
817+
function splitTask() {
817818
return new Promise(resolve => {
818819
setTimeout(resolve, 0);
819820
});
@@ -823,7 +824,7 @@ store("myPlugin", {
823824
actions: {
824825
handleClick: function* (event) {
825826
event.preventDefault();
826-
yield toMainThread();
827+
yield splitTask();
827828
doTheWork();
828829
},
829830
},

packages/interactivity/src/directives.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ import { deepSignal, peek, type DeepSignal } from 'deepsignal';
1111
/**
1212
* Internal dependencies
1313
*/
14-
import {
15-
useWatch,
16-
useInit,
17-
kebabToCamelCase,
18-
warn,
19-
yieldToMain,
20-
} from './utils';
14+
import { useWatch, useInit, kebabToCamelCase, warn, splitTask } from './utils';
2115
import type { DirectiveEntry } from './hooks';
2216
import { directive, getScope, getEvaluate } from './hooks';
2317

@@ -246,7 +240,7 @@ const getGlobalAsyncEventDirective = ( type: 'window' | 'document' ) => {
246240
const eventName = entry.suffix.split( '--', 1 )[ 0 ];
247241
useInit( () => {
248242
const cb = async ( event: Event ) => {
249-
await yieldToMain();
243+
await splitTask();
250244
evaluate( entry, event );
251245
};
252246
const globalVar = type === 'window' ? window : document;
@@ -361,7 +355,7 @@ export default () => {
361355
existingHandler( event );
362356
}
363357
entries.forEach( async ( entry ) => {
364-
await yieldToMain();
358+
await splitTask();
365359
evaluate( entry, event );
366360
} );
367361
};

packages/interactivity/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {
2525
useLayoutEffect,
2626
useCallback,
2727
useMemo,
28+
splitTask,
2829
} from './utils';
2930

3031
export { useState, useRef } from 'preact/hooks';

packages/interactivity/src/init.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { hydrate, type ContainerNode, type ComponentChild } from 'preact';
66
* Internal dependencies
77
*/
88
import { toVdom, hydratedIslands } from './vdom';
9-
import { createRootFragment, yieldToMain } from './utils';
9+
import { createRootFragment, splitTask } from './utils';
1010
import { directivePrefix } from './constants';
1111

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

3636
for ( const node of nodes ) {
3737
if ( ! hydratedIslands.has( node ) ) {
38-
await yieldToMain();
38+
await splitTask();
3939
const fragment = getRegionRootFragment( node );
4040
const vdom = toVdom( node );
4141
initialVdom.set( node, vdom );
42-
await yieldToMain();
42+
await splitTask();
4343
hydrate( vdom, fragment );
4444
}
4545
}

packages/interactivity/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const afterNextFrame = ( callback: () => void ) => {
5454
*
5555
* @return Promise
5656
*/
57-
export const yieldToMain = () => {
57+
export const splitTask = () => {
5858
return new Promise( ( resolve ) => {
5959
// TODO: Use scheduler.yield() when available.
6060
setTimeout( resolve, 0 );

0 commit comments

Comments
 (0)