Skip to content
Merged
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
39 changes: 18 additions & 21 deletions source/Lite.StateMachine/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public async Task<StateMachine<TStateId>> RunAsync(
if (result is null)
break;

var nextId = ResolveNext(reg, result.Value);
var nextId = StateMachine<TStateId>.ResolveNext(reg, result.Value);
if (nextId is null)
break;

Expand All @@ -237,6 +237,18 @@ public async Task<StateMachine<TStateId>> RunAsync(
return this;
}

/// <summary>Get next state transition based on state's result.</summary>
/// <param name="reg">State registration.</param>
/// <param name="result">State's returned result.</param>
/// <returns><see cref="TStateId"/> to go to next or NULL to bubble-up or end state machine process.</returns>
private static TStateId? ResolveNext(StateRegistration<TStateId> reg, Result result) => result switch
{
Result.Success => reg.OnSuccess,
Result.Error => reg.OnError,
Result.Failure => reg.OnFailure,
_ => null,
};

/// <summary>
/// Retrieves an existing state instance associated with the specified registration,
/// or creates and stores a new instance if none exists.
Expand Down Expand Up @@ -269,30 +281,15 @@ private StateRegistration<TStateId> GetRegistration(TStateId stateId)
return reg;
}

/// <summary>Get next state transition based on state's result.</summary>
/// <param name="reg">State registration.</param>
/// <param name="result">State's returned result.</param>
/// <returns><see cref="TStateId"/> to go to next or NULL to bubble-up or end state machine process.</returns>
private TStateId? ResolveNext(StateRegistration<TStateId> reg, Result result)
{
return result switch
{
Result.Success => reg.OnSuccess,
Result.Error => reg.OnError,
Result.Failure => reg.OnFailure,
_ => null,
};
}

private async Task<Result?> RunAnyStateRecursiveAsync(
StateRegistration<TStateId> reg,
PropertyBag? parameters,
PropertyBag? errors,
CancellationToken ct)
{
// Ensure we always operate on non-null, shared bags
parameters = parameters ?? [];
errors = errors ?? [];
parameters ??= [];
errors ??= [];

// Run Normal or Command State
if (!reg.IsCompositeParent)
Expand Down Expand Up @@ -362,7 +359,7 @@ private StateRegistration<TStateId> GetRegistration(TStateId stateId)

// TODO (#76): Extract the Context.OnSuccess/Error/Failure override (if any)
lastChildResult = childResult;
var nextChildId = ResolveNext(childReg, childResult.Value);
var nextChildId = StateMachine<TStateId>.ResolveNext(childReg, childResult.Value);

// NULL mapping => last child => bubble-up to parent and exit
if (nextChildId is null)
Expand Down Expand Up @@ -396,13 +393,13 @@ private StateRegistration<TStateId> GetRegistration(TStateId stateId)
{
if (parameters is not null)
{
foreach (string k in parameters.Keys)
foreach (var k in parameters.Keys)
if (!originalParamKeys.Contains(k)) parameters.Remove(k);
}

if (errors is not null)
{
foreach (string k in errors.Keys)
foreach (var k in errors.Keys)
if (!originalErrorKeys.Contains(k)) errors.Remove(k);
}
}
Expand Down
Loading