~whereswaldon/sprout-go

ref: relay sprout-go/cmd/relay/message_store.go -rw-r--r-- 3.2 KiB 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
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
120
121
122
123
124
125
126
127
128
129
130
package main

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

type MessageStore struct {
	store             forest.Store
	requests          chan func()
	nextSubscriberKey int
	subscribers       map[int]func(forest.Node)
}

// NewMessageStore creates a thread-safe storage structure for
// forest nodes.
func NewMessageStore() *MessageStore {
	m := &MessageStore{
		store:       forest.NewMemoryStore(),
		requests:    make(chan func()),
		subscribers: make(map[int]func(forest.Node)),
	}
	go func() {
		for function := range m.requests {
			function()
		}
	}()
	return m
}

func (m *MessageStore) SubscribeToNewMessages(handler func(n forest.Node)) (subscriptionID int) {
	m.requests <- func() {
		subscriptionID = m.nextSubscriberKey
		m.nextSubscriberKey++
		m.subscribers[subscriptionID] = handler
	}
	return
}

func (m *MessageStore) UnsubscribeToNewMessages(subscriptionID int) {
	m.requests <- func() {
		if _, subscribed := m.subscribers[subscriptionID]; subscribed {
			delete(m.subscribers, subscriptionID)
		}
	}
	return
}

func (m *MessageStore) Size() (size int, err error) {
	m.requests <- func() {
		size, err = m.store.Size()
	}
	return
}

func (m *MessageStore) CopyInto(s forest.Store) (err error) {
	m.requests <- func() {
		err = m.store.CopyInto(s)
	}
	return
}

func (m *MessageStore) Get(id *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	m.requests <- func() {
		node, present, err = m.store.Get(id)
	}
	return
}

func (m *MessageStore) GetIdentity(id *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	m.requests <- func() {
		node, present, err = m.store.GetIdentity(id)
	}
	return
}

func (m *MessageStore) GetCommunity(id *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	m.requests <- func() {
		node, present, err = m.store.GetCommunity(id)
	}
	return
}

func (m *MessageStore) GetConversation(communityID, conversationID *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	m.requests <- func() {
		node, present, err = m.store.GetConversation(communityID, conversationID)
	}
	return
}

func (m *MessageStore) GetReply(communityID, conversationID, replyID *fields.QualifiedHash) (node forest.Node, present bool, err error) {
	m.requests <- func() {
		node, present, err = m.store.GetReply(communityID, conversationID, replyID)
	}
	return
}

func (m *MessageStore) Children(parent *fields.QualifiedHash) (children []*fields.QualifiedHash, err error) {
	m.requests <- func() {
		children, err = m.store.Children(parent)
	}
	return
}

func (m *MessageStore) Recent(nodeType fields.NodeType, quantity int) (recentNodes []forest.Node, err error) {
	m.requests <- func() {
		recentNodes, err = m.store.Recent(nodeType, quantity)
	}
	return
}

func (m *MessageStore) Add(node forest.Node, addedByID int) (err error) {
	m.requests <- func() {
		err = m.store.Add(node)
		if err == nil {
			for subscriptionID, handler := range m.subscribers {
				if subscriptionID != addedByID {
					handler(node)
				}
			}
		}
	}
	return
}

// Shut down the worker gorountine that powers this store. Subsequent
// calls to methods on this MessageStore have undefined behavior
func (m *MessageStore) Destroy() {
	close(m.requests)
}