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.

48 lines
775 B

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package lucifer3
  2. import (
  3. "sync/atomic"
  4. )
  5. type Service interface {
  6. Active() bool
  7. HandleEvent(bus *EventBus, event Event)
  8. }
  9. type ActiveService interface {
  10. Service
  11. HandleCommand(bus *EventBus, command Command)
  12. }
  13. type ServiceCommon struct {
  14. inactive uint32
  15. }
  16. func (s *ServiceCommon) Active() bool {
  17. return atomic.LoadUint32(&s.inactive) == 0
  18. }
  19. func (s *ServiceCommon) markInactive() {
  20. atomic.StoreUint32(&s.inactive, 1)
  21. }
  22. type callbackService struct {
  23. ServiceCommon
  24. cb func(event Event) bool
  25. }
  26. func (c *callbackService) HandleEvent(_ *EventBus, event Event) {
  27. if !c.cb(event) {
  28. c.markInactive()
  29. }
  30. }
  31. func newCallbackService(cb func(event Event) bool) Service {
  32. return &callbackService{
  33. ServiceCommon: ServiceCommon{
  34. inactive: 0,
  35. },
  36. cb: cb,
  37. }
  38. }