Watchers - a proposal for reacting to context changes #2399
Replies: 3 comments 5 replies
-
This looks interesting! Another thought would be.. if you trigger 4 actions and a computed property that depends on the context changed by all of them.. do you trigger it 4 times? |
Beta Was this translation helpful? Give feedback.
-
It would be great to have some feedback on this proposal. I also wanted to note that if |
Beta Was this translation helpful? Give feedback.
-
Isn't the potential problem here that you have too many events touching a single context variable? Wouldn't namespacing them (and matching descriptors like This would introduce an extra step to the microstep algorithm but what I'm most concerned about is that this makes the modeling part potentially more fuzzy. Instead of reacting to business/process-oriented events you want to focus more on an implementation detail (a context variable). |
Beta Was this translation helpful? Give feedback.
-
This is a proposal to bring reactivity to XState by adding a
watch
property to state nodes. Thewatch
value is an object or array of objects containing:actions
dependencies
- an array of strings that are keys of properties on the machinecontext
Each time any of the specified
context
properties change, the actions are executed.With this proposal it is possible to:
As watchers are part of the state node definition, they can be located at any position in the state hierarchy. This offers a lot of flexibility and means that the actions triggered by
context
changes are determined by the current state.What problems does this solve?
When using XState, there is often a tradeoff between locating application code in XState or in UI components. By moving the code into XState you can lose some of the benefits of using a reactive framework.
The same
context
value can be updated in response to different events. If further actions should then be executed in response to this change then these need to be duplicated in different parts of the machine definition. This can be repetitive and error-prone.Examples
Derived context
Sync value with external service
Implementation
Given the way that XState is implemented, I think it makes sense to implement this by dirty checking the
context
object. This should happen after anyassign
actions have executed in response to an event. Watchers with one or moredependencies
marked as dirty should then execute theirassign
actions. Another dirty check should take place and this process should loop until no watchers are called. Any properties that have changed on thecontext
object since the event was fired should then be marked as dirty and the watchers should be traversed one more time to push non-assign actions to theactions
array.It should be noted that if
assign
actions always used the object form then dirty checking may be unnecessary. The changed properties could instead be determined by the keys in the assigner. I wonder if watchers could render the function form for assigners unnecessary as it is often used when multiple context values depend on each other. I imagine the combination of explicit assignments and watchers with explicit dependencies would also be great for visualisation.Questions
Should the watchers execute on entering a new state?
This would be desired in some cases, such as the derived context example above. More consideration is needed to decide if this should be the default or an additional option.
Should nested values be allowed as dependencies?
Although this might be possible using dot notation, my feeling is that it's better to encourage flatter data structures and only detect changes at the top level.
Summary
This is a proposal for a new
watch
property that enables declaringactions
that are executed in response tocontext
changes. I am interested in whether you think the idea is worth considering.Beta Was this translation helpful? Give feedback.
All reactions