Files
Server/service.go
T
2022-08-14 13:57:56 +02:00

47 lines
765 B
Go

package lucifer3
import (
"sync/atomic"
)
type Service interface {
Active() bool
HandleEvent(bus *EventBus, event Event)
}
type ActiveService interface {
HandleCommand(bus *EventBus, command Command)
}
type ServiceCommon struct {
inactive uint32
}
func (s *ServiceCommon) Active() bool {
return atomic.LoadUint32(&s.inactive) == 0
}
func (s *ServiceCommon) markInactive() {
atomic.StoreUint32(&s.inactive, 1)
}
type callbackService struct {
ServiceCommon
cb func(event Event) bool
}
func (c *callbackService) HandleEvent(_ *EventBus, event Event) {
if !c.cb(event) {
c.markInactive()
}
}
func newCallbackService(cb func(event Event) bool) Service {
return &callbackService{
ServiceCommon: ServiceCommon{
inactive: 0,
},
cb: cb,
}
}