Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit a9a29c7

Browse files
Eunjae LeeHaroenv
andauthored
fix(infiniteHits): fix stale hits issue (#3021)
* fix(infiniteHits): fix stale hits issue * Revert "fix(infiniteHits): fix stale hits issue" This reverts commit 1c429c6. * fix(infiniteHits): fix stale hits issue * return an empty array when there is no results * clean up the flow * Apply suggestions from code review Co-authored-by: Haroen Viaene <hello@haroen.me> Co-authored-by: Haroen Viaene <hello@haroen.me>
1 parent ff1e0fe commit a9a29c7

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
lines changed

packages/react-instantsearch-core/src/connectors/__tests__/connectInfiniteHits.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,14 @@ describe('connectInfiniteHits', () => {
318318
'theQueryID_1',
319319
'theQueryID_1',
320320
// page 2
321-
'theQueryID_2',
322-
'theQueryID_2',
323-
'theQueryID_2',
324-
'theQueryID_2',
325-
'theQueryID_2',
326-
'theQueryID_2',
327-
'theQueryID_2',
328-
'theQueryID_2',
321+
'theQueryID_2_',
322+
'theQueryID_2_',
323+
'theQueryID_2_',
324+
'theQueryID_2_',
325+
'theQueryID_2_',
326+
'theQueryID_2_',
327+
'theQueryID_2_',
328+
'theQueryID_2_',
329329
]);
330330
expect(res3.hasMore).toBe(true);
331331
});
@@ -498,17 +498,6 @@ describe('connectInfiniteHits', () => {
498498
expect(instance.refine).toHaveBeenLastCalledWith(event, 1);
499499
});
500500

501-
it('adds 1 to page when calling refine', () => {
502-
const props = { contextValue };
503-
const state0 = {};
504-
505-
const state1 = connect.refine.call({}, props, state0);
506-
expect(state1).toEqual({ page: 2 });
507-
508-
const state2 = connect.refine.call({}, props, state1);
509-
expect(state2).toEqual({ page: 3 });
510-
});
511-
512501
it('set page to the corresponding index', () => {
513502
const props = { contextValue };
514503
const state0 = {};
@@ -520,14 +509,6 @@ describe('connectInfiniteHits', () => {
520509
expect(state1).toEqual({ page: 6 });
521510
});
522511

523-
it('automatically converts String state to Number', () => {
524-
const props = { contextValue };
525-
const state0 = { page: '0' };
526-
527-
const state1 = connect.refine.call({}, props, state0);
528-
expect(state1).toEqual({ page: 1 });
529-
});
530-
531512
it('expect to always return an array of hits', () => {
532513
const props = { contextValue };
533514
const searchState = {};
@@ -561,7 +542,7 @@ describe('connectInfiniteHits', () => {
561542
expect(actual).toEqual(expectation);
562543
});
563544

564-
it('read from given cache', () => {
545+
it('does not read from given cache when results is empty', () => {
565546
const cache = {
566547
read: jest.fn(),
567548
write: jest.fn(),
@@ -570,6 +551,25 @@ describe('connectInfiniteHits', () => {
570551
const searchState = {};
571552
const searchResults = {};
572553
connect.getProvidedProps.call({}, props, searchState, searchResults);
554+
expect(cache.read).toHaveBeenCalledTimes(0);
555+
});
556+
557+
it('read from given cache', () => {
558+
const cache = {
559+
read: jest.fn(),
560+
write: jest.fn(),
561+
};
562+
const props = { cache, contextValue };
563+
const searchState = {};
564+
const searchResults = {
565+
results: {
566+
hits: [{}, {}, {}],
567+
hitsPerPage: 3,
568+
page: 1,
569+
nbPages: 3,
570+
},
571+
};
572+
connect.getProvidedProps.call({}, props, searchState, searchResults);
573573
expect(cache.read).toHaveBeenCalledTimes(1);
574574
});
575575

@@ -579,7 +579,14 @@ describe('connectInfiniteHits', () => {
579579
write: jest.fn(),
580580
};
581581
const searchState = {};
582-
const searchResults = {};
582+
const searchResults = {
583+
results: {
584+
hits: [{}, {}, {}],
585+
hitsPerPage: 3,
586+
page: 1,
587+
nbPages: 3,
588+
},
589+
};
583590
const instance = {};
584591
connect.getProvidedProps.call(
585592
instance,

packages/react-instantsearch-core/src/connectors/connectInfiniteHits.js

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,9 @@ export default createConnector({
7878
multiIndexContext: props.indexContextValue,
7979
});
8080

81-
this._prevState = this._prevState || {};
82-
83-
this._cache = props.cache ? props.cache : this._cache || getInMemoryCache();
84-
let cachedHits = this._cache.read({ state: searchState }) || {};
85-
8681
if (!results) {
8782
return {
88-
hits: extractHitsFromCachedHits(cachedHits),
83+
hits: [],
8984
hasPrevious: false,
9085
hasMore: false,
9186
refine: () => {},
@@ -94,29 +89,20 @@ export default createConnector({
9489
};
9590
}
9691

97-
const {
98-
page,
99-
hits,
100-
hitsPerPage,
101-
nbPages,
102-
_state: { page: p, ...currentState } = {},
103-
} = results;
92+
const { page, hits, hitsPerPage, nbPages, _state: state } = results;
93+
94+
this._cache = props.cache ? props.cache : this._cache || getInMemoryCache();
95+
const cachedHits = this._cache.read({ state }) || {};
10496

10597
const hitsWithPositions = addAbsolutePositions(hits, hitsPerPage, page);
10698
const hitsWithPositionsAndQueryID = addQueryID(
10799
hitsWithPositions,
108100
results.queryID
109101
);
110102

111-
if (!isEqual(currentState, this._prevState)) {
112-
cachedHits = this._cache.read({ state: searchState }) || {};
113-
}
114-
if (cachedHits[page] === undefined) {
115-
cachedHits[page] = hitsWithPositionsAndQueryID;
116-
this._cache.write({ state: searchState, hits: cachedHits });
117-
}
103+
cachedHits[page] = hitsWithPositionsAndQueryID;
104+
this._cache.write({ state, hits: cachedHits });
118105

119-
this._prevState = currentState;
120106
/*
121107
Math.min() and Math.max() returns Infinity or -Infinity when no argument is given.
122108
But there is always something in this point because of `cachedHits[page]`.
@@ -150,22 +136,6 @@ export default createConnector({
150136
},
151137

152138
refine(props, searchState, event, index) {
153-
this._cache = props.cache ? props.cache : this._cache || getInMemoryCache();
154-
const cachedHits = this._cache.read({ state: searchState }) || {};
155-
const pages = Object.keys(cachedHits).map(Number);
156-
const lastReceivedPage =
157-
pages.length === 0 ? undefined : Math.max(...pages);
158-
// If there is no key in `this._cachedHits`,
159-
// then `lastReceivedPage` should be `undefined`.
160-
if (index === undefined && lastReceivedPage !== undefined) {
161-
index = lastReceivedPage + 1;
162-
} else if (index === undefined) {
163-
index = getCurrentRefinement(props, searchState, {
164-
ais: props.contextValue,
165-
multiIndexContext: props.indexContextValue,
166-
});
167-
}
168-
169139
const id = getId();
170140
const nextValue = { [id]: index + 1 };
171141
const resetPage = false;

0 commit comments

Comments
 (0)