pancsta / asyncmachine-go

AOP Actor Model for distributed workflows
https://asyncmachine.dev
MIT License
72 stars 0 forks source link

feat(machine): add Index() and Time.Is(index) #123

Closed pancsta closed 2 months ago

pancsta commented 2 months ago

Operating on state indexes is very widespread in the codebase now and having checking methods directly on the Time struct makes asserting prev/next state in transition handlers short and concise.

Example from rpc#Client:

func (c *Client) StartEnd(e *am.Event) {
    // gather state from before the transition
    before := e.Transition().TimeBefore
    mach := e.Machine
    wasConn := before.Is1(mach.Index(ss.Connecting)) ||
        before.Is1(mach.Index(ss.Connected))

    // graceful disconnect
    if wasConn {
        c.Mach.Add1(ss.Disconnecting, nil)
    }
}

Theres helpers for lists of indexes.