Skip to content

Commit

Permalink
Improve selector caching for receipt data
Browse files Browse the repository at this point in the history
  • Loading branch information
katat committed Sep 12, 2019
1 parent f860963 commit 4d4427d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
14 changes: 5 additions & 9 deletions src/containers/NahmiiHoc/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ export const createReceiptsSelector = createSelectorCreator(
(previousArray, currentArray) => {
let changed = false;
currentArray.keySeq().forEach((address) => {
const previousReceipts = previousArray.getIn([address, 'receipts']);
const currentReceipts = currentArray.getIn([address, 'receipts']);
const previousReceipts = previousArray.getIn([address, 'receipts']) || fromJS([]);
const currentReceipts = currentArray.getIn([address, 'receipts']) || fromJS([]);

if (!currentReceipts && !previousReceipts) {
return;
}
const previousLastCreated = previousReceipts.getIn([previousReceipts.size - 1, 'created']);
const currentLastCreated = currentReceipts.getIn([currentReceipts.size - 1, 'created']);

if (
(!previousReceipts && currentReceipts) ||
(previousReceipts.size !== currentReceipts.size)
) {
if (previousLastCreated !== currentLastCreated) {
changed = true;
}
});
Expand Down
18 changes: 9 additions & 9 deletions src/containers/NahmiiHoc/tests/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@ describe('makeSelectReceipts', () => {
receiptsSelector(storeMock.setIn(['nahmiiHoc', 'receipts', address, 'receipts'], null))
).toEqual(receiptsLoaded.setIn([address, 'receipts'], null));
});
it('should update the cache when the current receipts state has initialised array value', () => {
expect(
receiptsSelector(storeMock.setIn(['nahmiiHoc', 'receipts', address, 'receipts'], []))
).toEqual(receiptsLoaded.setIn([address, 'receipts'], []));
});
});

describe('when previous receipts state has initialised', () => {
const store = storeMock.setIn(['nahmiiHoc', 'receipts', address, 'receipts'], fromJS([{}]));
const txs = receiptsLoaded.setIn([address, 'receipts'], fromJS([{}]));
const store = storeMock.setIn(['nahmiiHoc', 'receipts', address, 'receipts'], fromJS([{ created: '2018' }]));
const txs = receiptsLoaded.setIn([address, 'receipts'], fromJS([{ created: '2018' }]));
beforeEach(() => {
expect(
receiptsSelector(store)
Expand All @@ -98,11 +93,16 @@ describe('makeSelectReceipts', () => {
describe('should update the cache', () => {
it('when the size of receipts array is different from previous state', () => {
expect(receiptsSelector(
store.updateIn(['nahmiiHoc', 'receipts', address, 'receipts'], (arr) => arr.push({}))
)).toEqual(txs.updateIn([address, 'receipts'], (arr) => arr.push({})));
store.updateIn(['nahmiiHoc', 'receipts', address, 'receipts'], (arr) => arr.push(fromJS({ created: '2019' })))
)).toEqual(txs.updateIn([address, 'receipts'], (arr) => arr.push(fromJS({ created: '2019' }))));
});
});
describe('should not update the cache', () => {
it('when the last created property is the same', () => {
expect(receiptsSelector(
store.updateIn(['nahmiiHoc', 'receipts', address, 'receipts'], (arr) => arr.push(fromJS({ created: '2018' })))
)).toEqual(txs);
});
it('even when the loading state is changed', () => {
expect(txs.getIn([address, 'loading'])).toEqual(false);
expect(receiptsSelector(
Expand Down

0 comments on commit 4d4427d

Please sign in to comment.