Evgenii Akentev
·
2024-09-02
subscriptions.go
1package main
2
3type Subscription map[Address](map[*Client]struct{})
4
5type Subscriptions struct {
6 wallets Subscription
7 copyrights Subscription
8}
9
10func newSubscriptions() *Subscriptions {
11 return &Subscriptions{
12 wallets: make(map[Address](map[*Client]struct{})),
13 copyrights: make(map[Address](map[*Client]struct{})),
14 }
15}
16
17func (s *Subscriptions) removeClient(c *Client) {
18 for _, v := range s.wallets {
19 delete(v, c)
20 }
21}
22
23func (s *Subscriptions) getSub(at AddressType) Subscription {
24 switch at {
25 case Wallet:
26 return s.wallets
27 case Contract:
28 return s.copyrights
29 }
30 return nil
31}
32
33func (s *Subscriptions) subscribe(at AddressType, addr Address, c *Client) {
34 var m = s.getSub(at)
35 if subs, ok := m[addr]; ok {
36 subs[c] = struct{}{}
37 //TODO: not sure if it works, maybe need m[addr] ...
38 } else {
39 newsubs := make(map[*Client]struct{})
40 newsubs[c] = struct{}{}
41 m[addr] = newsubs
42 }
43}
44
45func (s *Subscriptions) unsubscribe(at AddressType, addr Address, c *Client) {
46 var m = s.getSub(at)
47 if subs, ok := m[addr]; ok {
48 // TODO: not sure as well
49 delete(subs, c)
50 }
51}