Skip to content

Commit

Permalink
Improve result rendering for small result sets
Browse files Browse the repository at this point in the history
Ensure that the result pausing timeout is always reset while there are still not yet enough results to render a full set.
  • Loading branch information
lennym committed Jul 21, 2016
1 parent 2b8c22a commit 3ce8512
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 7 additions & 12 deletions src/actions/search-results.js
Expand Up @@ -57,8 +57,11 @@ export function receiveRelatedResult (items) {

export function setSearchComplete (result = 'timeout') {
return (dispatch, getState) => {
const { search: { resultId } } = getState();
const { search: { resultId, displayedItems } } = getState();
if (result === 'timeout' || result.graphql.searchId === resultId) { // check result corresponds to the current search
if (displayedItems.length === 0) {
dispatch(loadMoreItemsIntoFeed());
}
return dispatch({ type: SEARCH_COMPLETE });
}
};
Expand Down Expand Up @@ -161,8 +164,6 @@ export function startSearch (a) {
};
}

let renderTimer, fallbackTimer;

export function saveSearchResult (result) {
return (dispatch, getState) => {
const { search: { resultId } } = getState();
Expand All @@ -178,22 +179,16 @@ export function saveSearchResult (result) {
};
}

let renderTimer;
export function loadInitialData () {
return (dispatch, getState) => {
const { search: { displayedItems, items, initialPageSize } } = getState();
// if there is enough data to render the first page of results, do so immediately and clear any timers
if (displayedItems.length < initialPageSize && items.length > initialPageSize) {
if (renderTimer) timers.clearTimeout(renderTimer);
if (fallbackTimer) timers.clearTimeout(fallbackTimer);
dispatch(loadMoreItemsIntoFeed());
// wait up to 3s for results before rendering anyway
} else if (displayedItems.length === 0) {
if (renderTimer) timers.clearTimeout(renderTimer);
fallbackTimer = timers.setTimeout(() => {
dispatch(loadMoreItemsIntoFeed());
}, 3000);
// otherwise if no results are received for 1s then push what we have
} else {
} else if (displayedItems.length < initialPageSize) {
// otherwise if no results are received for 1s then push what we have
if (renderTimer) timers.clearTimeout(renderTimer);
renderTimer = timers.setTimeout(() => {
dispatch(loadMoreItemsIntoFeed());
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/search.js
Expand Up @@ -21,7 +21,7 @@ import {
UPDATE_DISPLAYED_ITEMS,
RECEIVE_RELATED_RESULT,
SEARCH_COMPLETE,
UPDATE_TILE_RANKING,
UPDATE_TILE_RANKING
} from '../constants/actionTypes';

import DEFAULT_TAG from '../constants/default-tag.js';
Expand Down

0 comments on commit 3ce8512

Please sign in to comment.