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) {
21 21
 	f(e)
22 22
 }
23 23
 
24
-// MultiHandler is an event handler that fans out events it receives to a list
25
-// of handlers.
26
-type MultiHandler []Handler
24
+// MultiHandler returns a new Handler which broadcasts the events it receives
25
+// to its list of handlers.
26
+func MultiHandler(handlers ...Handler) Handler {
27
+	c := make([]Handler, len(handlers))
28
+	copy(c, handlers)
29
+	return &multiHandler{
30
+		handlers: c,
31
+	}
32
+}
33
+
34
+type multiHandler struct {
35
+	handlers []Handler
36
+}
27 37
 
28
-// HandleEvent broadcasts e to all handlers of m.
29
-func (m MultiHandler) HandleEvent(e *Event) {
30
-	for _, h := range m {
38
+func (m *multiHandler) HandleEvent(e *Event) {
39
+	for _, h := range m.handlers {
31 40
 		h.HandleEvent(e)
32 41
 	}
33 42
 }

+ 2
- 2
handler_test.go View File

@@ -4,10 +4,10 @@ import "testing"
4 4
 
5 5
 func TestMultiHandler(t *testing.T) {
6 6
 	n := 0
7
-	m := MultiHandler{
7
+	m := MultiHandler(
8 8
 		HandlerFunc(func(e *Event) { n++ }),
9 9
 		HandlerFunc(func(e *Event) { n++ }),
10
-	}
10
+	)
11 11
 
12 12
 	m.HandleEvent(&Event{})
13 13
 

Loading…
Cancel
Save