system.go 629 B

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