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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
package ddp
// ----------------------------------------------------------------------
// Collection
// ----------------------------------------------------------------------
type Update map[string]interface{}
type UpdateListener interface {
CollectionUpdate(collection, operation, id string, doc Update)
}
// Collection managed cached collection data sent from the server in a
// livedata subscription.
//
// It would be great to build an entire mongo compatible local store (minimongo)
type Collection interface {
// FindOne queries objects and returns the first match.
FindOne(id string) Update
// FindAll returns a map of all items in the cache - this is a hack
// until we have time to build out a real minimongo interface.
FindAll() map[string]Update
// AddUpdateListener adds a channel that receives update messages.
AddUpdateListener(listener UpdateListener)
// livedata updates
added(msg Update)
changed(msg Update)
removed(msg Update)
addedBefore(msg Update)
movedBefore(msg Update)
init() // init informs the collection that the connection to the server has begun/resumed
reset() // reset informs the collection that the connection to the server has been lost
}
// NewMockCollection creates an empty collection that does nothing.
func NewMockCollection() Collection {
return &MockCache{}
}
// NewCollection creates a new collection - always KeyCache.
func NewCollection(name string) Collection {
return &KeyCache{name, make(map[string]Update), nil}
}
// KeyCache caches items keyed on unique ID.
type KeyCache struct {
// The name of the collection
Name string
// items contains collection items by ID
items map[string]Update
// listeners contains all the listeners that should be notified of collection updates.
listeners []UpdateListener
// TODO(badslug): do we need to protect from multiple threads
}
func (c *KeyCache) added(msg Update) {
id, fields := parseUpdate(msg)
if fields != nil {
c.items[id] = fields
c.notify("create", id, fields)
}
}
func (c *KeyCache) changed(msg Update) {
id, fields := parseUpdate(msg)
if fields != nil {
item, ok := c.items[id]
if ok {
for key, value := range fields {
item[key] = value
}
c.items[id] = item
c.notify("update", id, item)
}
}
}
func (c *KeyCache) removed(msg Update) {
id, _ := parseUpdate(msg)
if len(id) > 0 {
delete(c.items, id)
c.notify("remove", id, nil)
}
}
func (c *KeyCache) addedBefore(msg Update) {
// for keyed cache, ordered commands are a noop
}
func (c *KeyCache) movedBefore(msg Update) {
// for keyed cache, ordered commands are a noop
}
// init prepares the collection for data updates (called when a new connection is
// made or a connection/session is resumed).
func (c *KeyCache) init() {
// TODO start to patch up the current data with fresh server state
}
func (c *KeyCache) reset() {
// TODO we should mark the collection but maintain it's contents and then
// patch up the current contents with the new contents when we receive them.
//c.items = nil
c.notify("reset", "", nil)
}
// notify sends a Update to all UpdateListener's which should never block.
func (c *KeyCache) notify(operation, id string, doc Update) {
for _, listener := range c.listeners {
listener.CollectionUpdate(c.Name, operation, id, doc)
}
}
// FindOne returns the item with matching id.
func (c *KeyCache) FindOne(id string) Update {
return c.items[id]
}
// FindAll returns a dump of all items in the collection
func (c *KeyCache) FindAll() map[string]Update {
return c.items
}
// AddUpdateListener adds a listener for changes on a collection.
func (c *KeyCache) AddUpdateListener(listener UpdateListener) {
c.listeners = append(c.listeners, listener)
}
// OrderedCache caches items based on list order.
// This is a placeholder, currently not implemented as the Meteor server
// does not transmit ordered collections over DDP yet.
type OrderedCache struct {
// ranks contains ordered collection items for ordered collections
items []interface{}
}
func (c *OrderedCache) added(msg Update) {
// for ordered cache, key commands are a noop
}
func (c *OrderedCache) changed(msg Update) {
}
func (c *OrderedCache) removed(msg Update) {
}
func (c *OrderedCache) addedBefore(msg Update) {
}
func (c *OrderedCache) movedBefore(msg Update) {
}
func (c *OrderedCache) init() {
}
func (c *OrderedCache) reset() {
}
// FindOne returns the item with matching id.
func (c *OrderedCache) FindOne(id string) Update {
return nil
}
// FindAll returns a dump of all items in the collection
func (c *OrderedCache) FindAll() map[string]Update {
return map[string]Update{}
}
// AddUpdateListener does nothing.
func (c *OrderedCache) AddUpdateListener(ch UpdateListener) {
}
// MockCache implements the Collection interface but does nothing with the data.
type MockCache struct {
}
func (c *MockCache) added(msg Update) {
}
func (c *MockCache) changed(msg Update) {
}
func (c *MockCache) removed(msg Update) {
}
func (c *MockCache) addedBefore(msg Update) {
}
func (c *MockCache) movedBefore(msg Update) {
}
func (c *MockCache) init() {
}
func (c *MockCache) reset() {
}
// FindOne returns the item with matching id.
func (c *MockCache) FindOne(id string) Update {
return nil
}
// FindAll returns a dump of all items in the collection
func (c *MockCache) FindAll() map[string]Update {
return map[string]Update{}
}
// AddUpdateListener does nothing.
func (c *MockCache) AddUpdateListener(ch UpdateListener) {
}
// parseUpdate returns the ID and fields from a DDP Update document.
func parseUpdate(up Update) (ID string, Fields Update) {
key, ok := up["id"]
if ok {
switch id := key.(type) {
case string:
updates, ok := up["fields"]
if ok {
switch fields := updates.(type) {
case map[string]interface{}:
return id, Update(fields)
default:
// Don't know what to do...
}
}
return id, nil
}
}
return "", nil
}
|