Skip to content

Commit

Permalink
Core Data: Partially reimplement resolvers as controls
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jul 20, 2018
1 parent dc23f9c commit 4045905
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
16 changes: 16 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
*/
import { castArray } from 'lodash';

/**
* Given an apiFetch payload, returns an action object used in signalling that
* an API request should be issued. This can be used in tandem with the data
* module's `controls` option for asynchronous API request logic flows.
*
* @param {Object} options apiFetch options payload.
*
* @return {Object} Action object.
*/
export function fetchFromAPI( options ) {
return {
type: 'FETCH_FROM_API',
...options,
};
}

/**
* Returns an action object used in signalling that terms have been received
* for a given taxonomy.
Expand Down
16 changes: 16 additions & 0 deletions packages/core-data/src/controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';

/**
* Given an action object, triggers an API request to retrieve the result from
* the given action path.
*
* @param {Object} action Fetch action object.
*
* @return {Promise} Fetch promise.
*/
export function FETCH_FROM_API( action ) {
return apiFetch( { path: action.path } );
}
2 changes: 2 additions & 0 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import reducer from './reducer';
import * as selectors from './selectors';
import * as actions from './actions';
import * as resolvers from './resolvers';
import * as controls from './controls';
import { defaultEntities, getMethodName } from './entities';
import { REDUCER_KEY } from './name';

Expand All @@ -26,6 +27,7 @@ const entitySelectors = createEntityRecordGetter( selectors );
const store = registerStore( REDUCER_KEY, {
reducer,
actions,
controls,
selectors: { ...selectors, ...entitySelectors },
resolvers: { ...resolvers, ...entityResolvers },
} );
Expand Down
13 changes: 7 additions & 6 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies
*/
import {
fetchFromAPI,
receiveTerms,
receiveUserQuery,
receiveEntityRecords,
Expand All @@ -23,16 +24,16 @@ import { getKindEntities } from './entities';
* Requests categories from the REST API, yielding action objects on request
* progress.
*/
export async function* getCategories() {
const categories = await apiFetch( { path: '/wp/v2/categories?per_page=-1' } );
export function* getCategories() {
const categories = yield fetchFromAPI( { path: '/wp/v2/categories?per_page=-1' } );
yield receiveTerms( 'categories', categories );
}

/**
* Requests authors from the REST API.
*/
export async function* getAuthors() {
const users = await apiFetch( { path: '/wp/v2/users/?who=authors&per_page=-1' } );
export function* getAuthors() {
const users = yield fetchFromAPI( { path: '/wp/v2/users/?who=authors&per_page=-1' } );
yield receiveUserQuery( 'authors', users );
}

Expand Down Expand Up @@ -74,7 +75,7 @@ export async function* getEntityRecords( state, kind, name ) {
/**
* Requests theme supports data from the index.
*/
export async function* getThemeSupports() {
const index = await apiFetch( { path: '/' } );
export function* getThemeSupports() {
const index = yield fetchFromAPI( { path: '/' } );
yield receiveThemeSupportsFromIndex( index );
}
22 changes: 11 additions & 11 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies
*/
import { getCategories, getEntityRecord, getEntityRecords } from '../resolvers';
import { receiveTerms, receiveEntityRecords, addEntities } from '../actions';
import {
fetchFromAPI,
receiveTerms,
receiveEntityRecords,
addEntities,
} from '../actions';

jest.mock( '@wordpress/api-fetch' );

describe( 'getCategories', () => {
const CATEGORIES = [ { id: 1 } ];

beforeAll( () => {
apiFetch.mockImplementation( ( options ) => {
if ( options.path === '/wp/v2/categories?per_page=-1' ) {
return Promise.resolve( CATEGORIES );
}
} );
} );

it( 'yields with requested terms', async () => {
const fulfillment = getCategories();
const received = ( await fulfillment.next() ).value;
expect( received ).toEqual( receiveTerms( 'categories', CATEGORIES ) );
let yielded;
yielded = fulfillment.next().value;
expect( yielded.type ).toBe( fetchFromAPI().type );
yielded = fulfillment.next( CATEGORIES ).value;
expect( yielded ).toEqual( receiveTerms( 'categories', CATEGORIES ) );
} );
} );

Expand Down

0 comments on commit 4045905

Please sign in to comment.