Skip to content

Commit f712e7d

Browse files
committedNov 3, 2015
Update to fix edge cases of unwatch fn
1 parent f979b20 commit f712e7d

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed
 

‎src/reactor.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,9 @@ class Reactor {
201201

202202
dirtyStores.forEach(id => {
203203
const entries = this.observerState.getIn(['stores', id])
204-
if (!entries) {
205-
return
204+
if (entries) {
205+
set.union(entries)
206206
}
207-
set.union(entries)
208207
})
209208
})
210209

@@ -219,11 +218,14 @@ class Reactor {
219218
const currValue = currEvaluateResult.result
220219

221220
if (!Immutable.is(prevValue, currValue)) {
222-
const handlers = this.observerState.getIn(['gettersMap', getter])
223-
.map(observerId => this.observerState.getIn(['observersMap', observerId, 'handler']))
224-
// don't notify here in the case a handler called unobserve on another observer
225-
226-
handlers.forEach(handler => handler.call(null, currValue))
221+
const observerIds = this.observerState.getIn(['gettersMap', getter], [])
222+
observerIds.forEach(observerId => {
223+
const handler = this.observerState.getIn(['observersMap', observerId, 'handler'])
224+
// don't notify here in the case a handler called unobserve on another observer
225+
if (handler) {
226+
handler.call(null, currValue)
227+
}
228+
})
227229
}
228230
})
229231

‎src/reactor/fns.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ exports.addObserver = function(observerState, getter, handler) {
177177

178178
if (storeDeps.size === 0) {
179179
// no storeDeps means the observer is dependent on any of the state changing
180-
181180
updatedObserverState = updatedObserverState.updateIn(['any'], getters => getters.add(getter))
182181
} else {
183182
updatedObserverState = updatedObserverState.withMutations(map => {
@@ -246,24 +245,34 @@ exports.removeObserverByEntry = function(observerState, entry) {
246245
const getter = entry.get('getter')
247246
const storeDeps = entry.get('storeDeps')
248247

249-
map.updateIn(['gettersMap', getter], observerIds => observerIds.remove(id));
250-
251-
if (map.getIn(['gettersMap', getter]).size <= 0) {
248+
const observerIds = map.getIn(['gettersMap', getter])
249+
if (!observerIds) {
250+
// getter doesn't exist if reactor.reset() is called before the unwatchFn()
251+
return
252+
}
253+
const updatedObserverIds = observerIds.remove(id)
254+
map.setIn(['gettersMap', getter], updatedObserverIds)
252255

256+
if (updatedObserverIds.size === 0) {
257+
// all observers have been removed for this getter, remove other entries
253258
if (storeDeps.size === 0) {
254259
// no storeDeps means the observer is dependent on any of the state changing
255260
map.update('any', getters => getters.remove(getter));
256261
} else {
257262
storeDeps.forEach(storeId => {
258-
map.updateIn(['stores', storeId]
259-
, getters => getters.remove(getter) )
263+
map.updateIn(['stores', storeId], getters => {
264+
if (getters) {
265+
// check to make sure the getters Set exists for this store,
266+
// in the case of reactor.reset() is called before the unwatchFn()
267+
return getters.remove(getter)
268+
}
269+
return getters
270+
})
260271
})
261272
}
262-
263273
}
264274

265275
map.removeIn(['observersMap', id])
266-
267276
})
268277
}
269278

0 commit comments

Comments
 (0)