Skip to content

Commit

Permalink
Data: Avoid tracking itemIds while request in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 4, 2018
1 parent 1e599cb commit fda0520
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 3 additions & 3 deletions core-data/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import getQueryParts from './get-query-parts';
*
* @return {number[]} Merged array of item IDs.
*/
export function getMergedItemIds( itemIds = [], nextItemIds, page, perPage ) {
export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
const nextItemIdsStartIndex = ( page - 1 ) * perPage;

// If later page has already been received, default to the larger known
Expand Down Expand Up @@ -125,15 +125,15 @@ const queries = flowRight( [
// for default query on initialization.
withReturnUndefinedOnUnhandledDefault,
] )( combineReducers( {
itemIds( state = [], action ) {
itemIds( state = null, action ) {
const { type, page, perPage } = action;

if ( type !== RECEIVE_ITEMS ) {
return state;
}

return getMergedItemIds(
state,
state || [],
map( action.items, 'id' ),
page,
perPage
Expand Down
4 changes: 4 additions & 0 deletions core-data/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function getQueriedItems( state, query ) {
}

const itemIds = state.queries[ stableKey ].itemIds;
if ( ! itemIds ) {
return null;
}

const startOffset = ( page - 1 ) * perPage;
const endOffset = startOffset + perPage;

Expand Down
4 changes: 2 additions & 2 deletions core-data/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import reducer, {

describe( 'getMergedItemIds', () => {
it( 'should receive a page', () => {
const result = getMergedItemIds( undefined, [ 4, 5, 6 ], 2, 3 );
const result = getMergedItemIds( [], [ 4, 5, 6 ], 2, 3 );

expect( result ).toEqual( [
undefined,
Expand Down Expand Up @@ -164,7 +164,7 @@ describe( 'reducer', () => {
items: {},
queries: {
'': {
itemIds: [],
itemIds: null,
requestingPageByPerPage: {
10: {
1: true,
Expand Down
26 changes: 26 additions & 0 deletions core-data/queried-data/test/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import { getQueriedItems } from '../selectors';

describe( 'getQueriedItems', () => {
it( 'should return null if requesting but no item IDs', () => {
const state = {
items: {},
queries: {
'': {
itemIds: null,
requestingPageByPerPage: {
10: {
1: true,
},
},
},
},
};

const result = getQueriedItems( state );

expect( result ).toBe( null );
} );
} );

0 comments on commit fda0520

Please sign in to comment.