Skip to content

Commit

Permalink
Refactor core-data actions and resolvers from generators to thunks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Sep 2, 2021
1 parent b029315 commit eef42e3
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 224 deletions.
1 change: 0 additions & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/blocks": "file:../blocks",
"@wordpress/data": "file:../data",
"@wordpress/data-controls": "file:../data-controls",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/element": "file:../element",
"@wordpress/html-entities": "file:../html-entities",
Expand Down
78 changes: 31 additions & 47 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { v4 as uuid } from 'uuid';
/**
* WordPress dependencies
*/
import { __unstableAwaitPromise } from '@wordpress/data-controls';
import triggerFetch from '@wordpress/api-fetch';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
Expand All @@ -17,7 +16,6 @@ import { addQueryArgs } from '@wordpress/url';
import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
import { createBatch } from './batch';
import { getDispatch } from './controls';
import { STORE_NAME } from './name';

/**
Expand Down Expand Up @@ -168,7 +166,7 @@ export const deleteEntityRecord = (
name,
recordId,
query,
{ __unstableFetch = triggerFetch } = {}
{ __unstableFetch = apiFetch } = {}
) => async ( { dispatch } ) => {
const entities = await dispatch( getKindEntities( kind ) );
const entity = find( entities, { kind, name } );
Expand Down Expand Up @@ -204,7 +202,7 @@ export const deleteEntityRecord = (
method: 'DELETE',
} );

await dispatch( removeItems( kind, name, recordId, true ) );
dispatch( removeItems( kind, name, recordId, true ) );
} catch ( _error ) {
error = _error;
}
Expand Down Expand Up @@ -242,7 +240,7 @@ export const editEntityRecord = (
recordId,
edits,
options = {}
) => async ( { select, dispatch } ) => {
) => ( { select, dispatch } ) => {
const entity = select.getEntity( kind, name );
if ( ! entity ) {
throw new Error(
Expand Down Expand Up @@ -270,7 +268,7 @@ export const editEntityRecord = (
}, {} ),
transientEdits,
};
return await dispatch( {
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...edit,
meta: {
Expand Down Expand Up @@ -347,7 +345,7 @@ export const saveEntityRecord = (
kind,
name,
record,
{ isAutosave = false, __unstableFetch = triggerFetch } = {}
{ isAutosave = false, __unstableFetch = apiFetch } = {}
) => async ( { select, dispatch } ) => {
const entities = await dispatch( getKindEntities( kind ) );
const entity = find( entities, { kind, name } );
Expand All @@ -371,7 +369,7 @@ export const saveEntityRecord = (
const evaluatedValue = value(
select.getEditedEntityRecord( kind, name, recordId )
);
await dispatch.editEntityRecord(
dispatch.editEntityRecord(
kind,
name,
recordId,
Expand All @@ -384,7 +382,7 @@ export const saveEntityRecord = (
}
}

await dispatch( {
dispatch( {
type: 'SAVE_ENTITY_RECORD_START',
kind,
name,
Expand Down Expand Up @@ -437,13 +435,11 @@ export const saveEntityRecord = (
: data.status,
}
);
const options = {
updatedRecord = await __unstableFetch( {
path: `${ path }/autosaves`,
method: 'POST',
data,
};
updatedRecord = await __unstableFetch( options );

} );
// An autosave may be processed by the server as a regular save
// when its update is requested by the author and the post had
// draft or auto-draft status.
Expand Down Expand Up @@ -487,15 +483,15 @@ export const saveEntityRecord = (
},
{}
);
await dispatch.receiveEntityRecords(
dispatch.receiveEntityRecords(
kind,
name,
newRecord,
undefined,
true
);
} else {
await dispatch.receiveAutosaves(
dispatch.receiveAutosaves(
persistedRecord.id,
updatedRecord
);
Expand All @@ -511,13 +507,12 @@ export const saveEntityRecord = (
),
};
}
const options = {
updatedRecord = await __unstableFetch( {
path,
method: recordId ? 'PUT' : 'POST',
data: edits,
};
updatedRecord = await __unstableFetch( options );
await dispatch.receiveEntityRecords(
} );
dispatch.receiveEntityRecords(
kind,
name,
updatedRecord,
Expand All @@ -540,7 +535,7 @@ export const saveEntityRecord = (

return updatedRecord;
} finally {
await dispatch.__unstableReleaseStoreLock( lock );
dispatch.__unstableReleaseStoreLock( lock );
}
};

Expand All @@ -566,52 +561,41 @@ export const saveEntityRecord = (
* @return {Promise} A promise that resolves to an array containing the return
* values of each function given in `requests`.
*/
export function* __experimentalBatch( requests ) {
export const __experimentalBatch = ( requests ) => async ( { dispatch } ) => {
const batch = createBatch();
const dispatch = yield getDispatch();
const api = {
saveEntityRecord( kind, name, record, options ) {
return batch.add( ( add ) =>
dispatch( STORE_NAME ).saveEntityRecord( kind, name, record, {
dispatch.saveEntityRecord( kind, name, record, {
...options,
__unstableFetch: add,
} )
);
},
saveEditedEntityRecord( kind, name, recordId, options ) {
return batch.add( ( add ) =>
dispatch( STORE_NAME ).saveEditedEntityRecord(
kind,
name,
recordId,
{
...options,
__unstableFetch: add,
}
)
dispatch.saveEditedEntityRecord( kind, name, recordId, {
...options,
__unstableFetch: add,
} )
);
},
deleteEntityRecord( kind, name, recordId, query, options ) {
return batch.add( ( add ) =>
dispatch( STORE_NAME ).deleteEntityRecord(
kind,
name,
recordId,
query,
{
...options,
__unstableFetch: add,
}
)
dispatch.deleteEntityRecord( kind, name, recordId, query, {
...options,
__unstableFetch: add,
} )
);
},
};
const resultPromises = requests.map( ( request ) => request( api ) );
const [ , ...results ] = yield __unstableAwaitPromise(
Promise.all( [ batch.run(), ...resultPromises ] )
);
const [ , ...results ] = await Promise.all( [
batch.run(),
...resultPromises,
] );
return results;
}
};

/**
* Action triggered to save an entity record's edits.
Expand Down
31 changes: 0 additions & 31 deletions packages/core-data/src/controls.js

This file was deleted.

26 changes: 10 additions & 16 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { addEntities } from './actions';
import { STORE_NAME } from './name';

export const DEFAULT_ENTITY_KEY = 'id';

Expand Down Expand Up @@ -167,8 +165,8 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
*
* @return {Promise} Entities promise
*/
function* loadPostTypeEntities() {
const postTypes = yield apiFetch( { path: '/wp/v2/types?context=edit' } );
async function loadPostTypeEntities() {
const postTypes = await apiFetch( { path: '/wp/v2/types?context=edit' } );
return map( postTypes, ( postType, name ) => {
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
name
Expand Down Expand Up @@ -199,8 +197,8 @@ function* loadPostTypeEntities() {
*
* @return {Promise} Entities promise
*/
function* loadTaxonomyEntities() {
const taxonomies = yield apiFetch( {
async function loadTaxonomyEntities() {
const taxonomies = await apiFetch( {
path: '/wp/v2/taxonomies?context=edit',
} );
return map( taxonomies, ( taxonomy, name ) => {
Expand Down Expand Up @@ -248,12 +246,8 @@ export const getMethodName = (
*
* @return {Array} Entities
*/
export function* getKindEntities( kind ) {
let entities = yield controls.select(
STORE_NAME,
'getEntitiesByKind',
kind
);
export const getKindEntities = ( kind ) => async ( { select, dispatch } ) => {
let entities = select.getEntitiesByKind( kind );
if ( entities && entities.length !== 0 ) {
return entities;
}
Expand All @@ -263,8 +257,8 @@ export function* getKindEntities( kind ) {
return [];
}

entities = yield kindConfig.loadEntities();
yield addEntities( entities );
entities = await kindConfig.loadEntities();
dispatch( addEntities( entities ) );

return entities;
}
};
3 changes: 0 additions & 3 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { createReduxStore, register } from '@wordpress/data';
import { controls } from '@wordpress/data-controls';

/**
* Internal dependencies
Expand All @@ -12,7 +11,6 @@ import * as selectors from './selectors';
import * as actions from './actions';
import * as resolvers from './resolvers';
import createLocksActions from './locks/actions';
import customControls from './controls';
import { defaultEntities, getMethodName } from './entities';
import { STORE_NAME } from './name';

Expand Down Expand Up @@ -58,7 +56,6 @@ const entityActions = defaultEntities.reduce( ( result, entity ) => {

const storeConfig = () => ( {
reducer,
controls: { ...customControls, ...controls },
actions: { ...actions, ...entityActions, ...createLocksActions() },
selectors: { ...selectors, ...entitySelectors },
resolvers: { ...resolvers, ...entityResolvers },
Expand Down
Loading

0 comments on commit eef42e3

Please sign in to comment.