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

Is there a way to do OnEntry with parameter trigger right before updating the state #409

Open
rsahli opened this issue Dec 17, 2020 · 3 comments

Comments

@rsahli
Copy link

rsahli commented Dec 17, 2020

Hi @HenningTorsteinsenBouvet,

Thanks for this project, this is definitely a "must-have" for .NET applications ! I searched in the already posted questions but did not find an anwser. This is the closest match I found, but I would need the parameter trigger.

I am configuring states with OnEntry/OnExit with parameter trigger. Very much exactly as your ObjectsAsStates example.

Somehow, I am interested to perform an OnActivate (no state change), but with the trigger as input parameter.
I would like to prevent StateMachine.State to be updated before I can execute some "activation work". This "activation work" is really specific to this state, but I would like to prevent other processes to think that the state is already active if I am still in the activation work.

But I fully agree that the existing OnActivate and OnEntry functions have the right signature and work as expected. Have you ever required to perform some activation work of a specific state, with trigger paramter as input?

Thank you for you help, take care !

@HenningNT
Copy link
Contributor

Thank you for those kind words, but I must point out that there are many contributors to this project.

When we first added OnActivate we thought "something" would restore the state from disk / database, and then execute Activate to get the object up and ready for action. We thought that the object would have all its required information contained in the object. I can, however, see that having parameters to Activate could be an advantage.

I wonder if an InternalTransition would do the trick for you? This doesn't change the state.
Or you could supply the object with the required information, and then have the OnActivate callback use that information?

@rsahli
Copy link
Author

rsahli commented Dec 18, 2020

Thank you for your fast reply.

I wonder if an InternalTransition would do the trick for you? This doesn't change the state.

InternalTransition is for a specific trigger, which means I would need to define internal transition for all the triggers that can activate this state. This is not so good as I would re-configure state transitions.

The same "activation work" shall be done independently of the trigger.

The workaround I found so far, is to still use OnEntry but to store the state outside of the StateMachine object. I update it when StateMachine.Fire(trigger) returned.

If no one every requested an OnActivate(Action<StateMachine<TState, TTrigger>.Transition> activateAction), it probably means I am trying to achieve something that is not very common, which I would like to avoid of course.

Thank you !

@HenningNT
Copy link
Contributor

I think your use case is valid, but not very common 😉
The workaround could maybe also use the state mutator, available in the StateMachine constructor: public StateMachine(Func<TState> stateAccessor, Action<TState> stateMutator).

I think an OnActivate with parameters would not take a transition as a parameter, but one generic parameter like this OnActivate<TArg>(Action<TArg> activateAction)

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

No branches or pull requests

2 participants