|
| 1 | +package bus |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "sync" |
| 7 | +) |
| 8 | + |
| 9 | +//The type of the function's first and only argument |
| 10 | +//declares the msg to listen for. |
| 11 | +type HandlerFunc interface{} |
| 12 | + |
| 13 | +type Msg interface{} |
| 14 | + |
| 15 | +//It is a simple but powerful publish-subscribe event system. It requires object to |
| 16 | +//register themselves with the event bus to receive events. |
| 17 | +type Bus interface { |
| 18 | + Dispatch(msg Msg) error |
| 19 | + AddHandler(handler HandlerFunc) error |
| 20 | + AddEventListener(handler HandlerFunc) |
| 21 | + Publish(msg Msg) error |
| 22 | +} |
| 23 | + |
| 24 | +type InProcBus struct { |
| 25 | + sync.Mutex |
| 26 | + handlers map[string]reflect.Value |
| 27 | + listeners map[string][]reflect.Value |
| 28 | +} |
| 29 | + |
| 30 | +func New() Bus { |
| 31 | + return &InProcBus{ |
| 32 | + handlers: make(map[string]reflect.Value), |
| 33 | + listeners: make(map[string][]reflect.Value), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Dispatch sends an msg to registered handler that were declared |
| 38 | +// to accept values of a msg |
| 39 | +func (b *InProcBus) Dispatch(msg Msg) error { |
| 40 | + nameOfMsg := reflect.TypeOf(msg) |
| 41 | + |
| 42 | + handler, ok := b.handlers[nameOfMsg.String()] |
| 43 | + if !ok { |
| 44 | + return fmt.Errorf("handler not found for %s", nameOfMsg) |
| 45 | + } |
| 46 | + |
| 47 | + params := make([]reflect.Value, 0, 1) |
| 48 | + params = append(params, reflect.ValueOf(msg)) |
| 49 | + |
| 50 | + ret := handler.Call(params) |
| 51 | + v := ret[0].Interface() |
| 52 | + if err, ok := v.(error); ok && err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// Dispatch sends an msg to all registered listeners that were declared |
| 59 | +// to accept values of a msg |
| 60 | +func (b *InProcBus) Publish(msg Msg) error { |
| 61 | + nameOfMsg := reflect.TypeOf(msg) |
| 62 | + listeners := b.listeners[nameOfMsg.String()] |
| 63 | + |
| 64 | + params := make([]reflect.Value, 0, 1) |
| 65 | + params = append(params, reflect.ValueOf(msg)) |
| 66 | + |
| 67 | + for _, listenerHandler := range listeners { |
| 68 | + ret := listenerHandler.Call(params) |
| 69 | + v := ret[0].Interface() |
| 70 | + if err, ok := v.(error); ok && err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// AddHandler registers a handler function that will be called when a matching |
| 78 | +// msg is dispatched. |
| 79 | +func (b *InProcBus) AddHandler(handler HandlerFunc) error { |
| 80 | + b.Mutex.Lock() |
| 81 | + defer b.Mutex.Unlock() |
| 82 | + |
| 83 | + handlerType := reflect.TypeOf(handler) |
| 84 | + validateHandlerFunc(handlerType) |
| 85 | + |
| 86 | + typeOfMsg := handlerType.In(0) |
| 87 | + if _, ok := b.handlers[typeOfMsg.String()]; ok { |
| 88 | + return fmt.Errorf("handler exists for %s", typeOfMsg) |
| 89 | + } |
| 90 | + |
| 91 | + b.handlers[typeOfMsg.String()] = reflect.ValueOf(handler) |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// AddListener registers a listener function that will be called when a matching |
| 96 | +// msg is dispatched. |
| 97 | +func (b *InProcBus) AddEventListener(handler HandlerFunc) { |
| 98 | + b.Mutex.Lock() |
| 99 | + defer b.Mutex.Unlock() |
| 100 | + |
| 101 | + handlerType := reflect.TypeOf(handler) |
| 102 | + validateHandlerFunc(handlerType) |
| 103 | + // the first input parameter is the msg |
| 104 | + typOfMsg := handlerType.In(0) |
| 105 | + _, ok := b.listeners[typOfMsg.String()] |
| 106 | + if !ok { |
| 107 | + b.listeners[typOfMsg.String()] = make([]reflect.Value, 0) |
| 108 | + } |
| 109 | + b.listeners[typOfMsg.String()] = append(b.listeners[typOfMsg.String()], reflect.ValueOf(handler)) |
| 110 | +} |
| 111 | + |
| 112 | +// panic if conditions not met (this is a programming error) |
| 113 | +func validateHandlerFunc(handlerType reflect.Type) { |
| 114 | + switch { |
| 115 | + case handlerType.Kind() != reflect.Func: |
| 116 | + panic(BadFuncError("handler func must be a function")) |
| 117 | + case handlerType.NumIn() != 1: |
| 118 | + panic(BadFuncError("handler func must take exactly one input argument")) |
| 119 | + case handlerType.NumOut() != 1: |
| 120 | + panic(BadFuncError("handler func must take exactly one output argument")) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// BadFuncError is raised via panic() when AddEventListener is called with an |
| 125 | +// invalid listener function. |
| 126 | +type BadFuncError string |
| 127 | + |
| 128 | +func (bhf BadFuncError) Error() string { |
| 129 | + return fmt.Sprintf("bad handler func: %s", string(bhf)) |
| 130 | +} |
0 commit comments