From e58ddf854542d9622dfae92c229e8f73032460f7 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sat, 16 Dec 2017 21:36:26 +0000 Subject: [PATCH] Implement shallow compare for the journal reducer. The reason why we need it is that we don't have a concept of etag for journal fetching, which means we were setting the store on each fetch causing everything that depends on the journals to invalidate. This triggers a bug that causes the entries not to refresh if the journals haven't changed. --- src/store/reducers.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/store/reducers.ts b/src/store/reducers.ts index d277ea7..b6574de 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -39,6 +39,7 @@ export type JournalsData = List; const JournalsFetchRecord = fetchTypeRecord(); export type JournalsType = FetchType; +export type JournalsTypeImmutable = Record; export type EntriesData = List; @@ -118,7 +119,30 @@ export const entries = handleAction( const journals = handleAction( actions.fetchJournals, - fetchTypeIdentityReducer, + (state: JournalsTypeImmutable, action: any) => { + const newState = fetchTypeIdentityReducer(state, action); + // Compare the states and see if they are really different + const oldJournals = state.get('value', null); + const newJournals = newState.get('value', null); + + if (!oldJournals || !newJournals || (oldJournals.size !== newJournals.size)) { + return newState; + } + + let oldJournalHash = {}; + oldJournals.forEach((x) => { + oldJournalHash[x.uid] = x.serialize(); + }); + + if (newJournals.every((journal: EteSync.Journal) => ( + (journal.uid in oldJournalHash) && + (journal.serialize().content === oldJournalHash[journal.uid].content) + ))) { + return state; + } else { + return newState; + } + }, new JournalsFetchRecord(), );