Skip to content

Commit

Permalink
Data: Remove default query handling
Browse files Browse the repository at this point in the history
Simplifies reducer to avoid handling case where reducer is called on undefined initialization, not with query.
  • Loading branch information
aduth committed May 4, 2018
1 parent fda0520 commit 623bad6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 40 deletions.
2 changes: 1 addition & 1 deletion core-data/queried-data/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { withWeakMapCache } from './utils';
*
* @return {WPQueriedDataQueryParts} Query parts.
*/
export function getQueryParts( query = {} ) {
export function getQueryParts( query ) {
/**
* @type {WPQueriedDataQueryParts}
*/
Expand Down
40 changes: 13 additions & 27 deletions core-data/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,6 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
return mergedItemIds;
}

/**
* Higher-order reducer which returns undefined if the original reducer returns
* the default state value. Used in combination with `onSubKey` in avoiding
* assingment for undefined value to key.
*
* @param {Function} reducer Original reducer.
*
* @return {Function} Enhanced reducer.
*/
function withReturnUndefinedOnUnhandledDefault( reducer ) {
const defaultState = reducer( undefined, {} );
return ( state = defaultState, action ) => {
const nextState = reducer( state, action );
if ( nextState !== defaultState ) {
return nextState;
}
};
}

/**
* Reducer tracking items state, keyed by ID. Items are assumed to be normal,
* where identifiers are common across all queries.
Expand Down Expand Up @@ -112,18 +93,23 @@ const queries = flowRight( [
ifMatchingAction( ( action ) => 'query' in action ),

// Inject query parts into action for use both in `onSubKey` and reducer.
replaceAction( ( action ) => ( {
...action,
...getQueryParts( action.query ),
} ) ),
replaceAction( ( action ) => {
// `ifMatchingAction` still passes on initialization, where state is
// undefined and a query is not assigned. Avoid attempting to parse
// parts. `onSubKey` will omit by lack of `stableKey`.
if ( action.query ) {
return {
...action,
...getQueryParts( action.query ),
};
}

return action;
} ),

// Queries shape is shared, but keyed by query `stableKey` part. Original
// reducer tracks only a single query object.
onSubKey( 'stableKey' ),

// Since query is optional for handled actions, avoid tracking empty object
// for default query on initialization.
withReturnUndefinedOnUnhandledDefault,
] )( combineReducers( {
itemIds( state = null, action ) {
const { type, page, perPage } = action;
Expand Down
4 changes: 2 additions & 2 deletions core-data/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import getQueryParts from './get-query-parts';
*
* @return {?boolean} `requestingPageByPerPage` value for query.
*/
function getRequestingPageByPerPage( state, query ) {
function getRequestingPageByPerPage( state, query = {} ) {
const { stableKey, page, perPage } = getQueryParts( query );

return get( state, [
Expand All @@ -39,7 +39,7 @@ function getRequestingPageByPerPage( state, query ) {
*
* @return {?Array} Query items.
*/
export function getQueriedItems( state, query ) {
export function getQueriedItems( state, query = {} ) {
const { stableKey, page, perPage } = getQueryParts( query );
if ( ! state.queries[ stableKey ] ) {
return null;
Expand Down
10 changes: 0 additions & 10 deletions core-data/queried-data/test/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
import { getQueryParts } from '../get-query-parts';

describe( 'getQueryParts', () => {
it( 'returns default value if passed undefined', () => {
const parts = getQueryParts();

expect( parts ).toEqual( {
page: 1,
perPage: 10,
stableKey: '',
} );
} );

it( 'parses out pagination data', () => {
const parts = getQueryParts( { page: 2, perPage: 2 } );

Expand Down

0 comments on commit 623bad6

Please sign in to comment.