This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(infinite-hits): support cache (#2921)
Co-authored-by: Yannick Croissant <yannick.croissant@algolia.com>
- Loading branch information
Eunjae Lee
and
Yannick Croissant
authored
Jun 8, 2020
1 parent
b542b0d
commit 7b26adc
Showing
8 changed files
with
252 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"packages": ["packages/*"], | ||
"sandboxes": [ | ||
"github/algolia/create-instantsearch-app/tree/templates/react-instantsearch" | ||
"github/algolia/create-instantsearch-app/tree/templates/react-instantsearch", | ||
"github/algolia/doc-code-samples/tree/master/React InstantSearch/routing-basic" | ||
] | ||
} |
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
94 changes: 94 additions & 0 deletions
94
packages/react-instantsearch-dom/src/lib/infiniteHitsCache/__tests__/sessionStorage.js
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,94 @@ | ||
import createInfiniteHitsSessionStorageCache from '../sessionStorage'; | ||
|
||
const KEY = 'ais.infiniteHits'; | ||
|
||
describe('createInfiniteHitsSessionStorageCache', () => { | ||
const originalSessionStorage = window.sessionStorage; | ||
delete window.sessionStorage; | ||
|
||
let store = {}; | ||
const getItem = jest.fn(key => store[key]); | ||
const setItem = jest.fn((key, value) => { | ||
store[key] = value; | ||
}); | ||
const removeItem = jest.fn(key => delete store[key]); | ||
const defaultHits = [ | ||
{ objectID: 'a', __position: 0 }, | ||
{ objectID: 'b', __position: 1 }, | ||
{ objectID: 'c', __position: 2 }, | ||
]; | ||
|
||
beforeAll(() => { | ||
Object.defineProperty(window, 'sessionStorage', { | ||
value: { | ||
getItem, | ||
setItem, | ||
removeItem, | ||
}, | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
store = {}; | ||
}); | ||
|
||
afterEach(() => { | ||
getItem.mockClear(); | ||
setItem.mockClear(); | ||
removeItem.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
window.sessionStorage = originalSessionStorage; | ||
}); | ||
|
||
it('returns null initially', () => { | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
expect(cache.read({ state: {} })).toBeNull(); | ||
}); | ||
|
||
it('returns what it was assigned before', () => { | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
const state = { query: 'hello' }; | ||
const hits = { | ||
1: defaultHits, | ||
}; | ||
cache.write({ state, hits }); | ||
expect(cache.read({ state })).toEqual(hits); | ||
}); | ||
|
||
it('returns null if the state is different', () => { | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
const state = { query: 'hello' }; | ||
const newState = { query: 'world' }; | ||
const hits = { 1: defaultHits }; | ||
cache.write({ state, hits }); | ||
expect(cache.read({ state: newState })).toBeNull(); | ||
}); | ||
|
||
it('clears cache if fails to read', () => { | ||
getItem.mockImplementation(() => '{invalid_json}'); | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
cache.read({ state: {} }); | ||
expect(removeItem).toHaveBeenCalledTimes(1); | ||
expect(removeItem).toHaveBeenCalledWith(KEY); | ||
}); | ||
|
||
it('returns null if sessionStorage.getItem() throws', () => { | ||
getItem.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
expect(cache.read({ state: {} })).toBeNull(); | ||
}); | ||
|
||
it('does nothing if sessionStorage.setItem() throws', () => { | ||
setItem.mockImplementation(() => { | ||
throw new Error(); | ||
}); | ||
const cache = createInfiniteHitsSessionStorageCache(); | ||
expect(() => { | ||
cache.write({ state: {}, hits: [] }); | ||
}).not.toThrow(); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/react-instantsearch-dom/src/lib/infiniteHitsCache/index.js
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 @@ | ||
export { | ||
default as createInfiniteHitsSessionStorageCache, | ||
} from './sessionStorage'; |
56 changes: 56 additions & 0 deletions
56
packages/react-instantsearch-dom/src/lib/infiniteHitsCache/sessionStorage.js
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,56 @@ | ||
import isEqual from 'react-fast-compare'; | ||
|
||
function getStateWithoutPage(state) { | ||
const { page, ...rest } = state || {}; | ||
return rest; | ||
} | ||
|
||
const KEY = 'ais.infiniteHits'; | ||
|
||
function hasSessionStorage() { | ||
return ( | ||
typeof window !== 'undefined' && | ||
typeof window.sessionStorage !== 'undefined' | ||
); | ||
} | ||
|
||
export default function createInfiniteHitsSessionStorageCache() { | ||
return { | ||
read({ state }) { | ||
if (!hasSessionStorage()) { | ||
return null; | ||
} | ||
try { | ||
const cache = JSON.parse(window.sessionStorage.getItem(KEY)); | ||
return cache && isEqual(cache.state, getStateWithoutPage(state)) | ||
? cache.hits | ||
: null; | ||
} catch (error) { | ||
if (error instanceof SyntaxError) { | ||
try { | ||
window.sessionStorage.removeItem(KEY); | ||
} catch (err) { | ||
// do nothing | ||
} | ||
} | ||
return null; | ||
} | ||
}, | ||
write({ state, hits }) { | ||
if (!hasSessionStorage()) { | ||
return; | ||
} | ||
try { | ||
window.sessionStorage.setItem( | ||
KEY, | ||
JSON.stringify({ | ||
state: getStateWithoutPage(state), | ||
hits, | ||
}) | ||
); | ||
} catch (error) { | ||
// do nothing | ||
} | ||
}, | ||
}; | ||
} |
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