Skip to content

Commit

Permalink
Data: Allow using store object as argument for select and dispatch (#…
Browse files Browse the repository at this point in the history
…26655)

Co-authored-by: Riad Benguella <benguella@gmail.com>
  • Loading branch information
gziolo and youknowriad authored Nov 17, 2020
1 parent 3f3f310 commit 5c3a32d
Show file tree
Hide file tree
Showing 81 changed files with 685 additions and 355 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/annotations/src/format/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { applyFormat, removeFormat } from '@wordpress/rich-text';
const FORMAT_NAME = 'core/annotation';

const ANNOTATION_ATTRIBUTE_PREFIX = 'annotation-text-';
const STORE_KEY = 'core/annotations';
const STORE_NAME = 'core/annotations';

/**
* Applies given annotations to the given record.
Expand Down Expand Up @@ -145,7 +145,7 @@ export const annotation = {
) {
return {
annotations: select(
STORE_KEY
STORE_NAME
).__experimentalGetAnnotationsForRichText(
blockClientId,
richTextIdentifier
Expand All @@ -165,9 +165,9 @@ export const annotation = {
},
__experimentalGetPropsForEditableTreeChangeHandler( dispatch ) {
return {
removeAnnotation: dispatch( STORE_KEY )
removeAnnotation: dispatch( STORE_NAME )
.__experimentalRemoveAnnotation,
updateAnnotationRange: dispatch( STORE_KEY )
updateAnnotationRange: dispatch( STORE_NAME )
.__experimentalUpdateAnnotationRange,
};
},
Expand Down
3 changes: 2 additions & 1 deletion packages/annotations/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import './store';
import './format';
import './block';

export { store } from './store';
15 changes: 11 additions & 4 deletions packages/annotations/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { register, createReduxStore } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -13,12 +13,19 @@ import * as actions from './actions';
/**
* Module Constants
*/
const MODULE_KEY = 'core/annotations';
const STORE_NAME = 'core/annotations';

