git ssb

0+

Dominic / ssb-feed



Tree: 7f5b9989dbc45ee625499416f98f9ce28de20509

Files: 7f5b9989dbc45ee625499416f98f9ce28de20509 / test / mock.js

1822 bytesRaw
1
2
3var Validator = require('../validator')
4var Pushable = require('pull-pushable')
5function compare (a, b) {
6 return a.value.timestamp - b.value.timestamp
7// return a.key < b.key ? 1 : a.key > b.key ? -1 : 0
8}
9
10var pull = require('pull-stream')
11
12module.exports = function (async, opts) {
13
14 var data = [], validator, live
15
16 var ssbMock = {
17 data: data,
18
19 getLatest: async(function (id, cb) {
20 var last, max = 0
21 data.forEach(function (data) {
22 if(data.value.author === id && data.value.sequence >= max) {
23 last = data; max = last.value.sequence
24 }
25 })
26 cb (null, last || {key:null,value:null})
27 }, 'add'),
28
29 batch: async(function (batch, cb) {
30 console.log('BATCH', JSON.stringify(batch))
31 batch.forEach(function (d) {
32 if(!d.key && d.value) throw new Error('invalid batch')
33 if(live) live.push(d)
34 data.push(d)
35 })
36 data.sort(compare)
37 cb()
38 }, 'batch'),
39
40 get: async(function (id, cb) {
41 for(var i = 0; i < data.length; i++)
42 if(id === data[i].key)
43 return cb(null, data[i].value)
44 cb(new Error('value not found'))
45
46 }, 'get'),
47
48 //this is also needed for the tests.
49 createFeedStream: function (opts) {
50 opts = opts || {}
51 if(opts.live) {
52 live = Pushable()
53 data.forEach(function (v) { live.push(v) })
54 }
55 return pull(
56 opts.live ? live : pull.values(data),
57 pull.map(function (data) {
58 if(opts.keys === false) return data.value
59 return data
60 }),
61 pull.through(function (D) {
62 console.log('READ', D)
63 }, function (err) {
64 console.log("ERR", err)
65 }),
66 async.through('feed')
67 )
68 }
69 }
70 ssbMock.add = Validator(ssbMock, opts)
71
72 return ssbMock
73}
74
75
76

Built with git-ssb-web