Skip to content

Commit 8b0a459

Browse files
authored
Merge pull request #82 from SuessLabs/feature/80-Cleanup-IsContextPersistent
#80 - Cleanup Chore IsContextPersistent Type Cleanup for Keys
2 parents eeee38b + 8bd0c17 commit 8b0a459

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

source/Lite.StateMachine/StateMachine.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public async Task<StateMachine<TStateId>> RunAsync(
227227
if (result is null)
228228
break;
229229

230-
var nextId = ResolveNext(reg, result.Value);
230+
var nextId = StateMachine<TStateId>.ResolveNext(reg, result.Value);
231231
if (nextId is null)
232232
break;
233233

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

240+
/// <summary>Get next state transition based on state's result.</summary>
241+
/// <param name="reg">State registration.</param>
242+
/// <param name="result">State's returned result.</param>
243+
/// <returns><see cref="TStateId"/> to go to next or NULL to bubble-up or end state machine process.</returns>
244+
private static TStateId? ResolveNext(StateRegistration<TStateId> reg, Result result) => result switch
245+
{
246+
Result.Success => reg.OnSuccess,
247+
Result.Error => reg.OnError,
248+
Result.Failure => reg.OnFailure,
249+
_ => null,
250+
};
251+
240252
/// <summary>
241253
/// Retrieves an existing state instance associated with the specified registration,
242254
/// or creates and stores a new instance if none exists.
@@ -269,30 +281,15 @@ private StateRegistration<TStateId> GetRegistration(TStateId stateId)
269281
return reg;
270282
}
271283

272-
/// <summary>Get next state transition based on state's result.</summary>
273-
/// <param name="reg">State registration.</param>
274-
/// <param name="result">State's returned result.</param>
275-
/// <returns><see cref="TStateId"/> to go to next or NULL to bubble-up or end state machine process.</returns>
276-
private TStateId? ResolveNext(StateRegistration<TStateId> reg, Result result)
277-
{
278-
return result switch
279-
{
280-
Result.Success => reg.OnSuccess,
281-
Result.Error => reg.OnError,
282-
Result.Failure => reg.OnFailure,
283-
_ => null,
284-
};
285-
}
286-
287284
private async Task<Result?> RunAnyStateRecursiveAsync(
288285
StateRegistration<TStateId> reg,
289286
PropertyBag? parameters,
290287
PropertyBag? errors,
291288
CancellationToken ct)
292289
{
293290
// Ensure we always operate on non-null, shared bags
294-
parameters = parameters ?? [];
295-
errors = errors ?? [];
291+
parameters ??= [];
292+
errors ??= [];
296293

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

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

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

403400
if (errors is not null)
404401
{
405-
foreach (string k in errors.Keys)
402+
foreach (var k in errors.Keys)
406403
if (!originalErrorKeys.Contains(k)) errors.Remove(k);
407404
}
408405
}

0 commit comments

Comments
 (0)