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

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. HandleCommand(bus *EventBus, command Command)
  11. }
  12. type ServiceCommon struct {
  13. inactive uint32
  14. }
  15. func (s *ServiceCommon) Active() bool {
  16. return atomic.LoadUint32(&s.inactive) == 0
  17. }
  18. func (s *ServiceCommon) markInactive() {
  19. atomic.StoreUint32(&s.inactive, 1)
  20. }
  21. type callbackService struct {
  22. ServiceCommon
  23. cb func(event Event) bool
  24. }
  25. func (c *callbackService) HandleEvent(_ *EventBus, event Event) {
  26. if !c.cb(event) {
  27. c.markInactive()
  28. }
  29. }
  30. func newCallbackService(cb func(event Event) bool) Service {
  31. return &callbackService{
  32. ServiceCommon: ServiceCommon{
  33. inactive: 0,
  34. },
  35. cb: cb,
  36. }
  37. }