diff --git a/packages/core-data/src/actions.js b/packages/core-data/src/actions.js index 8bf6d40b8c5c74..364d1d3a680528 100644 --- a/packages/core-data/src/actions.js +++ b/packages/core-data/src/actions.js @@ -22,6 +22,7 @@ import { } from './locks'; import { createBatch } from './batch'; import { getDispatch } from './controls'; +import { CORE_STORE_NAME as coreStoreName } from './utils/constants'; /** * Returns an action object used in signalling that authors have been received. @@ -182,7 +183,7 @@ export function* deleteEntityRecord( } const lock = yield* __unstableAcquireStoreLock( - 'core', + coreStoreName, [ 'entities', 'data', kind, name, recordId ], { exclusive: true } ); @@ -246,7 +247,12 @@ export function* deleteEntityRecord( * @return {Object} Action object. */ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) { - const entity = yield controls.select( 'core', 'getEntity', kind, name ); + const entity = yield controls.select( + coreStoreName, + 'getEntity', + kind, + name + ); if ( ! entity ) { throw new Error( `The entity being edited (${ kind }, ${ name }) does not have a loaded config.` @@ -254,14 +260,14 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) { } const { transientEdits = {}, mergedEdits = {} } = entity; const record = yield controls.select( - 'core', + coreStoreName, 'getRawEntityRecord', kind, name, recordId ); const editedRecord = yield controls.select( - 'core', + coreStoreName, 'getEditedEntityRecord', kind, name, @@ -306,7 +312,7 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) { * an entity record, if any. */ export function* undo() { - const undoEdit = yield controls.select( 'core', 'getUndoEdit' ); + const undoEdit = yield controls.select( coreStoreName, 'getUndoEdit' ); if ( ! undoEdit ) { return; } @@ -324,7 +330,7 @@ export function* undo() { * edit to an entity record, if any. */ export function* redo() { - const redoEdit = yield controls.select( 'core', 'getRedoEdit' ); + const redoEdit = yield controls.select( coreStoreName, 'getRedoEdit' ); if ( ! redoEdit ) { return; } @@ -374,7 +380,7 @@ export function* saveEntityRecord( const recordId = record[ entityIdKey ]; const lock = yield* __unstableAcquireStoreLock( - 'core', + coreStoreName, [ 'entities', 'data', kind, name, recordId || uuid() ], { exclusive: true } ); @@ -385,7 +391,7 @@ export function* saveEntityRecord( if ( typeof value === 'function' ) { const evaluatedValue = value( yield controls.select( - 'core', + coreStoreName, 'getEditedEntityRecord', kind, name, @@ -419,7 +425,7 @@ export function* saveEntityRecord( recordId ? '/' + recordId : '' }`; const persistedRecord = yield controls.select( - 'core', + coreStoreName, 'getRawEntityRecord', kind, name, @@ -432,12 +438,12 @@ export function* saveEntityRecord( // but ideally this should all be handled in the back end, // so the client just sends and receives objects. const currentUser = yield controls.select( - 'core', + coreStoreName, 'getCurrentUser' ); const currentUserId = currentUser ? currentUser.id : undefined; const autosavePost = yield controls.select( - 'core', + coreStoreName, 'getAutosave', persistedRecord.type, persistedRecord.id, @@ -608,15 +614,20 @@ export function* __experimentalBatch( requests ) { const api = { saveEntityRecord( kind, name, record, options ) { return batch.add( ( add ) => - dispatch( 'core' ).saveEntityRecord( kind, name, record, { - ...options, - __unstableFetch: add, - } ) + dispatch( coreStoreName ).saveEntityRecord( + kind, + name, + record, + { + ...options, + __unstableFetch: add, + } + ) ); }, saveEditedEntityRecord( kind, name, recordId, options ) { return batch.add( ( add ) => - dispatch( 'core' ).saveEditedEntityRecord( + dispatch( coreStoreName ).saveEditedEntityRecord( kind, name, recordId, @@ -629,7 +640,7 @@ export function* __experimentalBatch( requests ) { }, deleteEntityRecord( kind, name, recordId, query, options ) { return batch.add( ( add ) => - dispatch( 'core' ).deleteEntityRecord( + dispatch( coreStoreName ).deleteEntityRecord( kind, name, recordId, @@ -660,7 +671,7 @@ export function* __experimentalBatch( requests ) { export function* saveEditedEntityRecord( kind, name, recordId, options ) { if ( ! ( yield controls.select( - 'core', + coreStoreName, 'hasEditsForEntityRecord', kind, name, @@ -670,7 +681,7 @@ export function* saveEditedEntityRecord( kind, name, recordId, options ) { return; } const edits = yield controls.select( - 'core', + coreStoreName, 'getEntityRecordNonTransientEdits', kind, name, @@ -698,7 +709,7 @@ export function* __experimentalSaveSpecifiedEntityEdits( ) { if ( ! ( yield controls.select( - 'core', + coreStoreName, 'hasEditsForEntityRecord', kind, name, @@ -708,7 +719,7 @@ export function* __experimentalSaveSpecifiedEntityEdits( return; } const edits = yield controls.select( - 'core', + coreStoreName, 'getEntityRecordNonTransientEdits', kind, name, diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index b315e20cd38c8d..741e6696e3ed0e 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -14,6 +14,7 @@ import { __ } from '@wordpress/i18n'; * Internal dependencies */ import { addEntities } from './actions'; +import { CORE_STORE_NAME as coreStoreName } from './utils/constants'; export const DEFAULT_ENTITY_KEY = 'id'; @@ -247,7 +248,11 @@ export const getMethodName = ( * @return {Array} Entities */ export function* getKindEntities( kind ) { - let entities = yield controls.select( 'core', 'getEntitiesByKind', kind ); + let entities = yield controls.select( + coreStoreName, + 'getEntitiesByKind', + kind + ); if ( entities && entities.length !== 0 ) { return entities; } diff --git a/packages/core-data/src/entity-provider.js b/packages/core-data/src/entity-provider.js index 5957848ee66d7b..3af512e04152d9 100644 --- a/packages/core-data/src/entity-provider.js +++ b/packages/core-data/src/entity-provider.js @@ -10,6 +10,11 @@ import { import { useSelect, useDispatch } from '@wordpress/data'; import { parse, __unstableSerializeAndClean } from '@wordpress/blocks'; +/** + * Internal dependencies + */ +import { CORE_STORE_NAME as coreStoreName } from './utils/constants'; + const EMPTY_ARRAY = []; /** @@ -91,7 +96,9 @@ export function useEntityProp( kind, type, prop, _id ) { const { value, fullValue } = useSelect( ( select ) => { - const { getEntityRecord, getEditedEntityRecord } = select( 'core' ); + const { getEntityRecord, getEditedEntityRecord } = select( + coreStoreName + ); const entity = getEntityRecord( kind, type, id ); // Trigger resolver. const editedEntity = getEditedEntityRecord( kind, type, id ); return entity && editedEntity @@ -103,7 +110,7 @@ export function useEntityProp( kind, type, prop, _id ) { }, [ kind, type, id, prop ] ); - const { editEntityRecord } = useDispatch( 'core' ); + const { editEntityRecord } = useDispatch( coreStoreName ); const setValue = useCallback( ( newValue ) => { editEntityRecord( kind, type, id, { @@ -139,7 +146,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) { const id = _id ?? providerId; const { content, blocks } = useSelect( ( select ) => { - const { getEditedEntityRecord } = select( 'core' ); + const { getEditedEntityRecord } = select( coreStoreName ); const editedEntity = getEditedEntityRecord( kind, type, id ); return { blocks: editedEntity.blocks, @@ -149,7 +156,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) { [ kind, type, id ] ); const { __unstableCreateUndoLevel, editEntityRecord } = useDispatch( - 'core' + coreStoreName ); useEffect( () => { diff --git a/packages/core-data/src/locks/actions.js b/packages/core-data/src/locks/actions.js index 2689b35bdf0a7b..e0a54311e3fea1 100644 --- a/packages/core-data/src/locks/actions.js +++ b/packages/core-data/src/locks/actions.js @@ -4,6 +4,11 @@ import { __unstableAwaitPromise } from '@wordpress/data-controls'; import { controls } from '@wordpress/data'; +/** + * Internal dependencies + */ +import { CORE_STORE_NAME as coreStoreName } from '../utils/constants'; + export function* __unstableAcquireStoreLock( store, path, { exclusive } ) { const promise = yield* __unstableEnqueueLockRequest( store, path, { exclusive, @@ -37,13 +42,13 @@ export function* __unstableProcessPendingLockRequests() { type: 'PROCESS_PENDING_LOCK_REQUESTS', }; const lockRequests = yield controls.select( - 'core', + coreStoreName, '__unstableGetPendingLockRequests' ); for ( const request of lockRequests ) { const { store, path, exclusive, notifyAcquired } = request; const isAvailable = yield controls.select( - 'core', + coreStoreName, '__unstableIsLockAvailable', store, path, diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index d362e074ca71c3..4b61b8f0b63631 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -13,6 +13,7 @@ import { apiFetch } from '@wordpress/data-controls'; * Internal dependencies */ import { regularFetch } from './controls'; +import { CORE_STORE_NAME as coreStoreName } from './utils/constants'; /** * Internal dependencies @@ -85,7 +86,7 @@ export function* getEntityRecord( kind, name, key = '', query ) { } const lock = yield* __unstableAcquireStoreLock( - 'core', + coreStoreName, [ 'entities', 'data', kind, name, key ], { exclusive: false } ); @@ -122,7 +123,7 @@ export function* getEntityRecord( kind, name, key = '', query ) { // fields, so it's tested here, prior to initiating the REST request, // and without causing `getEntityRecords` resolution to occur. const hasRecords = yield controls.select( - 'core', + coreStoreName, 'hasEntityRecords', kind, name, @@ -174,7 +175,7 @@ export function* getEntityRecords( kind, name, query = {} ) { } const lock = yield* __unstableAcquireStoreLock( - 'core', + coreStoreName, [ 'entities', 'data', kind, name ], { exclusive: false } ); @@ -350,7 +351,7 @@ export function* canUser( action, resource, id ) { */ export function* getAutosaves( postType, postId ) { const { rest_base: restBase } = yield controls.resolveSelect( - 'core', + coreStoreName, 'getPostType', postType ); @@ -373,7 +374,12 @@ export function* getAutosaves( postType, postId ) { * @param {number} postId The id of the parent post. */ export function* getAutosave( postType, postId ) { - yield controls.resolveSelect( 'core', 'getAutosaves', postType, postId ); + yield controls.resolveSelect( + coreStoreName, + 'getAutosaves', + postType, + postId + ); } /** @@ -402,7 +408,7 @@ export function* __experimentalGetTemplateForLink( link ) { yield getEntityRecord( 'postType', 'wp_template', template.id ); const record = yield controls.select( - 'core', + coreStoreName, 'getEntityRecord', 'postType', 'wp_template', diff --git a/packages/core-data/src/selectors.js b/packages/core-data/src/selectors.js index 3dad25c0794dfc..a7f81931646111 100644 --- a/packages/core-data/src/selectors.js +++ b/packages/core-data/src/selectors.js @@ -17,6 +17,7 @@ import { STORE_NAME } from './name'; import { getQueriedItems } from './queried-data'; import { DEFAULT_ENTITY_KEY } from './entities'; import { getNormalizedCommaSeparable } from './utils'; +import { CORE_DATA_STORE_NAME as coreDataStoreName } from './utils/constants'; /** * Shared reference to an empty array for cases where it is important to avoid @@ -40,7 +41,7 @@ const EMPTY_ARRAY = []; */ export const isRequestingEmbedPreview = createRegistrySelector( ( select ) => ( state, url ) => { - return select( 'core/data' ).isResolving( + return select( coreDataStoreName ).isResolving( STORE_NAME, 'getEmbedPreview', [ url ] diff --git a/packages/core-data/src/utils/constants.js b/packages/core-data/src/utils/constants.js new file mode 100644 index 00000000000000..9e05d0959a228c --- /dev/null +++ b/packages/core-data/src/utils/constants.js @@ -0,0 +1,13 @@ +/** + * The identifier for the core store. + * + * @type {string} + */ +export const CORE_STORE_NAME = 'core'; + +/** + * The identifier for the core/data store. + * + * @type {string} + */ +export const CORE_DATA_STORE_NAME = 'core/data'; diff --git a/packages/core-data/src/utils/if-not-resolved.js b/packages/core-data/src/utils/if-not-resolved.js index 5b53350541be0d..fda22b930215b9 100644 --- a/packages/core-data/src/utils/if-not-resolved.js +++ b/packages/core-data/src/utils/if-not-resolved.js @@ -3,6 +3,11 @@ */ import { controls } from '@wordpress/data'; +/** + * Internal dependencies + */ +import { CORE_STORE_NAME as coreStoreName } from './constants'; + /** * Higher-order function which invokes the given resolver only if it has not * already been resolved with the arguments passed to the enhanced function. @@ -21,7 +26,7 @@ const ifNotResolved = ( resolver, selectorName ) => */ function* resolveIfNotResolved( ...args ) { const hasStartedResolution = yield controls.select( - 'core', + coreStoreName, 'hasStartedResolution', selectorName, args