Skip to content

Commit

Permalink
Data: Revert deprecation of async generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 4, 2018
1 parent 31b9f42 commit 5d9b7ad
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 78 deletions.
16 changes: 0 additions & 16 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
*/
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: 0 additions & 16 deletions packages/core-data/src/controls.js

This file was deleted.

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

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

Expand Down Expand Up @@ -75,7 +74,7 @@ export async function* getEntityRecords( state, kind, name ) {
/**
* Requests theme supports data from the index.
*/
export function* getThemeSupports() {
const index = yield fetchFromAPI( { path: '/' } );
export async function* getThemeSupports() {
const index = await apiFetch( { 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 {
fetchFromAPI,
receiveTerms,
receiveEntityRecords,
addEntities,
} from '../actions';
import { receiveTerms, receiveEntityRecords, addEntities } from '../actions';

jest.mock( '@wordpress/api-fetch', () => jest.fn() );

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();
let yielded;
yielded = fulfillment.next().value;
expect( yielded.type ).toBe( fetchFromAPI().type );
yielded = fulfillment.next( CATEGORIES ).value;
expect( yielded ).toEqual( receiveTerms( 'categories', CATEGORIES ) );
const received = ( await fulfillment.next() ).value;
expect( received ).toEqual( receiveTerms( 'categories', CATEGORIES ) );
} );
} );

Expand Down
38 changes: 12 additions & 26 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,34 +223,20 @@ export function createRegistry( storeConfigs = {} ) {
let fulfillment = resolver.fulfill( state, ...args );

// Attempt to normalize fulfillment as async iterable.
if ( isGenerator( fulfillment ) ) {
// Override original fulfillment to trigger resolution
// finish once deferred yielded result is completed.
const originalFulfillment = fulfillment;
fulfillment = ( function* () {
yield* originalFulfillment;
finishResolution( reducerKey, selectorName, args );
}() );
} else {
// Attempt to normalize fulfillment as async iterable.
fulfillment = toAsyncIterable( fulfillment );
if ( isAsyncIterable( fulfillment ) ) {
deprecated( 'Asynchronous iterable resolvers', {
alternative: 'synchronous generators with `controls` plugin',
plugin: 'Gutenberg',
version: '3.7',
} );

for await ( const maybeAction of fulfillment ) {
// Dispatch if it quacks like an action.
if ( isActionLike( maybeAction ) ) {
store.dispatch( maybeAction );
}
}
}

fulfillment = toAsyncIterable( fulfillment );
if ( ! isAsyncIterable( fulfillment ) ) {
finishResolution( reducerKey, selectorName, args );
return;
}

for await ( const maybeAction of fulfillment ) {
// Dispatch if it quacks like an action.
if ( isActionLike( maybeAction ) ) {
store.dispatch( maybeAction );
}
}

finishResolution( reducerKey, selectorName, args );
}

if ( typeof resolver.isFulfilled === 'function' ) {
Expand Down

0 comments on commit 5d9b7ad

Please sign in to comment.