Skip to content

Commit 4fb6b8c

Browse files
committed
Fix bug where cache didn't update dirty entries
1 parent 2eefde8 commit 4fb6b8c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/reactor/fns.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ export function evaluate(reactorState, keyPathOrGetter) {
334334

335335
const cache = reactorState.get('cache')
336336
var cacheEntry = cache.lookup(keyPathOrGetter)
337-
const isCacheMiss = !cacheEntry
338-
if (isCacheMiss || isDirtyCacheEntry(reactorState, cacheEntry)) {
337+
const isCacheMiss = !cacheEntry || isDirtyCacheEntry(reactorState, cacheEntry)
338+
if (isCacheMiss) {
339339
cacheEntry = createCacheEntry(reactorState, keyPathOrGetter)
340340
}
341341

tests/reactor-tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,24 @@ describe('Reactor', () => {
595595
expect(taxPercentSpy.calls.count()).toEqual(2)
596596
expect(subtotalSpy.calls.count()).toEqual(1)
597597
})
598+
599+
it('should update cache with updated item after action', () => {
600+
const lastItemGetter = [['items', 'all'], (items) => items.last()]
601+
602+
// ensure its in cache
603+
const lastItemBefore = reactor.evaluate(lastItemGetter)
604+
const cacheEntryBefore = reactor.reactorState.cache.lookup(lastItemGetter)
605+
expect(lastItemBefore === cacheEntryBefore.value).toBe(true)
606+
607+
checkoutActions.addItem('potato', 0.80)
608+
609+
const lastItemAfter = reactor.evaluate(lastItemGetter)
610+
const cacheEntryAfter = reactor.reactorState.cache.lookup(lastItemGetter)
611+
expect(lastItemAfter === cacheEntryAfter.value).toBe(true)
612+
613+
// sanity check that lastItem actually changed for completeness
614+
expect(lastItemAfter !== lastItemBefore).toBe(true)
615+
})
598616
})
599617

600618
describe('#observe', () => {

0 commit comments

Comments
 (0)