zundo icon indicating copy to clipboard operation
zundo copied to clipboard

[Help] How do I made `zundo` work with `subscribeWithSelector` in typescript?

Open tresabhi opened this issue 3 years ago • 6 comments

How do I made zundo work with subscribeWithSelector in typescript?

Wrapping subscribeWithSelector with zundo middleware unleashes Pandora's box and it does not make sense to me, a relatively new typescipt-er.

I have tried extending BlueprintStoreState with zundo's UndoState (reference: https://github.com/charkour/zundo#alternatively-use-the-middleware), but that got me nowhere).

Current code:

interface BlueprintStoreState extends Blueprint {}

const blueprintStore = create<
  BlueprintStoreState,
  SetState<BlueprintStoreState>,
  GetState<BlueprintStoreState>,
  Mutate<
    StoreApi<BlueprintStoreState>,
    [['zustand/subscribeWithSelector', never]]
  >
>(
  subscribeWithSelector(() => blueprintTemplate),
);

tresabhi avatar Mar 02 '22 02:03 tresabhi

Hey! Thanks for making this issue.

To be honest, I haven't tried using zundo with SubscribeWithSelector. Would you be willing to provide a code sandbox with the issue you are seeing? That will help me debug the issue more quickly. Thanks!

charkour avatar Mar 02 '22 04:03 charkour

Wow, I made it work in the sandbox somehow; I surprise myself sometimes...

https://codesandbox.io/s/angry-star-njtw0r

Edit: I haven't tested it yet, so I am going to keep this issue open. If I find no issues, I'll close it.

tresabhi avatar Mar 02 '22 17:03 tresabhi

Awesome, thanks for the update. Check back and let me know how it goes. Thanks!

charkour avatar Mar 02 '22 17:03 charkour

I updated the sandbox to actually test the selector, and it is working flawlessly!

Edit: feel free to put this in the docs as an example.

tresabhi avatar Mar 02 '22 17:03 tresabhi

Thanks!! I'll update the docs tonight. Thanks for the example and for the updates!

charkour avatar Mar 02 '22 18:03 charkour

Re-opening so I update the docs.

charkour avatar Aug 03 '22 13:08 charkour

Updated the code sandbox for v2. Works!

Thanks for the initial implementation @tresabhi!

charkour avatar Sep 25 '22 23:09 charkour