-
Notifications
You must be signed in to change notification settings - Fork 13
/
publisher.go
executable file
·75 lines (64 loc) · 1.5 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package event
import (
"sync"
// Frameworks
"github.com/djthorpe/gopi"
)
type Publisher struct {
sync.Mutex
channels []chan gopi.Event
}
// Subscribe returns a new channel on which emitting events can occur
func (this *Publisher) Subscribe() <-chan gopi.Event {
this.Lock()
defer this.Unlock()
// Create channels with a capacity of one
if this.channels == nil {
this.channels = make([]chan gopi.Event, 0, 1)
}
// Return a new channel
channel := make(chan gopi.Event)
this.channels = append(this.channels, channel)
return channel
}
// Unsubscribe closes a channel and removes it from the list
// of channels which emitting can happen on
func (this *Publisher) Unsubscribe(subscriber <-chan gopi.Event) {
this.Lock()
defer this.Unlock()
if this.channels != nil {
for i := range this.channels {
if this.channels[i] == subscriber {
close(this.channels[i])
this.channels[i] = nil
}
}
}
}
// Close will unsubscribe all remaining channels
func (this *Publisher) Close() {
this.Lock()
defer this.Unlock()
if this.channels != nil {
for _, subscriber := range this.channels {
if subscriber != nil {
close(subscriber)
}
}
this.channels = nil
}
}
// Emit an event onto all subscriber channels, this method
// will block if the subscribers are not processing incoming
// events
func (this *Publisher) Emit(evt gopi.Event) {
this.Lock()
defer this.Unlock()
if this.channels != nil {
for _, channel := range this.channels {
if channel != nil {
channel <- evt
}
}
}
}