-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data: Support async generator resolver functions
- Loading branch information
Showing
11 changed files
with
197 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { uniqBy } from 'lodash'; | ||
import { combineReducers } from 'redux'; | ||
|
||
/** | ||
* Reducer returning the categories list. | ||
* | ||
* @param {Object} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* @param {Object} state Current state. | ||
* @param {Object} action Dispatched action. | ||
* | ||
* @return {string} Updated state. | ||
*/ | ||
function categories( state = [], action ) { | ||
function categories( state = null, action ) { | ||
switch ( action.type ) { | ||
case 'RECEIVE_CATEGORIES': | ||
return uniqBy( | ||
[ | ||
...action.categories, | ||
...state, | ||
], | ||
( category ) => category.id | ||
); | ||
return [ ...action.categories ]; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export default combineReducers( { categories } ); | ||
/** | ||
* Reducer returning requested state, tracking whether requests have been | ||
* issued for a given data type. | ||
* | ||
* @param {Object} state Current state. | ||
* @param {Object} action Action object. | ||
* | ||
* @return {Object} Next state. | ||
*/ | ||
function requested( state = {}, action ) { | ||
switch ( action.type ) { | ||
case 'SET_REQUESTED': | ||
return { | ||
...state, | ||
[ action.dataType ]: true, | ||
}; | ||
} | ||
|
||
return state; | ||
} | ||
|
||
export default combineReducers( { | ||
categories, | ||
requested, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { receiveCategories } from './actions'; | ||
import { setRequested, receiveCategories } from './actions'; | ||
|
||
/** | ||
* Requests categories from the REST API, returning a promise resolving to an | ||
* action object for receiving categories. | ||
* | ||
* @return {Promise<Object>} Categories request promise. | ||
* Requests categories from the REST API, yielding action objects on request | ||
* progress. | ||
*/ | ||
export async function getCategories() { | ||
export async function* getCategories() { | ||
yield setRequested( 'categories' ); | ||
const categories = await wp.apiRequest( { path: '/wp/v2/categories' } ); | ||
return receiveCategories( categories ); | ||
yield receiveCategories( categories ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
|
||
/** | ||
* Returns all the available categories. | ||
* | ||
* @param {Object} state Global application state. | ||
* @param {Object} state Data state. | ||
* | ||
* @return {Array} Categories List | ||
* @return {Array} Categories list. | ||
*/ | ||
export function getCategories( state ) { | ||
return state.categories; | ||
} | ||
|
||
/** | ||
* Returns true if a request has been issued for the given data type, or false | ||
* otherwise. | ||
* | ||
* @param {Object} state Data state. | ||
* @param {string} dataType Data type to test. | ||
* | ||
* @return {boolean} Whether data type has been requested. | ||
*/ | ||
export function hasRequested( state, dataType ) { | ||
return !! state.requested[ dataType ]; | ||
} | ||
|
||
/** | ||
* Returns true if a request is in progress for categories data, or false | ||
* otherwise. | ||
* | ||
* @param {Object} state Data state. | ||
* | ||
* @return {boolean} Whether a request is in progress for categories. | ||
*/ | ||
export function isRequestingCategories( state ) { | ||
return ( | ||
hasRequested( state, 'categories' ) && | ||
getCategories( state ) === null | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if ( typeof Symbol.asyncIterator !== 'symbol' ) { | ||
Symbol.asyncIterator = Symbol( 'Symbol.asyncIterator' ); | ||
} |