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

What happens with a multi-store model? #46

Open
feuersteiner opened this issue Aug 18, 2022 · 11 comments
Open

What happens with a multi-store model? #46

feuersteiner opened this issue Aug 18, 2022 · 11 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@feuersteiner
Copy link

Hi, well done for the work on Zundo, love it.
question if self-explanatory, I am using zustand with a multi-store schema, example:

  • bear store
  • honey store
    I like them separate to minimize the surface of error (rerenders, mistakes, cleaner code...).
    I looked into the demos you provided, yet, not sure how that works if I have multiple stores.
    Thanks for your help!

P.S. if not implemented yet, would love to help when time allows

@charkour charkour self-assigned this Aug 18, 2022
@charkour
Copy link
Owner

Hi @feuersteiner,

Glad you appreciate zundo!

If you have two stores, you can wrap each of them in the zundo middleware. Do you want to undo them at the same time? If so, there isn't a built-in way to do that, but you can create a function that calls undo in both stores.

If you provide a code sandbox, that will give me a better idea of the context of your question. Thanks!

@charkour charkour added the question Further information is requested label Aug 18, 2022
@feuersteiner
Copy link
Author

thanks for your response @charkour

I'll better explain it:

  • I have a design app, with multiple stores for different types of design params
  • the stores are independent
  • changes are triggered by the user, mutating one store at a time

the expected behavior is, being able to deliver do and undo to the user, i.e. :

  • undo knowing which store have been mutated last
  • keeping track of the changes
  • reverting if needed (the typical redo)

as I understand, Zundo delivers that on a "per store" basis, so my understanding is that, in my case, I am going to handle which store was mutated last, and call its undo/redo.
While I can do that, I just wanted to check with you if you plan to have this implemented (again, would love to contribute)

@charkour
Copy link
Owner

Hi @feuersteiner,

This is a great explanation of the problem you're facing. I think zundo should be able to support this in the future, but currently, it does not.

I'm nearing the completion of v2 of zundo (#43) and you can see the progress there. I'd love to discuss with you how we can best implement this feature. And I'm happy to have you contribute that to version two.

What sort of API would you like?

const useTemporalStore = trackHistory(store1, store2, store);

const { undo, redo, clear } = useTemporalStore();

@feuersteiner
Copy link
Author

I think that looks neat, there's a case where some stores addition to the "undo stack" is useless (example: settings store).

I agree with the design.

Once V2 is shipped let's talk, don't want to overwhelm you.

But yeah, we can have an issue somewhere where we can spec the feature and open it to other input from the community, what do you think ?

@charkour
Copy link
Owner

Sounds great! If you want, you can create an issue and start to spec it out whenever you want.

I think it's a fantastic feature and not one I needed when I originally started the project, but I can see others requesting it as well. Thanks!

@ohthepain
Copy link

I would also like to implement undo and multiple stores or slices.

@charkour
Copy link
Owner

Thanks, @ohthepain

If you make a PR, I'd love to review it!

@feuersteiner
Copy link
Author

hey @charkour, although I had different stores at first, I pivoted to a single store strategy concerning the main features, and considered adding more "actions" and using Immer's "produce" to ease the headache. Will be using the package as is once the site's is up. Thought you'd want to know that.

@charkour
Copy link
Owner

Thanks for letting me know!

@charkour charkour added enhancement New feature or request and removed question Further information is requested labels Apr 22, 2023
@charkour charkour added the help wanted Extra attention is needed label May 27, 2023
@charkour charkour removed their assignment May 27, 2023
@maliuta-oleksandr
Copy link

maliuta-oleksandr commented Oct 19, 2023

Is it also about Slices Pattern / BoundStore ?

@alexanderhorner
Copy link

The great thing about Zustand is that it's so flexible, so really no wrong day of doing it if it works.

I think generally Zustand recommends having one big store in favor of multiple. I split my big store into multiple slices to make it cleaner and more readable, but its still one store and changes are tracked together. Semantically i think this makes more sense, since you have one application state you want to react to and track. Having multiple stores can also introduce race conditions and zombie children and annoying stuff like that. I experienced this in my app wich manages its state in one big store, but also uses reactflow (graph library) wich also uses zustand under the hood. Just something to consider :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants