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