-
Notifications
You must be signed in to change notification settings - Fork 52
/
events.go
153 lines (118 loc) · 2.5 KB
/
events.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package events
import (
"context"
"sync"
"github.com/golang-collections/go-datastructures/queue"
)
// Event Is a base interface for events
type Event interface{}
// EmitterInterface Root interface for events dispatch
type EmitterInterface interface {
// Emit Sends an event to the subscribed listeners
Emit(context.Context, Event)
// Subscribe Returns a channel that receives emitted events
Subscribe(context.Context) <-chan Event
// UnsubscribeAll close all listeners channels
UnsubscribeAll()
}
// EventEmitter Registers listeners and dispatches events to them
type EventEmitter struct {
subs []*eventSubscription
lock sync.Mutex
}
func (e *EventEmitter) UnsubscribeAll() {
for _, s := range e.allEventSubscription() {
s.cancel()
}
}
type eventSubscription struct {
ch chan Event
mu sync.Mutex
cancel context.CancelFunc
ctx context.Context
queue queue.Queue
closed bool
}
func (e *EventEmitter) allEventSubscription() []*eventSubscription {
e.lock.Lock()
defer e.lock.Unlock()
return append([]*eventSubscription(nil), e.subs...)
}
func (s *eventSubscription) queuedEmit(ctx context.Context) {
for {
items, err := s.queue.Get(1)
if err != nil {
return
}
if len(items) != 1 {
continue
}
evt, ok := items[0].(Event)
if !ok {
continue
}
s.mu.Lock()
if ctx.Err() != nil {
s.mu.Unlock()
return
}
select {
case <-ctx.Done():
s.mu.Unlock()
return
case s.ch <- evt:
}
s.mu.Unlock()
}
}
func (s *eventSubscription) emit(evt Event) {
if err := s.queue.Put(evt); err != nil {
s.close()
}
}
func (e *EventEmitter) Emit(ctx context.Context, evt Event) {
for _, sub := range e.allEventSubscription() {
sub.emit(evt)
}
}
func (s *eventSubscription) close() {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return
}
s.closed = true
s.cancel()
close(s.ch)
}
func (e *EventEmitter) removeSub(s *eventSubscription) {
e.lock.Lock()
defer e.lock.Unlock()
subs := e.subs
for i, can := range subs {
if can == s {
subs[i] = subs[len(subs)-1]
subs[len(subs)-1] = nil
e.subs = subs[:len(subs)-1]
break
}
}
}
func (e *EventEmitter) Subscribe(ctx context.Context) <-chan Event {
sub := &eventSubscription{
ch: make(chan Event),
}
sub.ctx, sub.cancel = context.WithCancel(ctx)
e.lock.Lock()
e.subs = append(e.subs, sub)
e.lock.Unlock()
go func() {
<-sub.ctx.Done()
e.removeSub(sub)
sub.queue.Dispose()
sub.close()
}()
go sub.queuedEmit(sub.ctx)
return sub.ch
}
var _ EmitterInterface = &EventEmitter{}