Yarikx/reductor

Subscribe to sub-state

blackvvine opened this issue · 2 comments

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?

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:

  • getState
  • subscribe

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

Thank you very much, that settles it.