Skip to content
13 changes: 13 additions & 0 deletions src/reactor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ export class BasicCache {

return new BasicCache(newCache)
}

empty() {
return new BasicCache()
}
}

const DEFAULT_LRU_LIMIT = 1000
Expand Down Expand Up @@ -224,6 +228,15 @@ export class LRUCache {
this.lru.remove(item)
)
}

empty() {
return new LRUCache(
this.limit,
this.evictCount,
this.cache.empty(),
OrderedSet()
)
}
}

/**
Expand Down
37 changes: 17 additions & 20 deletions src/reactor/fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,35 +160,32 @@ export function getOption(reactorState, option) {
return value
}



/**
* @param {ReactorState} reactorState
* @return {ReactorState}
*/
export function reset(reactorState) {
const prevState = reactorState.get('state')
const storeMap = reactorState.get('stores')

return reactorState.withMutations(reactorState => {
const storeMap = reactorState.get('stores')
const storeIds = storeMap.keySeq().toJS()
storeMap.forEach((store, id) => {
const storeState = prevState.get(id)
const resetStoreState = store.handleReset(storeState)
if (resetStoreState === undefined && getOption(reactorState, 'throwOnUndefinedStoreReturnValue')) {
throw new Error('Store handleReset() must return a value, did you forget a return statement')
}
if (getOption(reactorState, 'throwOnNonImmutableStore') && !isImmutableValue(resetStoreState)) {
throw new Error('Store reset state must be an immutable value, did you forget to call toImmutable')
}
reactorState.setIn(['state', id], resetStoreState)
})

reactorState.update('keypathStates', k => k.withMutations(keypathStates => {
storeIds.forEach(id => {
KeypathTracker.changed(keypathStates, [id])
// update state
reactorState.update('state', s => s.withMutations(state => {
storeMap.forEach((store, id) => {
const storeState = state.get(id)
const resetStoreState = store.handleReset(storeState)
if (resetStoreState === undefined && getOption(reactorState, 'throwOnUndefinedStoreReturnValue')) {
throw new Error('Store handleReset() must return a value, did you forget a return statement')
}
if (getOption(reactorState, 'throwOnNonImmutableStore') && !isImmutableValue(resetStoreState)) {
throw new Error('Store reset state must be an immutable value, did you forget to call toImmutable')
}
state.set(id, resetStoreState)
})
}))

reactorState.set('keypathStates', new KeypathTracker.RootNode())
reactorState.set('dispatchId', 1)
reactorState.update('cache', cache => cache.empty())
})
}

Expand Down
40 changes: 22 additions & 18 deletions tests/reactor-fns-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import * as fns from '../src/reactor/fns'
import * as KeypathTracker from '../src/reactor/keypath-tracker'
import { ReactorState, ObserverState } from '../src/reactor/records'
import { toImmutable } from '../src/immutable-helpers'
import { DefaultCache } from '../src/reactor/cache'

const status = KeypathTracker.status

describe('reactor fns', () => {
beforeEach(() => {
jasmine.addCustomEqualityTester(is)
})

describe('#registerStores', () => {
let reactorState
let store1
Expand Down Expand Up @@ -374,7 +379,13 @@ describe('reactor fns', () => {
let initialReactorState, nextReactorState, store1, store2

beforeEach(() => {
const reactorState = new ReactorState()
const cache = DefaultCache()
cache.miss('key', 'value')

const reactorState = new ReactorState({
cache: DefaultCache(),
})

store1 = new Store({
getInitialState() {
return toImmutable({
Expand Down Expand Up @@ -415,25 +426,18 @@ describe('reactor fns', () => {
expect(is(expected, result)).toBe(true)
})

it('should empty the cache', () => {
const cache = nextReactorState.get('cache')
expect(cache.asMap()).toEqual(Map({}))
})

it('reset the dispatchId', () => {
expect(nextReactorState.get('dispatchId')).toBe(1)
})

it('should update keypathStates', () => {
const result = nextReactorState.get('keypathStates')
const expected = new KeypathTracker.RootNode({
changedPaths: Set.of(List(['store1']), List(['store2'])),
state: 6,
status: status.DIRTY,
children: toImmutable({
store1: {
state: 3,
status: status.DIRTY,
children: {},
},
store2: {
state: 2,
status: status.DIRTY,
children: {},
},
}),
})
const expected = new KeypathTracker.RootNode()
expect(is(result, expected)).toBe(true)
})
})
Expand Down