Skip to content

Spike: Internal state machine exploration #142

Description

@mattwzawislak

Problem

As discussed here: #141

There is a problem with the way the internal state is represented, which is leading to a lot of messy code.

You can see this issue with a method like Deactivate where two branches handle a lot of the same logic but have subtle differences. These differences are largely a by-product of the previous-current-next shifting.

if (this.internalState.IsEntering)
{
    await DeactivateTriggers(token);
    if (PreviousState != null)
    {
        await PreviousState.Deactivate(Parameters, token);
    }
    Parameters.Clear();
    PreviousState = null;
    NextState = null;
    this.internalState = InternalState.Inactive;
    return;
}

try
{
    BeginTransition();
    await ExitState(token);
    await DeactivateTriggers(token);

    await PreviousState.Deactivate(Parameters, token);

    Parameters.Clear();
    PreviousState = null;
    this.internalState = InternalState.Inactive;
}
catch when (CurrentState == null)
{
    Rollback();
    this.internalState = InternalState.Recovery;
    throw;
}

The repetitions here aren't just "repetitive code" but "repetitive logic".

Solution

Try to extract an internal state machine from the state machine implementation to move the internal mechanisms (like EnterState) into an isolated component. This would allow the state machine to call upon the same internal state machine methods without duplicating business logic or drifting away.

Additional Context

None.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions