You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
765 B
46 lines
765 B
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,
|
|
}
|
|
}
|