Skip to content

Commit

Permalink
Amend callback in withSyncEvent instead of wrapping it.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Feb 10, 2025
1 parent dff811c commit f0fa92a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/interactivity-api/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ store( 'mySliderPlugin', {

### withSyncEvent()

Actions that require synchronous access to the `event` object need to use the `withSyncEvent()` function to wrap their handler callback. This is necessary due to an ongoing effort to handle store actions asynchronously by default, unless they require synchronous event access. Therefore, as of Gutenberg `TODO: Add release number here!` / WordPress 6.8 all actions that require synchronous event access need to use the `withSyncEvent()` utility wrapper function. Otherwise a deprecation warning will be triggered, and in a future release the behavior will change accordingly.
Actions that require synchronous access to the `event` object need to use the `withSyncEvent()` function to annotate their handler callback. This is necessary due to an ongoing effort to handle store actions asynchronously by default, unless they require synchronous event access. Therefore, as of Gutenberg `TODO: Add release number here!` / WordPress 6.8 all actions that require synchronous event access need to use the `withSyncEvent()` function. Otherwise a deprecation warning will be triggered, and in a future release the behavior will change accordingly.

Only very specific event methods and properties require synchronous access, so it is advised to only use `withSyncEvent()` when necessary. The following event methods and properties require synchronous access:

Expand Down
19 changes: 4 additions & 15 deletions packages/interactivity/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,10 @@ export const isPlainObject = (
* Indicates that the passed `callback` requires synchronous access to the event object.
*
* @param callback The event callback.
* @return Wrapped event callback.
* @return Altered event callback.
*/
export function withSyncEvent( callback: Function ): SyncAwareFunction {
let wrapped: SyncAwareFunction;

if ( callback?.constructor?.name === 'GeneratorFunction' ) {
wrapped = function* ( this: any, ...args: any[] ) {
yield* callback.apply( this, args );
};
} else {
wrapped = function ( this: any, ...args: any[] ) {
return callback.apply( this, args );
};
}

wrapped.sync = true;
return wrapped;
const syncAware = callback as SyncAwareFunction;
syncAware.sync = true;
return syncAware;
}

0 comments on commit f0fa92a

Please sign in to comment.