stateless
stateless copied to clipboard
Read from to the stateAccessor not so often
When using the StateMachine.ctor for the use with external storage
public StateMachine(Func<TState> stateAccessor, Action<TState> stateMutator)
the stateAccessor-delegate gets called rather often. For example for a simple state transition the pattern looks like this:
Loading state
Loading state
Loading state
Saving state
Loading state
If I would naively attach some service calls or database operations this might cause some pain.
What I would suggest as a default behaviour is to load state via stateAccessor() just a single time on creation of the state machine.
I've thought about it and came to the conclusion that the best would be a new overload for the ctor being a mixture of both existing ctors:
public StateMachine(TState initialState, Action<TState> stateMutator)
Reasoning behind this: Getting the initial state of the machine is not a responsibility of the machine, and therefore should be done outside and provided (via the first paramter initialState). Saving on the other hand might be performed on every state change, using the stateMutator.
PR to follow.