-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Experiments: sharing private APIs with lock() and unlock() #46131
Commits on Dec 22, 2022
-
Experiments: pivot towards lock()/unlock() based API
This commit introduces a more convenient API for managing the private experiments. The idea is to "lock" private data inside public objects: ```js const { lock, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( '<CONSENT STRING>', '@wordpress/blocks' ); export const publicObject = {}; lock( __experiments, "Shh, private data!" ); publicObject // {} unlock( publicObject ) // "Shh, private data!" ``` The lock()/unlock() API makes it easier to implement private functions, components, selectors, actions etc. Instead of using a separate import system, it's enough to opt-in to the experiments and then `unlock()` the publicly available artifact. For example: ```js import { store } from '@wordpress/blocks'; import { useSelect } from '@wordpress/data'; import { unlock } from './experiments'; function MyComponent() { const hasRole = useSelect( ( select ) => ( unlock( select( store ) ).__experimentalHasContentRoleAttribute() ) ); // ... } ``` This makes `unlock()` a drop-in replacement that does not require reworking the current code structure around the experimental APIs. What's also nice is that the above implementation of private selectors (coming in a follow-up PR) is short, sweet, and fairly easy to grasp. For comparison, an implementation based on the former register/unlock API required quite a bit of mental gymnastics: ```js // Package wordpress/block-editor: export const { register, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', '@wordpress/block-editor' ); const { __experimentalPrivateSelector } = unlock( dataExperiments ); import { __unstableSelectionHasUnmergeableBlock, } from './store/selectors'; export const __experimentalAccessKey = register( { __unstableSelectionHasUnmergeableBlock: __experimentalPrivateSelector( store, __unstableSelectionHasUnmergeableBlock ) } ); // Package wordpress/edit-page export const { register, unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules( 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', '@wordpress/block-editor' ); import { __experimentalAccessKey as blockEditorExperiments } from '@wordpress/block-editor'; const { __unstableSelectionHasUnmergeableBlock } = unlock(__experimentalAccessKey); ``` It also had an undesirable side-effect of requiring a separate import and a different treatment of the `__unstableSelectionHasUnmergeableBlock()` selector – which is to say a migration would be rather difficult.
Configuration menu - View commit details
-
Copy full SHA for 982b862 - Browse repository at this point
Copy the full SHA 982b862View commit details -
Configuration menu - View commit details
-
Copy full SHA for bb2e1e0 - Browse repository at this point
Copy the full SHA bb2e1e0View commit details -
Configuration menu - View commit details
-
Copy full SHA for 48fd5a4 - Browse repository at this point
Copy the full SHA 48fd5a4View commit details -
Add unit tests to demonstrate how to export private functions, functi…
…on arguments, and react components
Configuration menu - View commit details
-
Copy full SHA for 65750cf - Browse repository at this point
Copy the full SHA 65750cfView commit details -
Configuration menu - View commit details
-
Copy full SHA for 12bbcb6 - Browse repository at this point
Copy the full SHA 12bbcb6View commit details -
Configuration menu - View commit details
-
Copy full SHA for 2a706c3 - Browse repository at this point
Copy the full SHA 2a706c3View commit details -
Configuration menu - View commit details
-
Copy full SHA for 3efd500 - Browse repository at this point
Copy the full SHA 3efd500View commit details -
Configuration menu - View commit details
-
Copy full SHA for 435ea50 - Browse repository at this point
Copy the full SHA 435ea50View commit details -
Configuration menu - View commit details
-
Copy full SHA for 75ec4fd - Browse repository at this point
Copy the full SHA 75ec4fdView commit details -
Private experimental cross-module selectors and actions (#44521)
Introduces a private selectors APIs in `@wordpress/data` via [the new `@wordpress/experimental`](#43386 (comment)) package: ```js // Package wordpress/block-data: import { unlock } from '../experiments'; import { experiments as dataExperiments } from '@wordpress/data'; const { registerPrivateActionsAndSelectors } = unlock( dataExperiments ); import { store as blockEditorStore } from './store'; import { __unstableSelectionHasUnmergeableBlock } from './store/selectors'; registerPrivateActionsAndSelectors( store, {}, { __experimentalHasContentRoleAttribute } ); // plain usage unlock( registry.select( blockEditorStore ) ).getContentLockingParent(); // usage in React useSelect( ( select ) => ( { parent: privateOf( select( blockEditorStore ) ).__unstableSelectionHasUnmergeableBlock(); } ) ); ```
Configuration menu - View commit details
-
Copy full SHA for 7f1b499 - Browse repository at this point
Copy the full SHA 7f1b499View commit details -
Configuration menu - View commit details
-
Copy full SHA for 6933b2a - Browse repository at this point
Copy the full SHA 6933b2aView commit details -
1
Configuration menu - View commit details
-
Copy full SHA for f8e199f - Browse repository at this point
Copy the full SHA f8e199fView commit details -
1
Configuration menu - View commit details
-
Copy full SHA for cb11084 - Browse repository at this point
Copy the full SHA cb11084View commit details -
1
Configuration menu - View commit details
-
Copy full SHA for 8b5103c - Browse repository at this point
Copy the full SHA 8b5103cView commit details -
1
Configuration menu - View commit details
-
Copy full SHA for 00a07b8 - Browse repository at this point
Copy the full SHA 00a07b8View commit details -
1
Configuration menu - View commit details
-
Copy full SHA for ce9d2a2 - Browse repository at this point
Copy the full SHA ce9d2a2View commit details
Commits on Dec 23, 2022
-
Update packages/experiments/README.md
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
Configuration menu - View commit details
-
Copy full SHA for 9a01309 - Browse repository at this point
Copy the full SHA 9a01309View commit details
Commits on Jan 16, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 22369b7 - Browse repository at this point
Copy the full SHA 22369b7View commit details -
Configuration menu - View commit details
-
Copy full SHA for eaff546 - Browse repository at this point
Copy the full SHA eaff546View commit details
Commits on Jan 17, 2023
-
Update packages/experiments/src/implementation.js
Co-authored-by: Robert Anderson <robert@noisysocks.com>
Configuration menu - View commit details
-
Copy full SHA for 41582b7 - Browse repository at this point
Copy the full SHA 41582b7View commit details -
Update packages/experiments/src/implementation.js
Co-authored-by: Robert Anderson <robert@noisysocks.com>
Configuration menu - View commit details
-
Copy full SHA for c0ebbf8 - Browse repository at this point
Copy the full SHA c0ebbf8View commit details -
Clarify the meaning of the second argument to __dangerousOptInToUnsta…
…bleAPIsOnlyForCoreModules
Configuration menu - View commit details
-
Copy full SHA for 583fd45 - Browse repository at this point
Copy the full SHA 583fd45View commit details -
Assert that the public selector and actions still exist on the object…
… returned by unlock().
Configuration menu - View commit details
-
Copy full SHA for 954b353 - Browse repository at this point
Copy the full SHA 954b353View commit details