-
Notifications
You must be signed in to change notification settings - Fork 1
/
notificationsubscription.go
88 lines (71 loc) · 1.99 KB
/
notificationsubscription.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
// Copyright 2018 DataArt. All rights reserved.
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package devicehive_go
import (
"encoding/json"
"github.com/devicehive/devicehive-go/internal/transport"
"sync"
)
var notifSubsMutex sync.RWMutex
var notificationSubscriptions = make(map[*NotificationSubscription]string)
type NotificationSubscription struct {
NotificationChan chan *Notification
ErrorChan chan *Error
done chan struct{}
client *Client
}
// Sends request to unsubscribe from current notifications subscription
func (ns *NotificationSubscription) Remove() *Error {
notifSubsMutex.RLock()
subsId := notificationSubscriptions[ns]
notifSubsMutex.RUnlock()
err := ns.client.unsubscribe("notification/unsubscribe", subsId)
notifSubsMutex.Lock()
close(ns.done)
delete(notificationSubscriptions, ns)
notifSubsMutex.Unlock()
return err
}
func (ns *NotificationSubscription) sendError(err *Error) {
ns.ErrorChan <- err
}
func newNotificationSubscription(subsId string, tspSubs *transport.Subscription, client *Client) *NotificationSubscription {
subs := &NotificationSubscription{
NotificationChan: make(chan *Notification),
ErrorChan: make(chan *Error),
done: make(chan struct{}),
client: client,
}
notifSubsMutex.Lock()
notificationSubscriptions[subs] = subsId
notifSubsMutex.Unlock()
go func() {
loop:
for {
select {
case rawNotif, ok := <-tspSubs.DataChan:
if !ok {
break loop
}
notif := client.NewNotification()
err := json.Unmarshal(rawNotif, notif)
if err != nil {
subs.ErrorChan <- &Error{name: InvalidSubscriptionEventData, reason: err.Error()}
continue
}
subs.NotificationChan <- notif
case err, ok := <-tspSubs.ErrChan:
if !ok {
break loop
}
subs.sendError(newError(err))
case <-subs.done:
break loop
}
}
close(subs.NotificationChan)
close(subs.ErrorChan)
}()
return subs
}