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

reset only subset of the state in withReset() #160

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

daboudii
Copy link

In case you need to re initialize your state

@rainerhahnekamp
Copy link
Collaborator

Thanks @daboudii, there is already an extension withReset. So I would suggest to use that one and if there is something missing, to make a PR for withReset.

I have to admit that I like your approach of using a function setInitial() in patchState much, much better than how it is implemented in withReset. So maybe that is something you could start with - if you want.

@daboudii
Copy link
Author

The withReset do not meet my needs since it reset all the store and not just the callState, I implemented the setInitial() in this MR, is there anything more to add ?

@rainerhahnekamp
Copy link
Collaborator

@daboudii, yes what options do we have to do this in withReset()?

Would it help, if withReset() supports selecting a subset of the state?

The reason why I'm pushing for the integration in the other feature is that we would not be limited to support a very specific use case ( callState) but a much wider range.

@daboudii
Copy link
Author

Ok get it, I will have a look

@daboudii daboudii changed the title add setInitial in withCallState() reset only subset of the state in withReset() Mar 14, 2025
@daboudii
Copy link
Author

@rainerhahnekamp let me know if it need something else

withMethods((store): PublicMethods => {
// workaround to TS excessive property check
const methods = {
resetState() {
patchState(store, store._resetState.value);
},
resetSlice(slice: string | string[]) {
patchState(store, (state) => ({
Copy link

@abhijit-chikane abhijit-chikane Mar 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work🎉🎉 @daboudii
It would be even better if we could just reset the part of the slice
in case of obj sometimes we don't want to reset the whole inner object but part of the object

Copy link
Author

@daboudii daboudii Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like resetSlice('address.city') ? If yes it will add even more complexity, is it really necessary ?

Copy link

@abhijit-chikane abhijit-chikane Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like resetSlice('address.city') ? If yes it will add even more complexity, is it really necessary ?

I think it will definitely add complexity but it will add much more value for project like ours and many others where data structures are much more complex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I will add that, in this case just easier to patch your state with the value you which to change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I touched upon that in my review below #160 (review)

Copy link
Collaborator

@rainerhahnekamp rainerhahnekamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! I have a question about the design.

State changes would be better handled as functions within patchState. This ensures that patchState remains the sole entity modifying state and allows multiple update functions to run together as an “atomic update” of the internal Signal.

That being said, setResetState() was already implemented in a way that doesn’t align with this approach. What if we provide a more flexible reset() function that supports both full and partial state resets and is type-safe as well.

// reset the full state
patchState(store, reset())

// reset the partial state
patchState(store, reset(state => ({address: state.address, user: state.user}))

What do you say?

@daboudii
Copy link
Author

daboudii commented Mar 19, 2025

What do you say?

Yes it looks way better like that ! I'm starting the rewrite

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 this pull request may close these issues.

3 participants