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

Add support to reset a store #11

Open
jaecktec opened this issue Aug 16, 2022 · 3 comments
Open

Add support to reset a store #11

jaecktec opened this issue Aug 16, 2022 · 3 comments

Comments

@jaecktec
Copy link

jaecktec commented Aug 16, 2022

In certain use-cases I want to reset my store to a certain state

example:

const store = makeStore<T>({});

store.set({hello: "world"});

...

store.set({}); // does not modify the store
store.set(Object.keys(store.state).reduce((r,c) => {...r, [c]: undefined}, {})) // actually resets but is cumbersome

idea:

store.set({}, {overwrite: true});

I'd be willing to create a PR if you think this a feature that should exist in statery

@hmans
Copy link
Owner

hmans commented Aug 17, 2022

Thanks for submitting the issue, and I agree that it would be a good feature to have! For the upcoming 0.6 release, I have started adding options to set (forceNotify in this case), and I'd be happy to change the API for that to take an options object.

If you're willing to tackle that and submit a PR, go ahead! Otherwise I would probably just take care of it some time over the next couple of weeks, as part of the work to push 0.6 out of the door.

@jaecktec
Copy link
Author

Now that I check in the sources, maybe it's better to create a different method.
The interface is Partial<T>, however for an overwrite we should use T.

I still favour the options object - because it makes it more flexible in the future. However my suggestion is probably not working

@hmans
Copy link
Owner

hmans commented Aug 17, 2022

That's fine. I will think about a reset (analogous to set), or something similar.

As an alternative userland workaround/solution, consider writing a function that returns the desired initial state:

const initialState = (): YourStateType => ({ foo: "bar", count: 0, stuff: undefined })

And then just using that to update the store using the normal set method:

store.set(initialState())

It will not remove properties outside of your type that were added for some reason, of course.

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

Successfully merging a pull request may close this issue.

2 participants