-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheventsqueue.go
More file actions
45 lines (37 loc) · 781 Bytes
/
eventsqueue.go
File metadata and controls
45 lines (37 loc) · 781 Bytes
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
package wrapper
import (
"sync"
"github.com/wlwanpan/minecraft-wrapper/events"
)
type eventsQueue struct {
mu sync.RWMutex
q map[string]chan events.GameEvent
}
func newEventsQueue() *eventsQueue {
return &eventsQueue{
q: make(map[string]chan events.GameEvent),
}
}
func (eq *eventsQueue) get(e string) <-chan events.GameEvent {
eq.mu.Lock()
defer eq.mu.Unlock()
_, ok := eq.q[e]
if !ok {
eq.q[e] = make(chan events.GameEvent)
}
return eq.q[e]
}
func (eq *eventsQueue) push(ev events.GameEvent) {
eq.mu.RLock()
defer eq.mu.RUnlock()
c, ok := eq.q[ev.String()]
if !ok {
// No channel is registered, means no cmd awaits a response from this events queue.
// we can hence discard/ignore the event...
return
}
select {
case c <- ev:
default:
}
}