~whereswaldon/sprout-go

ref: relay sprout-go/cmd/relay/session.go -rw-r--r-- 862 bytes View raw
82082f28Chris Waldon Implement list verb in example relay 8 months ago
                                                                                
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 main

import (
	"sync"

	"git.sr.ht/~whereswaldon/forest-go/fields"
)

type Session struct {
	sync.RWMutex
	Communities map[*fields.QualifiedHash]struct{}
}

func NewSession() *Session {
	return &Session{
		Communities: make(map[*fields.QualifiedHash]struct{}),
	}
}

func (c *Session) Subscribe(communityID *fields.QualifiedHash) {
	c.Lock()
	defer c.Unlock()

	c.Communities[communityID] = struct{}{}
}

func (c *Session) IsSubscribed(communityID *fields.QualifiedHash) bool {
	c.RLock()
	defer c.RUnlock()

	for community := range c.Communities {
		if community.Equals(communityID) {
			return true
		}
	}
	return false
}

func (c *Session) Unsubscribe(communityID *fields.QualifiedHash) {
	c.Lock()
	defer c.Unlock()

	for community := range c.Communities {
		if community.Equals(communityID) {
			delete(c.Communities, community)
			return
		}
	}
}