-
Notifications
You must be signed in to change notification settings - Fork 399
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
Proposal: State Operators #545
Comments
Great ideas @markwhitfeld - our team would really benefit from those array operators, that's where I think we have the most verbose patching code. This would really clean things up for us. |
Currently, you can create a child state (sub state) for nested objects and trigger patchState for that same action in both parent and child states, but having this would be useful too! |
@garthmason That's great to hear. I have been busy moving house lately so have not had much chance to work on it. @MurhafSousli The child state would be useful if you had a nested child object (as opposed to an array) but I see the main utility for this where you want to do nested updates of an array. |
@markwhitfeld why not split that in two parts:
In this way, the core functionality doesn't grow much and at same time allows other kind of mutation by function, maybe passing the current state for it: |
@juracy This is almost exactly the idea. The operators will exist in a separate package and the base The signature will be more like this: This would allow for the use of something like |
See this stackblitz for the playground: |
@markwhitfeld Humm, sorry, a typical TL;DR mistake (well not too long, indead) ! So, why not submit a PR for patchState right away? |
I'm wanting to get to get the Typescript typings definitions working great for this first and this requires TypeScript 2.8 and above. @amcdnl is wanting to wait until Angular supports Typescript 2.8 before we take that dependency. I think that Angular 6.1 may be on TypeScript 2.9 now. I can bring this in with @amcdnl's go ahead. |
I just stumbled across an issue that could've been solved with this approach, looking forward! :) |
This comment has been minimized.
This comment has been minimized.
Hi everyone. So I think that this idea is evolving a bit...
After
Doesn't seem like much but this function could then be extracted and reused. Also this is the exact signature of these "patch operators". So you could forseeably do this:
So, what do I need from you? Since these operators could be more general purpose than just for patching we could think of a better name. Some of the options I have brainstormed with @eranshmil are:
As a general description of what they are I would say that they are functions that define transformations to apply to an existing immutable state to produce a new state. Can I ask for your votes or suggestions for a name? |
|
Can be closed? |
Published a teaser for the feature ahead of the 3.4 release: |
Your documentation is still pointing to this thread like this feature is a WIP |
@isochronous Thank you for pointing that out! |
Done. I made a very minimal change, simply adding a sentence with a link
to the state operators docs.
…On Thu, Aug 19, 2021 at 6:23 PM Mark Whitfeld ***@***.***> wrote:
@isochronous <https://github.com/isochronous> Thank you for pointing that
out!
Would you like to submit a PR with an improvement?
I definitely think that State Operators are one of the magic ingredients
in ngxs and it should be highlighted more here... and definitely the fact
that it is no longer a Work In Progress.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#545 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAF5DULD6FYRY2KPY7A74SDT5V76DANCNFSM4FP2VC5A>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>
.
|
Versions
Background
The NGXS patchState method is used to do immutable object updates to the container state slice without the typical long-handed syntax. This is very neat and convenient because you do not have to use the getState and setState as well as the Object.assign(…)or the spread operator to update the state. The patchState method only offers a shallow patch and as a result is left wanting in more advanced scenarios.
What if it is possible to make the patchState method extensible to create a more advanced yet simple mechanism for state updates?… Let me introduce patch operators…
Proposal
The basic idea of this proposal is that we could describe the modifications to the state using curried functions that are given any inputs that they need to describe the change and are assigned to the part of the state tree that they should be applied to. The curried function is then run using the state slice that they are assigned to and the returned value is used as the new value for that state slice.
The function that produces the curried function that is still awaiting the slice of state to be applied to is called a patch operator. The type of this curried function could be defined as follows:
We can make a number of these patch operator functions in order to help with common scenarios for state manipulation. Some ideas are:
patch<T>(value: T) : op<T>
set<T>(value: T) : op<T>
iif<T>(condition: Predicate<T>, then: op<T>, else: op<T>) : op<T>
And some array operators:
patchItem<T>(selector: number | Predicate<T>, operator: op<T>) : op<T[]>
removeItem<T>(selector: number | Predicate<T>) : op<T[]>
insertItem<T>(value: T, beforePosition? : number) : op<T[]>
Example:
We could express some modifications to a state as follows:
Please add any other ideas for operators to the comments.
Feedback welcome!
PS. Related Meduim posts here:
Initial proposal: https://medium.com/@mark.whitfeld/ngxs-proposal-patch-operators-75b24d309b91
Announcement before v3.4: https://medium.com/ngxs/ngxs-state-operators-8b339641b220
The text was updated successfully, but these errors were encountered: