Skip to content
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

is it possible to combineReducers? (combine stores in restate terminology) #1

Open
niron1 opened this issue Dec 4, 2022 · 7 comments

Comments

@niron1
Copy link

niron1 commented Dec 4, 2022

No description provided.

@endenwer
Copy link
Owner

endenwer commented Dec 5, 2022

The idea is to have only one store. Technically it is possible to combine data from multiple stores, but I would't do so.

const sub1 = store1.regRootSub(
  'sub1',
  ($root) => $root.data1
)

const sub2 = store2.regRootSub(
  'sub2',
  ($root) => $root.data2
)

const sub3 = store1.regSub(
  'sub3',
  () => [sub1(), sub2()]
  ($sub1, $sub2) => $sub1 + $sub2
)

@niron1
Copy link
Author

niron1 commented Dec 5, 2022

so is it not possible to create hierarchy as in redux and immutableJS? or, do all entries has to be on the root level ? 🤔
as per an example, with immutableJS, you can do getIn(['level1', 'level2', 'level3']) to access local component's data
anyway, I also tried to run your example here, it doesn't seem to work.
if I understand correctly, you are suggesting to combine selectors, not stores.

@endenwer
Copy link
Owner

endenwer commented Dec 5, 2022

Yes, you need to combine selectors(subs here). If you want, you can read more about this approach here https://day8.github.io/re-frame/subscriptions/
And the demo is running here https://svelte-restate-example.netlify.app/ You can look in Redux dev tools how it all works.

@niron1
Copy link
Author

niron1 commented Dec 5, 2022

thanks, I am already using your library as is :)
I am currently bypassing the problem by creating objects hierarchy like so:

export const getIbcTechnician = appDB.regRootSub( 'ibcTechnician', ($state) => $state[ORDER_FORM_STATE].ibcTechnician )

export const getIsErrorMessageDisplayed = appDB.regRootSub( 'isErrorMessageDisplayed', ($state) => $state[ERROR_STATE].isErrorMessageDisplayed )
(each constant ending with _STATE) represents a different category)
I am not sure how efficient is data access now, but it's currently more important to me to have the data arranged by categories and not flat.
I might come to a point to suggest a PR to combine multiple stores into one tree like CombineReducers if that's of interest.
but for the most part Restate resolved my main pain - lack of immutability and redux-dev-tools integration. thanks

@endenwer
Copy link
Owner

endenwer commented Dec 6, 2022

I would do it like this

export const orderForm = appDB.regRootSub(
  'orderForm',
  ($root) => $state[ORDER_FORM_STATE]
)

export const getIbcTechnician = appDB.regSub(
  'ibcTechnician',
  () => orderForm(),
  ($orderForm) => orderForm.ibcTechnician
)

export const errorState = appDB.regRootSub(
  'errorState',
  ($root) => $state[ERROR_STATE]
)

export const getIsErrorMessageDisplayed = appDB.regSub(
  'isErrorMessageDisplayed',
  () => errorState(),
  ($errorState) => errorState.isErrorMessageDisplayed
)

I don't plan to add new functionality. It is already working I as planned, but thanks for the suggestion.

@niron1
Copy link
Author

niron1 commented Dec 26, 2022

not sure how this line could word:

export const orderForm = appDB.regRootSub(
  'orderForm',
  ($root) => $state[ORDER_FORM_STATE]
)

when the function input is $root and the usage is $state ?

@endenwer
Copy link
Owner

yes, that is a mistake, it should be like this.

($root) => $root[ORDER_FORM_STATE]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants