system.go 763 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package event
  2. type System struct {
  3. busList []*eventBus
  4. }
  5. func NewSystem() *System {
  6. return &System{[]*eventBus{}}
  7. }
  8. // NewBus params are 0 - emitPrefix
  9. func (s *System) NewBus(params ...string) Bus {
  10. emitPrefix := ""
  11. if len(params) != 0 {
  12. emitPrefix = params[0]
  13. }
  14. evtBus := &eventBus{s, emitPrefix, map[string]*[]HandlerFunc{}}
  15. s.busList = append(s.busList, evtBus)
  16. return evtBus
  17. }
  18. func (s *System) RemoveBus(bus Bus) {
  19. where := -1
  20. for i, b := range s.busList {
  21. if b == bus { // Pointer comparison somehow?
  22. where = i
  23. break
  24. }
  25. }
  26. s.busList = append(s.busList[:where], s.busList[where+1:]...)
  27. }
  28. func (s *System) handleEvent(evt Event) {
  29. // Pass this event trough all buses
  30. for _, bus := range s.busList {
  31. bus.handleEvent(evt)
  32. }
  33. }