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

Expose internal transitions #376

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Stateless/StateMachine.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ async Task InternalFireOneAsync(TTrigger trigger, params object[] args)
{
var transition = new Transition(source, destination, trigger, args);

// Alert all listeners of state transition
await _onTransitionedInternalEvent.InvokeAsync(transition);

await CurrentRepresentation.InternalActionAsync(transition, args).ConfigureAwait(false);
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/Stateless/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public partial class StateMachine<TState, TTrigger>
private readonly Action<TState> _stateMutator;
private UnhandledTriggerAction _unhandledTriggerAction;
private OnTransitionedEvent _onTransitionedEvent;
private OnTransitionedEvent _onTransitionedInternalEvent;
private readonly FiringMode _firingMode;

private class QueuedTrigger
Expand Down Expand Up @@ -93,6 +94,7 @@ public StateMachine(TState initialState, FiringMode firingMode) : this()
{
_unhandledTriggerAction = new UnhandledTriggerAction.Sync(DefaultUnhandledTriggerAction);
_onTransitionedEvent = new OnTransitionedEvent();
_onTransitionedInternalEvent = new OnTransitionedEvent();
}

/// <summary>
Expand Down Expand Up @@ -386,7 +388,9 @@ void InternalFireOne(TTrigger trigger, params object[] args)
// Internal transitions does not update the current state, but must execute the associated action.
var transition = new Transition(source, source, trigger, args);
CurrentRepresentation.InternalAction(transition, args);
break;
_onTransitionedInternalEvent.Invoke(transition);

break;
}
default:
throw new InvalidOperationException("State machine configuration incorrect, no handler for trigger.");
Expand Down Expand Up @@ -600,5 +604,18 @@ public void OnTransitioned(Action<Transition> onTransitionAction)
if (onTransitionAction == null) throw new ArgumentNullException(nameof(onTransitionAction));
_onTransitionedEvent.Register(onTransitionAction);
}

/// <summary>
/// Registers a callback that will be invoked every time the statemachine
/// has an internal transition.
/// </summary>
/// <param name="onInternalTransitionAction">
/// The action to execute, accepting the details of the transition
/// </param>
public void OnTransitionedInternal(Action<Transition> onInternalTransitionAction)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is now a public method, I would be inclined to drop the internal keyword

Suggested change
public void OnTransitionedInternal(Action<Transition> onInternalTransitionAction)
public void OnTransitioned(Action<Transition> onTransitionAction)

{
if (onInternalTransitionAction == null) throw new ArgumentNullException(nameof(onInternalTransitionAction));
_onTransitionedInternalEvent.Register(onInternalTransitionAction);
}
}
}