feudal/context/dispatcher.go

42 lines
1.0 KiB
Go
Raw Normal View History

2024-05-05 07:11:52 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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)
}