-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.go
76 lines (62 loc) · 1.49 KB
/
subscription.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
package pubsub
// Subscription represents a Handler subscribed to a topic.
type Subscription interface {
// ID uniquely identifies the subscription. UUID algorithm, or similar, is
// recommended.
ID() string
// Options returns the options used to create the subscription.
Options() SubscribeOptions
// Topic returns the topic the subscription is subscribed to.
Topic() Topic
// Unsubscribe unsubscribes.
Unsubscribe() error
// Handler returns the underlying Handler function this subscription will
// use when receiving messages.
Handler() Handler
}
// UnsubscribeFunc represents a function responsible for unsubscribing a
// subscription.
type UnsubscribeFunc func() error
type subscription struct {
id string
topic Topic
options SubscribeOptions
unsub func() error
handler Handler
}
// NewSubscription convenience function for creating a new subscriptions.
func NewSubscription(
id string,
topic Topic,
handler Handler,
unsub UnsubscribeFunc,
options SubscribeOptions,
) Subscription {
if unsub == nil {
unsub = func() error {
return nil
}
}
return &subscription{
id: id,
topic: topic,
options: options,
unsub: unsub,
handler: handler,
}
}
func (s *subscription) ID() string {
return s.id
}
func (s *subscription) Options() SubscribeOptions {
return s.options
}
func (s *subscription) Topic() Topic {
return s.topic
}
func (s *subscription) Unsubscribe() error {
return s.unsub()
}
func (s *subscription) Handler() Handler {
return s.handler
}