-
Notifications
You must be signed in to change notification settings - Fork 75
/
mesages-by-type.js
82 lines (70 loc) · 2.21 KB
/
mesages-by-type.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
var tape = require('tape')
var cont = require('cont')
var pull = require('pull-stream')
var ssbKeys = require('ssb-keys')
var createFeed = require('ssb-feed')
var createSSB = require('./util/create-ssb')
function all (stream, cb) {
pull(stream, pull.collect(cb))
}
function run (opts = {}) {
tape('messagesByType', function (t) {
var dbA = createSSB('msg-by-type1')
var alice = createFeed(dbA, ssbKeys.generate(), opts)
cont.series([
alice.add({ type: 'foo', foo: 1 }),
alice.add({ type: 'bar', bar: 2 }),
alice.add({ type: 'foo', foo: 3 }),
alice.add({ type: 'bar', bar: 4 }),
alice.add({ type: 'baz', baz: 5 })
])(function (err) {
if (err) throw err
all(dbA.messagesByType({ type: 'foo', keys: true }), function (err, ary) {
if (err) throw err
t.equal(ary.length, 2)
t.deepEqual(ary.map(function (e) {
return e.value.content
}), [
{ type: 'foo', foo: 1 },
{ type: 'foo', foo: 3 }
])
var since = ary[1].timestamp
alice.add({ type: 'foo', foo: 6 }, function (err) {
if (err) throw err
all(dbA.messagesByType({
type: 'foo',
gt: since
}), function (err, ary) {
if (err) throw err
t.equal(ary.length, 1)
t.equal(typeof ary[0].key, 'string')
t.deepEqual(ary[0].value.content, { type: 'foo', foo: 6 })
all(dbA.messagesByType({
type: 'foo',
gt: since,
keys: false
}), function (err, ary) {
if (err) throw err
t.equal(ary.length, 1)
t.deepEqual(ary[0].content, { type: 'foo', foo: 6 })
all(dbA.messagesByType({
type: 'foo',
gt: since,
values: false
}), function (err, ary) {
if (err) throw err
t.equal(ary.length, 1)
t.equal(typeof ary[0], 'string')
dbA.close(err => {
t.error(err, 'ssb.close - messagesByType')
t.end()
})
})
})
})
})
})
})
})
}
run()