Skip to content
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

core-data: no-string-literals fix #32198

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -182,7 +183,7 @@ export function* deleteEntityRecord(
}

const lock = yield* __unstableAcquireStoreLock(
'core',
coreStoreName,
[ 'entities', 'data', kind, name, recordId ],
{ exclusive: true }
);
Expand Down Expand Up @@ -246,22 +247,27 @@ 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.`
);
}
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,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 }
);
Expand All @@ -385,7 +391,7 @@ export function* saveEntityRecord(
if ( typeof value === 'function' ) {
const evaluatedValue = value(
yield controls.select(
'core',
coreStoreName,
'getEditedEntityRecord',
kind,
name,
Expand Down Expand Up @@ -419,7 +425,7 @@ export function* saveEntityRecord(
recordId ? '/' + recordId : ''
}`;
const persistedRecord = yield controls.select(
'core',
coreStoreName,
'getRawEntityRecord',
kind,
name,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -660,7 +671,7 @@ export function* __experimentalBatch( requests ) {
export function* saveEditedEntityRecord( kind, name, recordId, options ) {
if (
! ( yield controls.select(
'core',
coreStoreName,
'hasEditsForEntityRecord',
kind,
name,
Expand All @@ -670,7 +681,7 @@ export function* saveEditedEntityRecord( kind, name, recordId, options ) {
return;
}
const edits = yield controls.select(
'core',
coreStoreName,
'getEntityRecordNonTransientEdits',
kind,
name,
Expand Down Expand Up @@ -698,7 +709,7 @@ export function* __experimentalSaveSpecifiedEntityEdits(
) {
if (
! ( yield controls.select(
'core',
coreStoreName,
'hasEditsForEntityRecord',
kind,
name,
Expand All @@ -708,7 +719,7 @@ export function* __experimentalSaveSpecifiedEntityEdits(
return;
}
const edits = yield controls.select(
'core',
coreStoreName,
'getEntityRecordNonTransientEdits',
kind,
name,
Expand Down
7 changes: 6 additions & 1 deletion packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 11 additions & 4 deletions packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

/**
Expand Down Expand Up @@ -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
Expand All @@ -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, {
Expand Down Expand Up @@ -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,
Expand All @@ -149,7 +156,7 @@ export function useEntityBlockEditor( kind, type, { id: _id } = {} ) {
[ kind, type, id ]
);
const { __unstableCreateUndoLevel, editEntityRecord } = useDispatch(
'core'
coreStoreName
);

useEffect( () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/core-data/src/locks/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,7 +86,7 @@ export function* getEntityRecord( kind, name, key = '', query ) {
}

const lock = yield* __unstableAcquireStoreLock(
'core',
coreStoreName,
[ 'entities', 'data', kind, name, key ],
{ exclusive: false }
);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -174,7 +175,7 @@ export function* getEntityRecords( kind, name, query = {} ) {
}

const lock = yield* __unstableAcquireStoreLock(
'core',
coreStoreName,
[ 'entities', 'data', kind, name ],
{ exclusive: false }
);
Expand Down Expand Up @@ -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
);
Expand All @@ -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
);
}

/**
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ]
Expand Down
13 changes: 13 additions & 0 deletions packages/core-data/src/utils/constants.js
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 6 additions & 1 deletion packages/core-data/src/utils/if-not-resolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +26,7 @@ const ifNotResolved = ( resolver, selectorName ) =>
*/
function* resolveIfNotResolved( ...args ) {
const hasStartedResolution = yield controls.select(
'core',
coreStoreName,
'hasStartedResolution',
selectorName,
args
Expand Down