-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel-queue.go
49 lines (39 loc) · 868 Bytes
/
channel-queue.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
package event
import (
"context"
"reflect"
"github.com/abibby/salusa/di"
)
type ChannelQueueConfig struct{}
var _ (Config) = (*ChannelQueueConfig)(nil)
func NewChannelQueueConfig() *ChannelQueueConfig {
return &ChannelQueueConfig{}
}
func (c *ChannelQueueConfig) Queue() Queue {
return NewChannelQueue()
}
type ChannelQueue struct {
channel chan []byte
}
func NewChannelQueue() *ChannelQueue {
return &ChannelQueue{
channel: make(chan []byte, 10),
}
}
func (q ChannelQueue) Push(e Event) error {
b, err := encodeEvent(e)
if err != nil {
return err
}
q.channel <- b
return nil
}
func (q ChannelQueue) Pop(events map[EventType]reflect.Type) (Event, error) {
return decodeEvent(<-q.channel, events)
}
func RegisterChannelQueue(ctx context.Context) error {
di.RegisterSingleton(ctx, func() Queue {
return NewChannelQueue()
})
return nil
}