Pārlūkot izejas kodu

hide implementation details

tags/v1.0.0
Achille Roussel pirms 2 gadiem
vecāks
revīzija
df3a77fd57
2 mainītis faili ar 17 papildinājumiem un 8 dzēšanām
  1. 15
    6
      handler.go
  2. 2
    2
      handler_test.go

+ 15
- 6
handler.go Parādīt failu

@@ -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 Parādīt failu

@@ -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{})


Notiek ielāde…
Atcelt
Saglabāt