This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobs.js
245 lines (218 loc) · 5.94 KB
/
obs.js
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
var { Value, Set, Struct, computed } = require('mutant')
var pull = require('pull-stream')
var ref = require('ssb-ref')
var set = require('lodash/set')
var unset = require('lodash/unset')
var get = require('lodash/get')
var isEmpty = require('lodash/isEmpty')
var tagsCache = {}
var messagesCache = {}
var cacheLoading = false
var sync = Value(false)
function Tag (server) {
return (tagId, nameFn) => {
let tagName
if (nameFn) {
// Use nameFn is supplied
tagName = nameFn(tagId)
} else if (server.names) {
// Fallback to ssb-names plugin if available
tagName = Value()
server.names.getSignifier(tagId, function (signifier) {
tagName.set(signifier)
})
} else {
// Otherwise use short id
tagName = Value(tagId.slice(1, 10))
}
return Struct({
tagId,
tagName
})
}
}
function messageTags (server) {
return (msgId) => {
if (!ref.isLink(msgId)) throw new Error('Requires an ssb ref!')
return withSync(computed(getObs(msgId, server, messagesCache), getMessageTags))
}
}
function messageTagsFrom (server) {
return (msgId, author) => {
if (!ref.isLink(msgId) || !ref.isFeedId(author)) throw new Error('Requires an ssb ref!')
return withSync(computed([getObs(msgId, server, messagesCache), author], getMessageTagsFrom))
}
}
function messageTaggers (server) {
return (msgId, tagId) => {
if (!ref.isLink(msgId) || !ref.isLink(tagId)) throw new Error('Requires an ssb ref!')
return withSync(computed([getObs(msgId, server, messagesCache), tagId], getMessageTaggers))
}
}
function allTags (server) {
return () => withSync(getAllTags(getCache(server, tagsCache)))
}
function allTagsFrom (server) {
return (author) => {
if (!ref.isFeedId(author)) throw new Error('Requires an ssb ref!')
return withSync(computed(getObs(author, server, tagsCache), getAllTagsFrom))
}
}
function messagesTaggedByWith (server) {
return (author, tagId) => {
if (!ref.isFeedId(author) || !ref.isLink(tagId)) throw new Error('Requires an ssb ref!')
return withSync(computed([getObs(author, server, tagsCache), tagId], getTaggedMessages))
}
}
function withSync (obs) {
obs.sync = sync
return obs
}
function getObs (id, server, lookup) {
if (!ref.isLink(id)) throw new Error('Requires an ssb ref!')
if (!cacheLoading) {
cacheLoading = true
loadCache(server)
}
if (!lookup[id]) {
lookup[id] = Value({})
}
return lookup[id]
}
function getCache (server, lookup) {
if (!cacheLoading) {
cacheLoading = true
loadCache(server)
}
return lookup
}
function update (id, values, server, lookup) {
const state = getObs(id, server, lookup)
const lastState = state()
var changed = false
for (const tag in values) {
const lastTag = lastState[tag]
const isUnusedTag = isEmpty(values[tag]) && (lastTag === undefined || !isEmpty(lastTag))
if (isUnusedTag) {
set(lastState, [ tag ], {})
changed = true
continue
}
for (const key in values[tag]) {
const value = get(values, [ tag, key ])
const lastValue = get(lastState, [ tag, key ])
if (value !== lastValue) {
if (value) {
set(lastState, [ tag, key ], value)
} else {
unset(lastState, [ tag, key ])
}
changed = true
}
}
}
if (changed) {
state.set(lastState)
}
}
// TODO: rewrite with a flume index to improve performance
function loadCache (server) {
pull(
server.tags.stream({ live: true }),
pull.drain(item => {
if (!sync()) {
// populate tags observable cache
const messageLookup = {}
for (const author in item) {
update(author, item[author], server, tagsCache)
// generate message lookup
for (const tag in item[author]) {
for (const message in item[author][tag]) {
set(messageLookup, [message, tag, author], item[author][tag][message])
}
}
}
// populate messages observable cache
for (const message in messageLookup) {
update(message, messageLookup[message], server, messagesCache)
}
if (!sync()) {
sync.set(true)
}
} else if (item && ref.isLink(item.tagKey) && ref.isFeedId(item.author) && ref.isLink(item.message)) {
// handle realtime updates
const { tagKey, author, message, tagged, timestamp } = item
if (tagged) {
update(author, { [tagKey]: { [message]: timestamp } }, server, tagsCache)
update(message, { [tagKey]: { [author]: timestamp } }, server, messagesCache)
} else {
update(author, { [tagKey]: { [message]: false } }, server, tagsCache)
update(message, { [tagKey]: { [author]: false } }, server, messagesCache)
}
}
})
)
}
function getTaggedMessages (lookup, key) {
const messages = []
for (const msg in lookup[key]) {
if (lookup[key][msg]) {
messages.push(msg)
}
}
return messages
}
function getMessageTags (lookup) {
const tags = []
for (const tag in lookup) {
if (!isEmpty(lookup[tag])) {
tags.push(tag)
}
}
return tags
}
function getMessageTagsFrom (lookup, author) {
const tags = []
for (const tag in lookup) {
if (lookup[tag][author]) {
tags.push(tag)
}
}
return tags
}
function getMessageTaggers (lookup, key) {
const taggers = []
for (const author in lookup[key]) {
if (lookup[key][author]) {
taggers.push(author)
}
}
return taggers
}
function getAllTags (lookup) {
const tags = Set([])
for (const author in lookup) {
const authorTags = lookup[author]()
for (const tag in authorTags) {
tags.add(tag)
}
}
return tags
}
function getAllTagsFrom (lookup) {
const tags = []
for (const tag in lookup) {
if (isEmpty(lookup[tag])) continue
tags.push(tag)
}
return tags
}
module.exports = {
Tag,
messageTags,
messageTagsFrom,
messageTaggers,
allTags,
allTagsFrom,
messagesTaggedByWith
}