A LogicBlock is a class that receives inputs, maintains a single state instance, and produces outputs. In v6, logic blocks do not use generic type parameters. Instead, states are defined as class records that inherit from LogicBlockState and live outside the logic block class.
To implement one:
- Create a class inheriting from
LogicBlock. - Preallocate states in the constructor using
Set(). - Define an abstract
LogicBlockState record containing nested Input and Output classes. - Implement concrete state records that implement
IGet<T> for specific inputs and return a Type (via To<T>()) to trigger transitions in their On handlers.
public partial class LightSwitchLogic : LogicBlock
{
public LightSwitchLogic()
{
Set(new LightSwitchState.PoweredOn());
Set(new LightSwitchState.PoweredOff());
}
}
public abstract partial record LightSwitchState : LogicBlockState
{
public static class Input { public readonly record struct Toggle; }
public static class Output { public readonly record struct StatusChanged(bool IsOn); }
}
public partial record LightSwitchState
{
public record PoweredOn : LightSwitchState, IGet<Input.Toggle>
{
public PoweredOn() => this.OnEnter(() => Output(new Output.StatusChanged(true)));
public Type On(in Input.Toggle input) => To<PoweredOff>();
}
public record PoweredOff : LightSwitchState, IGet<Input.Toggle>
{
public PoweredOff() => this.OnEnter(() => Output(new Output.StatusChanged(false)));
public Type On(in Input.Toggle input) => To<PoweredOn>();
}
}