Yarikx / reductor

Redux for Android. Predictable state container library for Java/Android
Apache License 2.0
463 stars 27 forks source link

Subscribe to sub-state #24

Closed blackvvine closed 7 years ago

blackvvine commented 7 years ago

Hi,

Thanks for the great work. I was wondering since the whole concept is built upon "single source of truth", we're going to ideally have the entire app state inside a single store.

Hence, in all parts of the app, we'll be subscribing to updates on the entire state tree. Isn't that a little inefficient? Shouldn't we be able to only subscribe to a single part of the combined state?

Yarikx commented 7 years ago

Hi @blackvvine,

Indeed it would be inefficient and will create leaky abstraction, as you will need to know the "path" of a particular sub-strate in every component that interacts with the global state.

However, in Reductor you can create Cursors. With Cursor you can "focus" on some particular sub-state, without knowing where it's actually coming from.

Cursor is the reader part of your state. There are only two methods there:

The Store<T> itself implements Cursor<T>.

Cursor can be obtained by calling Cursors.map(cursor, mappingFunction)

So map your global state to some sub-state and expose it to your component.

Check documentation or release notes for more information https://github.com/Yarikx/reductor/releases/tag/v0.12.0

blackvvine commented 7 years ago

Thank you very much, that settles it.