| 1 | package partybus |
| 2 | |
| 3 | import "sync" |
| 4 | |
| 5 | type EventType string |
| 6 | |
| 7 | type Event struct { |
| 8 | Type EventType |
| 9 | Source interface{} |
| 10 | Value interface{} |
| 11 | Error error |
| 12 | } |
| 13 | |
| 14 | func Join(cs ...<-chan Event) <-chan Event { |
| 15 | var wg sync.WaitGroup |
| 16 | trunk := make(chan Event) |
| 17 | |
| 18 | read := func(c <-chan Event) { |
| 19 | for n := range c { |
| 20 | trunk <- n |
| 21 | } |
| 22 | wg.Done() |
| 23 | } |
| 24 | wg.Add(len(cs)) |
| 25 | for _, c := range cs { |
| 26 | go read(c) |
| 27 | } |
| 28 | |
| 29 | go func() { |
| 30 | wg.Wait() |
| 31 | close(trunk) |
| 32 | }() |
| 33 | return trunk |
| 34 | } |