git ssb

1+

Dominic / secure-scuttlebutt



Tree: 9a7e43ac498a13a9720df2ba9fdd94eafe23d04d

Files: 9a7e43ac498a13a9720df2ba9fdd94eafe23d04d / indexes / last.js

1128 bytesRaw
1var pull = require('pull-stream')
2var path = require('path')
3var ltgt = require('ltgt')
4var u = require('../util')
5var pCont = require('pull-cont')
6//var ViewLevel = require('flumeview-level')
7var Reduce = require('flumeview-reduce')
8function isNumber (n) {
9 return typeof n === 'number'
10}
11
12function toSeq (latest) {
13 return isNumber(latest) ? latest : latest.sequence
14}
15
16module.exports = function () {
17
18 //TODO: rewrite as a flumeview-reduce
19 var createIndex = Reduce(1, function (acc, data) {
20 if(!acc) acc = {}
21 acc[data.value.author] = {id: data.key, sequence: data.value.sequence, ts: data.value.timestamp}
22 return acc
23 })
24
25 return function (log, name) {
26 var index = createIndex(log, name)
27 index.methods.latest = 'source'
28
29 index.latest = function (opts) {
30 return pCont(function (cb) {
31 index.get([], function (err, val) {
32 if(err) return cb(err)
33 cb(null, pull.values(Object.keys(val).map(function (author) {
34 return {id: author, sequence: val[author].sequence, ts: val[author].ts}
35 })))
36 })
37 })
38 }
39
40 return index
41
42 }
43}
44
45
46
47
48
49
50

Built with git-ssb-web