// Copyright 2024 Matthew Rich . All rights reserved. package context; import ( "feudal/feudal" "gitea.rosskeen.house/rosskeen.house/machine" "feudal/message" ) type DispatchReceiveFunc func(m message.Envelope, r message.Receiver) type Dispatcher interface { Dispatch(m message.Envelope, r message.Receiver) } type DispatchReceive struct { worker feudal.Context } func (d *DispatchReceive) DispatchToStateSubscribers(m message.Envelope, r message.Receiver) { switch b := m.Body().(type) { case *machine.EventMessage: recipients := d.worker.Subscribers("lifecycle") var event machine.EventMessage = *b recipients.Send(message.New(event, m.Sender())) } } func (d *DispatchReceive) Dispatch(m message.Envelope, r message.Receiver) { d.DispatchToStateSubscribers(m, r) r.Receive(m) } func DefaultDispatcher(worker feudal.Context) Dispatcher { return &DispatchReceive{ worker: worker } } func (f DispatchReceiveFunc) Dispatch(m message.Envelope, r message.Receiver) { f(m, r) }