zundo icon indicating copy to clipboard operation
zundo copied to clipboard

Will this work with native persist middleware?

Open adanielyan opened this issue 2 years ago • 1 comments

This looks great, but before adding it to my project I'd like to know if and how this middleware will work with zustand's persist middleware. Please advise!

adanielyan avatar Aug 16 '22 19:08 adanielyan

Hi @adanielyan,

Thanks for your great question. Are you asking if the past and future states are persisted, or if your store is persisted?

Thanks.

charkour avatar Aug 19 '22 04:08 charkour

v2 should work with the persist middleware.

You can test it out with npm i zundo@beta!

charkour avatar Sep 23 '22 02:09 charkour

v2 should work with the persist middleware

should, but how? 🤔 so far managed to get persist running of the main store but previous states do not get carried over

SugarF0x avatar Jan 06 '23 16:01 SugarF0x

@SugarF0x,

Thanks for the question. Right now, using persist will only persist the main store, and not the temporal store.

I haven't tried this, but does it work to wrap myStore.temporal with the persist middleware? If not, I can push an update that give the ability to use persist middleware on the temporal store.

Just let me know. Thanks.

charkour avatar Jan 06 '23 16:01 charkour

@charkour thanks for the quick reply! Unfortunately, wrapping temporal with persist does not work Would have been fantastic to have such an option to do so!

SugarF0x avatar Jan 06 '23 18:01 SugarF0x

@SugarF0x, thanks for the info.

Shoot, I was hoping it would work but it makes sense it does not. In v1 of zundo, it didn't play well with other middleware, so I fixed that, but we need to find a way to pass middleware to the temporal store.

zundo v2 creates a second zustand store for the temporal state when v1 injected the temporal state into the user's store. I've been thinking of returning to the v1 approach which should fix your issue. Do you have any input on how you'd like to see this go forward? Thanks.

charkour avatar Jan 06 '23 19:01 charkour

@charkour ive only started fiddling around with zustand & its ecosystem so not too too familiar with the way things work under the hood yet, so makes me wonder if a middleware can extend given initial state with additional fields, perhaps it would be possible to append temporal to the store root under _temporal key or something along these lines so that it would act much like an actual part of the store itself 🤔

SugarF0x avatar Jan 06 '23 19:01 SugarF0x

Awesome, I was thinking along the same lines.

Can't promise a release date for this, but I'll work on it this weekend. Thanks for the input!

charkour avatar Jan 06 '23 20:01 charkour

@charkour created a quick and extremely dirty proof of concept for in-store temporal state https://github.com/SugarF0x/zundo

SugarF0x avatar Jan 07 '23 19:01 SugarF0x

Damn, only now did i realize that instead of doing what i did i could have just modified current getVanillaZundo implementation to add other middlewares support :skull:

SugarF0x avatar Jan 07 '23 19:01 SugarF0x

That is awesome! Thanks for sharing, that is similar to what I have, but you got a lot further. You're welcome to make a PR and I can suggest code changes.

I'm not sure what you mean by getVanillaZundo, but that sounds promising.

charkour avatar Jan 07 '23 19:01 charkour

meant to say createVanillaTemporal i will fiddle around and see if that can be modified and should either of these two look promising, will get down to doing the typescript bits and updating tests

SugarF0x avatar Jan 07 '23 21:01 SugarF0x

@charkour updated the implementation to support optional persist middleware on temporal store leaving the functionality almost intact and (hopefully) backwards compatible https://github.com/charkour/zundo/pull/61

SugarF0x avatar Jan 08 '23 14:01 SugarF0x