-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grouping observers by getters in addObserver #183
base: jordan/refactor-everything!
Are you sure you want to change the base?
Changes from 6 commits
424db11
c1dc8c4
f77202c
d38b04e
fe88c19
6beea89
b75d528
eb162a1
22bedfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,18 +171,26 @@ exports.addObserver = function(observerState, getter, handler) { | |
handler: handler, | ||
}) | ||
|
||
let updatedObserverState | ||
let updatedObserverState = observerState.updateIn(['gettersMap', getter] | ||
, observerIds => | ||
observerIds | ||
? observerIds.add(currId) | ||
: Immutable.Set([]).add(currId) | ||
) | ||
|
||
if (storeDeps.size === 0) { | ||
// no storeDeps means the observer is dependent on any of the state changing | ||
updatedObserverState = observerState.update('any', observerIds => observerIds.add(currId)) | ||
|
||
updatedObserverState = updatedObserverState.updateIn(['any'], getters => getters.add(getter)) | ||
} else { | ||
updatedObserverState = observerState.withMutations(map => { | ||
updatedObserverState = updatedObserverState.withMutations(map => { | ||
storeDeps.forEach(storeId => { | ||
let path = ['stores', storeId] | ||
if (!map.hasIn(path)) { | ||
map.setIn(path, Immutable.Set([])) | ||
} | ||
map.updateIn(['stores', storeId], observerIds => observerIds.add(currId)) | ||
map.updateIn(['stores', storeId] | ||
, getters => | ||
getters | ||
? getters.add(getter) | ||
: Immutable.Set([]).add(getter) | ||
) | ||
}) | ||
}) | ||
} | ||
|
@@ -195,6 +203,7 @@ exports.addObserver = function(observerState, getter, handler) { | |
observerState: updatedObserverState, | ||
entry: entry, | ||
} | ||
|
||
} | ||
|
||
/** | ||
|
@@ -240,23 +249,27 @@ exports.removeObserver = function(observerState, getter, handler) { | |
exports.removeObserverByEntry = function(observerState, entry) { | ||
return observerState.withMutations(map => { | ||
const id = entry.get('id') | ||
const getter = entry.get('getter') | ||
const storeDeps = entry.get('storeDeps') | ||
|
||
if (storeDeps.size === 0) { | ||
map.update('any', anyObsevers => anyObsevers.remove(id)) | ||
} else { | ||
storeDeps.forEach(storeId => { | ||
map.updateIn(['stores', storeId], observers => { | ||
if (observers) { | ||
// check for observers being present because reactor.reset() can be called before an unwatch fn | ||
return observers.remove(id) | ||
} | ||
return observers | ||
map.updateIn(['gettersMap', getter], observerIds => observerIds.remove(id)); | ||
|
||
if (map.getIn(['gettersMap', getter]).size <= 0) { | ||
|
||
if (storeDeps.size === 0) { | ||
// no storeDeps means the observer is dependent on any of the state changing | ||
map.update('any', getters => getters.remove(getter)); | ||
} else { | ||
storeDeps.forEach(storeId => { | ||
map.updateIn(['stores', storeId] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think this is correct. If we observe add two observer entries with the same getter and then unobserve one it will unobserve all entries with that getter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's why it's under the condition, only removed when there's no entries listening to that getter
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add a test case to cover it if you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! This looks good. |
||
, getters => getters.remove(getter) ) | ||
}) | ||
}) | ||
} | ||
|
||
} | ||
|
||
map.removeIn(['observersMap', id]) | ||
|
||
}) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use the style: