-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
subscription_events.go
119 lines (96 loc) · 3.41 KB
/
subscription_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
package centrifuge
// SubscribedEvent is an event context passed
// to subscribe success callback.
type SubscribedEvent struct {
Positioned bool
Recoverable bool
StreamPosition *StreamPosition
WasRecovering bool
Recovered bool
Data []byte
}
// SubscriptionErrorEvent is a subscribe error event context passed to
// event callback.
type SubscriptionErrorEvent struct {
Error error
}
// SubscribingEvent is an event passed to subscribing event handler.
type SubscribingEvent struct {
Code uint32
Reason string
}
// UnsubscribedEvent is an event passed to unsubscribe event handler.
type UnsubscribedEvent struct {
Code uint32
Reason string
}
// LeaveEvent has info about user who left channel.
type LeaveEvent struct {
ClientInfo
}
// JoinEvent has info about user who joined channel.
type JoinEvent struct {
ClientInfo
}
// PublicationEvent has info about received channel Publication.
type PublicationEvent struct {
Publication
}
// PublicationHandler is a function to handle messages published in
// channels.
type PublicationHandler func(PublicationEvent)
// JoinHandler is a function to handle join messages.
type JoinHandler func(JoinEvent)
// LeaveHandler is a function to handle leave messages.
type LeaveHandler func(LeaveEvent)
// UnsubscribedHandler is a function to handle unsubscribe event.
type UnsubscribedHandler func(UnsubscribedEvent)
// SubscribingHandler is a function to handle subscribe success event.
type SubscribingHandler func(SubscribingEvent)
// SubscribedHandler is a function to handle subscribe success event.
type SubscribedHandler func(SubscribedEvent)
// SubscriptionErrorHandler is a function to handle subscribe error event.
type SubscriptionErrorHandler func(SubscriptionErrorEvent)
// subscriptionEventHub contains callback functions that will be called when
// corresponding event happens with subscription to channel.
type subscriptionEventHub struct {
onSubscribing SubscribingHandler
onSubscribed SubscribedHandler
onUnsubscribe UnsubscribedHandler
onError SubscriptionErrorHandler
onPublication PublicationHandler
onJoin JoinHandler
onLeave LeaveHandler
}
// newSubscriptionEventHub initializes new subscriptionEventHub.
func newSubscriptionEventHub() *subscriptionEventHub {
return &subscriptionEventHub{}
}
// OnSubscribing allows setting SubscribingHandler to SubEventHandler.
func (s *Subscription) OnSubscribing(handler SubscribingHandler) {
s.events.onSubscribing = handler
}
// OnSubscribed allows setting SubscribedHandler to SubEventHandler.
func (s *Subscription) OnSubscribed(handler SubscribedHandler) {
s.events.onSubscribed = handler
}
// OnUnsubscribed allows setting UnsubscribedHandler to SubEventHandler.
func (s *Subscription) OnUnsubscribed(handler UnsubscribedHandler) {
s.events.onUnsubscribe = handler
}
// OnError allows setting SubscriptionErrorHandler to SubEventHandler.
func (s *Subscription) OnError(handler SubscriptionErrorHandler) {
s.events.onError = handler
}
// OnPublication allows setting PublicationHandler to SubEventHandler.
func (s *Subscription) OnPublication(handler PublicationHandler) {
s.events.onPublication = handler
}
// OnJoin allows setting JoinHandler to SubEventHandler.
func (s *Subscription) OnJoin(handler JoinHandler) {
s.events.onJoin = handler
}
// OnLeave allows setting LeaveHandler to SubEventHandler.
func (s *Subscription) OnLeave(handler LeaveHandler) {
s.events.onLeave = handler
}