-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Interactivity: Restore scope when yielded promise rejects #59708
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
60214e9
Add test case
sirreal ecb2534
Fix scoping behavior
sirreal cfaa0a7
Restore 2 try/catch
sirreal 382a924
Fix render comment
sirreal e63f548
Exclude *.asset.php files from the hyphen naming rule
sirreal b14634d
Fix lint
sirreal 3148333
Improve exclude pattern
sirreal 04fd91b
Fix alignment
sirreal 09889c5
Use consistent HTML quoting
sirreal 443983a
Prettier view file
sirreal fa01570
Rename test file
sirreal 242f138
Add changelog
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/e2e-tests/plugins/interactive-blocks/generator-scope/block.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/generator-scope", | ||
| "title": "E2E Interactivity tests - generator scope", | ||
| "category": "text", | ||
| "icon": "heart", | ||
| "description": "", | ||
| "supports": { | ||
| "interactivity": true | ||
| }, | ||
| "textdomain": "e2e-interactivity", | ||
| "viewScriptModule": "file:./view.js", | ||
| "render": "file:./render.php" | ||
| } |
16 changes: 16 additions & 0 deletions
16
packages/e2e-tests/plugins/interactive-blocks/generator-scope/render.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
| /** | ||
| * HTML for testing scope restoration with generators. | ||
| * | ||
| * @package gutenberg-test-interactive-blocks | ||
| */ | ||
| ?> | ||
|
|
||
| <div | ||
| data-wp-interactive="test/generator-scope" | ||
| <?php echo wp_interactivity_data_wp_context( array( 'result' => '' ) ); ?> | ||
| > | ||
| <input readonly data-wp-bind--value="context.result" data-testid="result" /> | ||
| <button type="button" data-wp-on--click="callbacks.resolve" data-testid="resolve">Async resolve</button> | ||
| <button type="button" data-wp-on--click="callbacks.reject" data-testid="reject">Async reject</button> | ||
| </div> |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/plugins/interactive-blocks/generator-scope/view.asset.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <?php return array( 'dependencies' => array( '@wordpress/interactivity' ) ); |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/generator-scope/view.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { store, getContext } from '@wordpress/interactivity'; | ||
|
|
||
| store( 'test/generator-scope', { | ||
| callbacks: { | ||
| *resolve() { | ||
| try { | ||
| getContext().result = yield Promise.resolve( 'ok' ); | ||
| } catch ( err ) { | ||
| getContext().result = err.toString(); | ||
| } | ||
| }, | ||
| *reject() { | ||
| try { | ||
| getContext().result = yield Promise.reject( new Error( '😘' ) ); | ||
| } catch ( err ) { | ||
| getContext().result = err.toString(); | ||
| } | ||
| }, | ||
| }, | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,7 @@ | |
| <!-- Exclude PHPUnit tests from file and class name sniffs (for Core parity). --> | ||
| <rule ref="WordPress.Files.FileName.NotHyphenatedLowercase"> | ||
| <exclude-pattern>/phpunit/*</exclude-pattern> | ||
| <exclude-pattern>*\.asset\.php$</exclude-pattern> | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #59705 (comment) |
||
| </rule> | ||
| <rule ref="PEAR.NamingConventions.ValidClassName.Invalid"> | ||
| <exclude-pattern>/phpunit/*</exclude-pattern> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { test, expect } from './fixtures'; | ||
|
|
||
| test.describe( 'async actions', () => { | ||
| test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
| await utils.activatePlugins(); | ||
| await utils.addPostWithBlock( 'test/generator-scope' ); | ||
| } ); | ||
| test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
| await page.goto( utils.getLink( 'test/generator-scope' ) ); | ||
| } ); | ||
| test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
| await utils.deactivatePlugins(); | ||
| await utils.deleteAllPosts(); | ||
| } ); | ||
|
|
||
| test( 'Promise generator callbacks should restore scope on resolve and reject', async ( { | ||
| page, | ||
| } ) => { | ||
| const resultInput = page.getByTestId( 'result' ); | ||
| await expect( resultInput ).toHaveValue( '' ); | ||
|
|
||
| await page.getByTestId( 'resolve' ).click(); | ||
| await expect( resultInput ).toHaveValue( 'ok' ); | ||
|
|
||
| await page.getByTestId( 'reject' ).click(); | ||
| await expect( resultInput ).toHaveValue( 'Error: 😘' ); | ||
| } ); | ||
| } ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DAreRodz I tried 2 different approaches and they both fixed my test case. I wasn't sure why we have 2 try blocks, so my first version merge the try/finally and try/catch into one try/catch/finally. Then, since I wasn't sure what the motivation for 2 blocks were, I changed use 2 blocks. I'd like to get your insight here.
Both versions are on this branch in different commits and can be reviewed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the best approach: to have two separate try-catch blocks. The thing with async actions is that they can change the scope at any time―whenever they run―, thus leading to unexpected results when the scope is reset.
We decided here to set/reset the scope for synchronous code fragments, i.e., the generator code from one
yieldto the next one. So, from my understanding, we should reset the scope, then wait for the promise to be fulfilled (which will probably set their own scope), and if it fails, then set/reset the scope again.