-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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 OnTransition::Any that runs on any change to the state S
#10334
Conversation
@lee-orr, following your work in #10088 I'm really curious about what you think of this design. I'm fond of the simple incremental change. Still forming an opinion on the temporary resource 🤔 I think maybe it could be replaced by just looking at the current and next state resource that already exists? |
Yeah, I am also not 100% sure about the temporary resource. But I think it is not really possible with the If I am not missing anything, at least it's quite clean, because nobody can create, clone or modify the |
Yeah, very nice encapsulation :) I think I'm on board with it now. |
I think the design makes sense. There is actually a similar-ish temporary resource in #10088 - the An option for that is to have the state transition resource expose a "from" and "to" function rather than methods, since that will make it relatively easy to replace |
I definitely like the
What exactly do you mean by this? Answering my own question, whether the resource should only be made available for I think yes, because
|
Sorry! I meant methods rather than fields... I.e. abstract away how things are stored internally. |
As for your second point - I think it might be better to have it present for the entire transition, but make the values Options. This could allow a janky version of matching on enter/exit as well, where you check the resource for the outgoing/incoming value respectively in the system you're running. |
Okay, so for the methods vs fields: The fields are already private, so the methods are the only way to access them. Regarding the Please feel free to disagree, it's just the way I currently see it:) |
I disagree there, primarily on grounds of making the option explicit - I don't want people to find things crashing if they try to use a system for both OnTransition and OnEnter, for example - and OnEnter also runs the first time. Another option is to default to having it be a transition where both states are identical if it's the first entrance/exit? |
Can you give me an example where that would make sense? For me, the two are actually so different that I can't imagine what I would reuse. The way I see it, I think the root problem for providing the |
OK - sorry, it took me a bit to respond... I don't know why I was under the impression the resource exists during OnEnter/OnExit as well... It may have just been mis-remembering since that's what happens in #10088. I think my concern really comes from how Given the currently available API's and what you are proposing, I think you are probably right. And since I don't know if/when #10088 will get merged, maybe we don't need to actually plan for a compatible API. If you're interested - the reasons #10088 exposes ActiveTransition across all 3 transition schedules are:
One possible API compromise is doing something like this: // This code is not actually valid or fully formed, it's just expressing the idea
ActiveTransition<S> {
main: S,
secondary: Option<S>,
direction: TransitionDirection
}
enum TransitionDirection {
Entering,
Exiting
}
impl<S> ActiveTransition<S> {
fn entering(&self) -> S {
match self.direction {
Entering => self.main,
Exiting => self.secondary.unwrap_or(self.main)
}
}
fn exiting(&self) -> S {
match self.direction {
Entering => self.secondary.unwrap_or(self.main),
Exiting => self.main
}
}
} |
Ok, thanks for the feedback. I also took a quick look at #10088: To be honest, it does offer some new/powerful possibilities, but it seems very complex to me and also has a lot of subtle cleverness hidden in it. |
@jannik4 @alice-i-cecile - Should be able to close this at this point I think? |
Objective
I want to be able to run a system in between
OnExit
andOnEnter
that runs on any change to the state. This can be useful if, for example, you want to cover all the possibilities of the transition with a match statement.Solution
OnTransition { from, to }
schedule with an enum:ActiveTransition
resource that is made available by theapply_state_transition
system during the transition. This can be used to access the exiting and entering states.Open Questions
Currently theIt is now only available to theActiveTransition
resource is made available to all ofOnExit
/OnTransition
/OnEnter
. Maybe it should only be made available forOnTransition
?OnTransition
schedules.Changelog
OnTransition { from, to }
struct withOnTransition { Any, Exact { from, to } }
enumActiveTransition
resourceMigration Guide