-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Private experimental cross-module selectors and actions #44521
Merged
adamziel
merged 13 commits into
experiments/pivot-to-lock-unlock
from
experiments/private-selectors-and-actions
Dec 22, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7239c0a
one commit on yourBranch
adamziel 12881d2
Rm vscode settings
adamziel d07dc8a
Remove the obsolete block-library/src/experiments.js file
adamziel 729426b
Restore husky pre-commit hook
adamziel 27552b3
Handle private actions and selectors via specialized registerPrivateS…
adamziel 1eecb63
Replace privateOf() with a generic unlock()
adamziel babf98f
Adjust package-lock.json
adamziel 629f496
Document the experimental APIs
adamziel 7d85610
Regenerate the docs
adamziel c157794
Clean up the exports
adamziel e444d1d
Adjust blocks unit tests
adamziel aba188c
Simplify the `@wordpress/experiments` API
adamziel 9191851
Merge branch 'experiments/pivot-to-lock-unlock' into experiments/priv…
adamziel 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,9 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments'; | ||
|
||
export const { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( | ||
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', | ||
'@wordpress/block-editor' | ||
); |
This file contains 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 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 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,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments'; | ||
import { experiments as dataExperiments } from '@wordpress/data'; | ||
|
||
export const { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( | ||
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', | ||
'@wordpress/blocks' | ||
); | ||
|
||
export const { registerPrivateSelectors } = unlock( dataExperiments ); |
This file contains 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 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 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 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,104 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments'; | ||
|
||
export const { lock, unlock } = | ||
__dangerousOptInToUnstableAPIsOnlyForCoreModules( | ||
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', | ||
'@wordpress/data' | ||
); | ||
|
||
/** | ||
* Enables registering private actions and selectors on a store without exposing | ||
* them as public API. | ||
* | ||
* Use it with the store descriptor object: | ||
* | ||
* ```js | ||
* const store = createReduxStore( 'my-store', { ... } ); | ||
* registerPrivateActionsAndSelectors( store, { | ||
* __experimentalAction: ( state ) => state.value, | ||
* }, { | ||
* __experimentalSelector: ( state ) => state.value, | ||
* } ); | ||
* ``` | ||
* | ||
* Once the selectors are registered, they can be accessed using the | ||
* `unlock()` utility: | ||
* | ||
* ```js | ||
* unlock( registry.dispatch( blockEditorStore ) ).__experimentalAction(); | ||
* unlock( registry.select( blockEditorStore ) ).__experimentalSelector(); | ||
* ``` | ||
* | ||
* Note the objects returned by select() and dispatch() have the good old public | ||
* methods, but the modules that opted-in to the private APIs can also "unlock" | ||
* additional private selectors and actions. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* // In the package exposing the private selectors: | ||
* | ||
* import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/experiments'; | ||
* export const { lock, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( /* ... *\/ ); | ||
* | ||
* import { experiments as dataExperiments } from '@wordpress/data'; | ||
* const { registerPrivateActionsAndSelectors } = unlock( dataExperiments ); | ||
* | ||
* const store = registerStore( 'my-store', { /* ... *\/ } ); | ||
* registerPrivateActionsAndSelectors( store, { | ||
* __experimentalAction: ( state ) => state.value, | ||
* }, { | ||
* __experimentalSelector: ( state ) => state.value, | ||
* } ); | ||
* | ||
* // In the package using the private selectors: | ||
* import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
* unlock( registry.select( blockEditorStore ) ).__experimentalSelector(); | ||
* | ||
* // Or in a React component: | ||
* useSelect( ( select ) => ( | ||
* unlock( select( blockEditorStore ) ).__experimentalSelector() | ||
* ) ); | ||
* useDispatch( ( dispatch ) => { | ||
* unlock( dispatch( blockEditorStore ) ).__experimentalAction() | ||
* } ); | ||
* ``` | ||
* | ||
* @param {Object} store The store descriptor to register the private selectors on. | ||
* @param {Object} actions The private actions to register in a { actionName: ( ...args ) => action } format. | ||
* @param {Object} selectors The private selectors to register in a { selectorName: ( state, ...args ) => data } format. | ||
*/ | ||
export function registerPrivateActionsAndSelectors( | ||
store, | ||
actions = {}, | ||
selectors = {} | ||
) { | ||
lock( store, { actions, selectors } ); | ||
} | ||
|
||
/** | ||
* The experimental APIs exposed by the `@wordpress/data` package. | ||
* Only available to core packages. These APIs are not stable and may | ||
* change without notice. Do not use outside of core. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { unlock } from '../experiments'; | ||
* import { experiments as dataExperiments } from '@wordpress/data'; | ||
* const { registerPrivateSelectors } = unlock( dataExperiments ); | ||
* | ||
* import { store as blockEditorStore } from './store'; | ||
* import { __unstableSelectionHasUnmergeableBlock } from './store/selectors'; | ||
* registerPrivateSelectors( store, { | ||
* __experimentalHasContentRoleAttribute | ||
* } ); | ||
* ``` | ||
*/ | ||
export const experiments = {}; | ||
lock( experiments, { | ||
registerPrivateActionsAndSelectors, | ||
} ); |
This file contains 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
Oops, something went wrong.
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.
These selectors don't match, which adds confusion.
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.
ah snap good spot, I'll fix this in #46131
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.
8b5103c