Skip to content

Commit

Permalink
Import __unstableSelectionHasUnmergeableBlock from the correct module…
Browse files Browse the repository at this point in the history
… and add a clear error on invalid destructuring
  • Loading branch information
adamziel committed Sep 29, 2022
1 parent 0ac796d commit 7550dd2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
useSelect,
__experimentalAccessKey as dataExperiments,
} from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { isReusableBlock, getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';
import { unlock } from '../../../experiments';
import {
unlock,
__experimentalAccessKey as blockEditorExperiments,
} from '../../../experiments';

const { __unstableSelectionHasUnmergeableBlock } = unlock( dataExperiments );
const { __unstableSelectionHasUnmergeableBlock } = unlock(
blockEditorExperiments
);

/**
* Returns the class names used for the different states of the block.
Expand Down
9 changes: 6 additions & 3 deletions packages/data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,14 @@ function __experimentalPrivateSelector( { name }, selector ) {
return ( ...args ) =>
selector( defaultRegistry.stores[ name ].store.getState(), ...args );
}
function __experimentalPrivateDispatch( { name }, actionThunk ) {
return defaultRegistry.stores[ name ].store.dispatch( actionThunk );
function __experimentalPrivateActionCreator( { name }, actionCreator ) {
return ( ...args ) =>
defaultRegistry.stores[ name ].store.dispatch(
actionCreator( ...args )
);
}

export const __experimentalAccessKey = experimentalAPIs.register( {
__experimentalPrivateSelector,
__experimentalPrivateDispatch,
__experimentalPrivateActionCreator,
} );
15 changes: 13 additions & 2 deletions packages/experiments/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,20 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
'your product will inevitably break on the next WordPress release.'
);
}
const apis = {};
registeredExperiments[ moduleName ] = {
accessKey: {},
apis: {},
apis,
apisProxy: new Proxy( apis, {
get( target, name ) {
if ( ! ( name in target ) ) {
throw new Error(
`No experimental API with name "${ name }" has been registered under the module ${ moduleName }.`
);
}
return target[ name ];
},
} ),
};
return {
register: ( experiments ) => {
Expand All @@ -73,7 +84,7 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
unlock: ( accessKey ) => {
for ( const experiment of Object.values( registeredExperiments ) ) {
if ( experiment.accessKey === accessKey ) {
return experiment.apis;
return experiment.apisProxy;
}
}

Expand Down
16 changes: 16 additions & 0 deletions packages/experiments/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,20 @@ describe( '__dangerousOptInToUnstableAPIsOnlyForCoreModules', () => {
dataExperimentalFunctions.__experimentalFunction
).toHaveBeenCalled();
} );
it( 'Should throw an exception upon accessing a non-existing experimental API', () => {
const { register, unlock } =
__dangerousOptInToUnstableAPIsOnlyForCoreModules(
requiredConsent,
'@wordpress/block-editor'
);

const accessKey = register( {
fooAPI: true,
} );

expect( unlock( accessKey ).fooAPI ).toBe( true );
expect( () => unlock( accessKey ).noSuchAPI ).toThrowError(
/No experimental API with name "noSuchAPI" has been registered/
);
} );
} );

0 comments on commit 7550dd2

Please sign in to comment.