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 Aug 31, 2021
1 parent 7c5cd02 commit 3f770ef
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 327 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
180 changes: 76 additions & 104 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { v4 as uuid } from 'uuid';
/**
* WordPress dependencies
*/
import { controls } from '@wordpress/data';
import { apiFetch, __unstableAwaitPromise } from '@wordpress/data-controls';
import triggerFetch from '@wordpress/api-fetch';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
Expand All @@ -22,7 +20,6 @@ import {
__unstableReleaseStoreLock,
} from './locks';
import { createBatch } from './batch';
import { getDispatch } from './controls';
import { STORE_NAME } from './name';

/**
Expand Down Expand Up @@ -168,33 +165,35 @@ export function receiveEmbedPreview( url, preview ) {
* call instead of `apiFetch()`.
* Must return a control descriptor.
*/
export function* deleteEntityRecord(
export const deleteEntityRecord = (
kind,
name,
recordId,
query,
{ __unstableFetch = null } = {}
) {
const entities = yield getKindEntities( kind );
{ __unstableFetch = apiFetch } = {}
) => async ( { dispatch } ) => {
const entities = await dispatch( getKindEntities( kind ) );
const entity = find( entities, { kind, name } );
let error;
let deletedRecord = false;
if ( ! entity ) {
return;
}

const lock = yield* __unstableAcquireStoreLock(
STORE_NAME,
[ 'entities', 'data', kind, name, recordId ],
{ exclusive: true }
const lock = await dispatch(
__unstableAcquireStoreLock(
STORE_NAME,
[ 'entities', 'data', kind, name, recordId ],
{ exclusive: true }
)
);
try {
yield {
dispatch( {
type: 'DELETE_ENTITY_RECORD_START',
kind,
name,
recordId,
};
} );

try {
let path = `${ entity.baseURL }/${ recordId }`;
Expand All @@ -203,36 +202,29 @@ export function* deleteEntityRecord(
path = addQueryArgs( path, query );
}

const options = {
deletedRecord = await __unstableFetch( {
path,
method: 'DELETE',
};
if ( __unstableFetch ) {
deletedRecord = yield __unstableAwaitPromise(
__unstableFetch( options )
);
} else {
deletedRecord = yield apiFetch( options );
}
} );

yield removeItems( kind, name, recordId, true );
dispatch( removeItems( kind, name, recordId, true ) );
} catch ( _error ) {
error = _error;
}

yield {
dispatch( {
type: 'DELETE_ENTITY_RECORD_FINISH',
kind,
name,
recordId,
error,
};
} );

return deletedRecord;
} finally {
yield* __unstableReleaseStoreLock( lock );
dispatch( __unstableReleaseStoreLock( lock ) );
}
}
};

/**
* Returns an action object that triggers an
Expand All @@ -247,28 +239,22 @@ export function* deleteEntityRecord(
*
* @return {Object} Action object.
*/
export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
const entity = yield controls.select( STORE_NAME, 'getEntity', kind, name );
export const editEntityRecord = (
kind,
name,
recordId,
edits,
options = {}
) => ( { select, dispatch } ) => {
const entity = select.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(
STORE_NAME,
'getRawEntityRecord',
kind,
name,
recordId
);
const editedRecord = yield controls.select(
STORE_NAME,
'getEditedEntityRecord',
kind,
name,
recordId
);
const record = select.getRawEntityRecord( kind, name, recordId );
const editedRecord = select.getEditedEntityRecord( kind, name, recordId );

const edit = {
kind,
Expand All @@ -287,7 +273,7 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
}, {} ),
transientEdits,
};
return {
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...edit,
meta: {
Expand All @@ -300,44 +286,40 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
}, {} ),
},
},
};
}
} );
};

/**
* Action triggered to undo the last edit to
* an entity record, if any.
*/
export function* undo() {
const undoEdit = yield controls.select( STORE_NAME, 'getUndoEdit' );
export const undo = () => ( { select, dispatch } ) => {
const undoEdit = select.getUndoEdit();
if ( ! undoEdit ) {
return;
}
yield {
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...undoEdit,
meta: {
isUndo: true,
},
};
}
meta: { isUndo: true },
} );
};

/**
* Action triggered to redo the last undoed
* edit to an entity record, if any.
*/
export function* redo() {
const redoEdit = yield controls.select( STORE_NAME, 'getRedoEdit' );
export const redo = () => ( { select, dispatch } ) => {
const redoEdit = select.getRedoEdit();
if ( ! redoEdit ) {
return;
}
yield {
dispatch( {
type: 'EDIT_ENTITY_RECORD',
...redoEdit,
meta: {
isRedo: true,
},
};
}
meta: { isRedo: true },
} );
};

/**
* Forces the creation of a new undo level.
Expand All @@ -364,7 +346,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 @@ -389,7 +371,7 @@ export const saveEntityRecord = (
const evaluatedValue = value(
select.getEditedEntityRecord( kind, name, recordId )
);
await dispatch.editEntityRecord(
dispatch.editEntityRecord(
kind,
name,
recordId,
Expand All @@ -402,7 +384,7 @@ export const saveEntityRecord = (
}
}

await dispatch( {
dispatch( {
type: 'SAVE_ENTITY_RECORD_START',
kind,
name,
Expand Down Expand Up @@ -437,7 +419,11 @@ export const saveEntityRecord = (
// So we fallback to the previous autosave and then
// to the actual persisted entity if the edits don't
// have a value.
let data = { ...persistedRecord, ...autosavePost, ...record };
let data = {
...persistedRecord,
...autosavePost,
...record,
};
data = Object.keys( data ).reduce(
( acc, key ) => {
if (
Expand All @@ -455,13 +441,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 @@ -505,15 +489,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 @@ -529,13 +513,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 @@ -558,7 +541,7 @@ export const saveEntityRecord = (

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

Expand All @@ -584,52 +567,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
Loading

0 comments on commit 3f770ef

Please sign in to comment.