Browse Source

hide implementation details

tags/v1.0.0
Achille Roussel 2 years ago
parent
commit
df3a77fd57
2 changed files with 17 additions and 8 deletions
  1. 15
    6
      handler.go
  2. 2
    2
      handler_test.go

+ 15
- 6
handler.go View File

@@ -21,13 +21,22 @@ func (f HandlerFunc) HandleEvent(e *Event) {
f(e)
}

// MultiHandler is an event handler that fans out events it receives to a list
// of handlers.
type MultiHandler []Handler
// MultiHandler returns a new Handler which broadcasts the events it receives
// to its list of handlers.
func MultiHandler(handlers ...Handler) Handler {
c := make([]Handler, len(handlers))
copy(c, handlers)
return &multiHandler{
handlers: c,
}
}

type multiHandler struct {
handlers []Handler
}

// HandleEvent broadcasts e to all handlers of m.
func (m MultiHandler) HandleEvent(e *Event) {
for _, h := range m {
func (m *multiHandler) HandleEvent(e *Event) {
for _, h := range m.handlers {
h.HandleEvent(e)
}
}

+ 2
- 2
handler_test.go View File

@@ -4,10 +4,10 @@ import "testing"

func TestMultiHandler(t *testing.T) {
n := 0
m := MultiHandler{
m := MultiHandler(
HandlerFunc(func(e *Event) { n++ }),
HandlerFunc(func(e *Event) { n++ }),
}
)

m.HandleEvent(&Event{})


Loading…
Cancel
Save