Skip to content

Commit

Permalink
Fix bug where cache didn't update dirty entries
Browse files Browse the repository at this point in the history
  • Loading branch information
loganlinn committed Feb 2, 2016
1 parent 2eefde8 commit 4fb6b8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/reactor/fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ export function evaluate(reactorState, keyPathOrGetter) {

const cache = reactorState.get('cache')
var cacheEntry = cache.lookup(keyPathOrGetter)
const isCacheMiss = !cacheEntry
if (isCacheMiss || isDirtyCacheEntry(reactorState, cacheEntry)) {
const isCacheMiss = !cacheEntry || isDirtyCacheEntry(reactorState, cacheEntry)
if (isCacheMiss) {
cacheEntry = createCacheEntry(reactorState, keyPathOrGetter)
}

Expand Down
18 changes: 18 additions & 0 deletions tests/reactor-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,24 @@ describe('Reactor', () => {
expect(taxPercentSpy.calls.count()).toEqual(2)
expect(subtotalSpy.calls.count()).toEqual(1)
})

it('should update cache with updated item after action', () => {
const lastItemGetter = [['items', 'all'], (items) => items.last()]

// ensure its in cache
const lastItemBefore = reactor.evaluate(lastItemGetter)
const cacheEntryBefore = reactor.reactorState.cache.lookup(lastItemGetter)
expect(lastItemBefore === cacheEntryBefore.value).toBe(true)

checkoutActions.addItem('potato', 0.80)

const lastItemAfter = reactor.evaluate(lastItemGetter)
const cacheEntryAfter = reactor.reactorState.cache.lookup(lastItemGetter)
expect(lastItemAfter === cacheEntryAfter.value).toBe(true)

// sanity check that lastItem actually changed for completeness
expect(lastItemAfter !== lastItemBefore).toBe(true)
})
})

describe('#observe', () => {
Expand Down

0 comments on commit 4fb6b8c

Please sign in to comment.