events_hub.go 804 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package web
  2. type idEventFunc struct {
  3. id string
  4. ef EventFunc
  5. }
  6. type EventsHub struct {
  7. eventFuncs []*idEventFunc
  8. }
  9. func (p *EventsHub) RegisterEventFunc(eventFuncId string, ef EventFunc) (key string) {
  10. key = eventFuncId
  11. if p.eventFuncById(eventFuncId) != nil {
  12. return
  13. }
  14. p.eventFuncs = append(p.eventFuncs, &idEventFunc{eventFuncId, ef})
  15. return
  16. }
  17. func (p *EventsHub) addMultipleEventFuncs(vs ...interface{}) (key string) {
  18. if len(vs)%2 != 0 {
  19. panic("id and func not paired")
  20. }
  21. for i := 0; i < len(vs); i = i + 2 {
  22. p.RegisterEventFunc(vs[i].(string), vs[i+1].(func(ctx *EventContext) (r EventResponse, err error)))
  23. }
  24. return
  25. }
  26. func (p *EventsHub) eventFuncById(id string) (r EventFunc) {
  27. for _, ne := range p.eventFuncs {
  28. if ne.id == id {
  29. r = ne.ef
  30. return
  31. }
  32. }
  33. return
  34. }