const store = registerStore( MODULE_KEY, {
/**
* Store definition for the annotations namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore( STORE_NAME, {
reducer,
selectors,
actions,
} );

export default store;
register( store );
3 changes: 2 additions & 1 deletion packages/block-directory/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import '@wordpress/notices';
/**
* Internal dependencies
*/
import './store';
import './plugins';

export { store } from './store';
15 changes: 11 additions & 4 deletions packages/block-directory/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { createReduxStore, register } from '@wordpress/data';
import { controls as dataControls } from '@wordpress/data-controls';

/**
Expand All @@ -16,7 +16,7 @@ import controls from './controls';
/**
* Module Constants
*/
const MODULE_KEY = 'core/block-directory';
const STORE_NAME = 'core/block-directory';

/**
* Block editor data store configuration.
Expand All @@ -33,6 +33,13 @@ export const storeConfig = {
resolvers,
};

const store = registerStore( MODULE_KEY, storeConfig );
/**
* Store definition for the block directory namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore( STORE_NAME, storeConfig );

export default store;
register( store );
12 changes: 12 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,18 @@ _Properties_

Undocumented declaration.

<a name="store" href="#store">#</a> **store**

Store definition for the block editor namespace.

_Related_

- <https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore>

_Type_

- `Object`

<a name="storeConfig" href="#storeConfig">#</a> **storeConfig**

Block editor data store configuration.
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@wordpress/shortcode": "file:../shortcode",
"@wordpress/token-list": "file:../token-list",
"@wordpress/url": "file:../url",
"@wordpress/viewport": "file:../viewport",
"@wordpress/warning": "file:../warning",
"@wordpress/wordcount": "file:../wordcount",
"classnames": "^2.2.5",
Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import '@wordpress/blocks';
import '@wordpress/rich-text';
import '@wordpress/viewport';
import '@wordpress/keyboard-shortcuts';
import '@wordpress/notices';

Expand All @@ -13,5 +12,5 @@ import '@wordpress/notices';
import './hooks';
export * from './components';
export * from './utils';
export { storeConfig } from './store';
export { storeConfig, store } from './store';
export { SETTINGS_DEFAULTS } from './store/defaults';
23 changes: 18 additions & 5 deletions packages/block-editor/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { createReduxStore, registerStore } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -15,7 +15,7 @@ import controls from './controls';
/**
* Module Constants
*/
const MODULE_KEY = 'core/block-editor';
const STORE_NAME = 'core/block-editor';

/**
* Block editor data store configuration.
Expand All @@ -31,10 +31,23 @@ export const storeConfig = {
controls,
};

const store = registerStore( MODULE_KEY, {
/**
* Store definition for the block editor namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );
applyMiddlewares( store );

export default store;
// Ideally we'd use register instead of register stores.
// We should be able to make the switch once we remove the "effects" middleware.
// We also need a more generic way of defining persistence and not rely on a plugin.
const instantiatedStore = registerStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );
applyMiddlewares( instantiatedStore );
12 changes: 12 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,18 @@ _Parameters_

- _blockName_ `string`: Block name.

<a name="store" href="#store">#</a> **store**

Store definition for the blocks namespace.

_Related_

- <https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore>

_Type_

- `Object`

<a name="switchToBlockType" href="#switchToBlockType">#</a> **switchToBlockType**

Switch one or more blocks into one or more blocks of the new block type.
Expand Down
6 changes: 1 addition & 5 deletions packages/blocks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
// Blocks are inferred from the HTML source of a post through a parsing mechanism
// and then stored as objects in state, from which it is then rendered for editing.

/**
* Internal dependencies
*/
import './store';

export { store } from './store';
export * from './api';
export { withBlockContentContext } from './block-content-provider';
19 changes: 17 additions & 2 deletions packages/blocks/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { createReduxStore, register } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -10,4 +10,19 @@ import reducer from './reducer';
import * as selectors from './selectors';
import * as actions from './actions';

registerStore( 'core/blocks', { reducer, selectors, actions } );
const STORE_NAME = 'core/blocks';

/**
* Store definition for the blocks namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore( STORE_NAME, {
reducer,
selectors,
actions,
} );

register( store );
18 changes: 14 additions & 4 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { createReduxStore, register } from '@wordpress/data';
import { controls } from '@wordpress/data-controls';

/**
Expand All @@ -14,7 +14,7 @@ import * as resolvers from './resolvers';
import * as locksSelectors from './locks/selectors';
import * as locksActions from './locks/actions';
import { defaultEntities, getMethodName } from './entities';
import { REDUCER_KEY } from './name';
import { STORE_NAME } from './name';

// The entity selectors/resolvers and actions are shortcuts to their generic equivalents
// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecordss)
Expand Down Expand Up @@ -56,14 +56,24 @@ const entityActions = defaultEntities.reduce( ( result, entity ) => {
return result;
}, {} );

export const storeConfig = {
const storeConfig = {
reducer,
controls,
actions: { ...actions, ...entityActions, ...locksActions },
selectors: { ...selectors, ...entitySelectors, ...locksSelectors },
resolvers: { ...resolvers, ...entityResolvers },
};
registerStore( REDUCER_KEY, storeConfig );

/**
* Store definition for the code data namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/master/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore( STORE_NAME, storeConfig );

register( store );

export { default as EntityProvider } from './entity-provider';
export * from './entity-provider';
2 changes: 1 addition & 1 deletion packages/core-data/src/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
*
* @type {string}
*/
export const REDUCER_KEY = 'core';
export const STORE_NAME = 'core';
6 changes: 3 additions & 3 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { REDUCER_KEY } from './name';
import { STORE_NAME } from './name';
import { getQueriedItems } from './queried-data';
import { DEFAULT_ENTITY_KEY } from './entities';
import { getNormalizedCommaSeparable } from './utils';
Expand Down Expand Up @@ -41,7 +41,7 @@ const EMPTY_ARRAY = [];
export const isRequestingEmbedPreview = createRegistrySelector(
( select ) => ( state, url ) => {
return select( 'core/data' ).isResolving(
REDUCER_KEY,
STORE_NAME,
'getEmbedPreview',
[ url ]
);
Expand Down Expand Up @@ -692,7 +692,7 @@ export function getAutosave( state, postType, postId, authorId ) {
*/
export const hasFetchedAutosaves = createRegistrySelector(
( select ) => ( state, postType, postId ) => {
return select( REDUCER_KEY ).hasFinishedResolution( 'getAutosaves', [
return select( STORE_NAME ).hasFinishedResolution( 'getAutosaves', [
postType,
postId,
] );
Expand Down
6 changes: 3 additions & 3 deletions packages/core-data/src/test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createRegistry, controls } from '@wordpress/data';
import * as actions from '../actions';
import * as selectors from '../selectors';
import * as resolvers from '../resolvers';
import { storeConfig as coreStoreConfig } from '../';
import { store } from '../';

// Mock to prevent calling window.fetch in test environment
jest.mock( '@wordpress/data-controls', () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe( 'receiveEntityRecord', () => {
data: {},
},
};
registry.registerStore( 'core', coreStoreConfig );
registry.register( store );
registry.registerStore( 'test/resolution', {
actions: {
receiveEntityRecords: actions.receiveEntityRecords,
Expand Down Expand Up @@ -160,7 +160,7 @@ describe( 'receiveEntityRecord', () => {
describe( 'saveEntityRecord', () => {
function createTestRegistry() {
const registry = createRegistry();
registry.registerStore( 'core', coreStoreConfig );
registry.register( store );
return registry;
}

Expand Down
Loading

0 comments on commit 5c3a32d

Please sign in to comment.