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

Interactivity: Ensure stores are initialized on client #59842

Merged
merged 6 commits into from
Mar 14, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "test/deferred-store",
"title": "E2E Interactivity tests - deferred store",
"category": "text",
"icon": "heart",
"description": "",
"supports": {
"interactivity": true
},
"textdomain": "e2e-interactivity",
"viewScriptModule": "file:./view.js",
"render": "file:./render.php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* HTML for testing scope restoration with generators.
*
* @package gutenberg-test-interactive-blocks
*/
?>

<div
data-wp-interactive="test/deferred-store"
<?php echo wp_interactivity_data_wp_context( array( 'text' => '!dlrow ,olleH' ) ); ?>
>
<span data-wp-text="state.reversedText" data-testid="result"></span>
<span data-wp-text="state.reversedTextGetter" data-testid="result-getter"></span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array( 'dependencies' => array( '@wordpress/interactivity' ) );
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { store, getContext } from '@wordpress/interactivity';

document.addEventListener( 'DOMContentLoaded', () => {
setTimeout( () => {
store( 'test/deferred-store', {
state: {
reversedText() {
return [ ...getContext().text ].reverse().join( '' );
},

get reversedTextGetter() {
return [ ...getContext().text ].reverse().join( '' );
},
},
} );
}, 50 );
} );
1 change: 1 addition & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug Fixes

- Ensure that stores are available for subscription before hydration. ([#59842](https://github.com/WordPress/gutenberg/pull/59842))
- Ensure scope is restored when catching exceptions thrown in async generator actions. ([#59708](https://github.com/WordPress/gutenberg/pull/59708))

## 5.2.0 (2024-03-06)
Expand Down
10 changes: 8 additions & 2 deletions packages/interactivity/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { VNode, Context, RefObject } from 'preact';
/**
* Internal dependencies
*/
import { stores } from './store';
import { store, stores, universalUnlock } from './store';
interface DirectiveEntry {
value: string | Object;
namespace: string;
Expand Down Expand Up @@ -259,8 +259,14 @@ export const directive = (

// Resolve the path to some property of the store object.
const resolve = ( path, namespace ) => {
let resolvedStore = stores.get( namespace );
if ( typeof resolvedStore === 'undefined' ) {
resolvedStore = store( namespace, undefined, {
lock: universalUnlock,
} );
}
let current = {
...stores.get( namespace ),
...resolvedStore,
context: getScope().context[ namespace ],
};
path.split( '.' ).forEach( ( p ) => ( current = current[ p ] ) );
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ interface StoreOptions {
lock?: boolean | string;
}

const universalUnlock =
export const universalUnlock =
'I acknowledge that using a private store means my plugin will inevitably break on the next store release.';

/**
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/specs/interactivity/deferred-store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Internal dependencies
*/
import { test, expect } from './fixtures';

test.describe( 'deferred store', () => {
test.beforeAll( async ( { interactivityUtils: utils } ) => {
await utils.activatePlugins();
await utils.addPostWithBlock( 'test/deferred-store' );
} );
test.beforeEach( async ( { interactivityUtils: utils, page } ) => {
await page.goto( utils.getLink( 'test/deferred-store' ) );
} );
test.afterAll( async ( { interactivityUtils: utils } ) => {
await utils.deactivatePlugins();
await utils.deleteAllPosts();
} );

test( 'Ensure that a store can be subscribed to before it is initialized', async ( {
page,
} ) => {
const resultInput = page.getByTestId( 'result' );
await expect( resultInput ).toHaveText( '' );
await expect( resultInput ).toHaveText( 'Hello, world!' );
} );

// There is a known issue for deferred getters right now.
// eslint-disable-next-line playwright/no-skipped-test
test.skip( 'Ensure that a state getter can be subscribed to before it is initialized', async ( {
page,
} ) => {
const resultInput = page.getByTestId( 'result-getter' );
await expect( resultInput ).toHaveText( '' );
await expect( resultInput ).toHaveText( 'Hello, world!' );
} );
} );
Loading