git ssb

2+

Dominic / flumedb



Tree: 9a1552566c99ce0139006a43936bf9fd109afa1c

Files: 9a1552566c99ce0139006a43936bf9fd109afa1c / wrap.js

1885 bytesRaw
1var PullCont = require('pull-cont')
2var pull = require('pull-stream')
3
4module.exports = function wrap(sv, since, isReady) {
5 var waiting = []
6
7 var meta = {}
8
9 sv.since(function (upto) {
10 if(!isReady.value) return
11 while(waiting.length && waiting[0].seq <= upto)
12 waiting.shift().cb()
13 })
14
15 isReady(function (ready) {
16 if(!ready) return
17 var upto = sv.since.value
18 if(upto == undefined) return
19 while(waiting.length && waiting[0].seq <= upto)
20 waiting.shift().cb()
21 })
22
23 function ready (cb) {
24 if(isReady.value && since.value != null && since.value === sv.since.value) cb()
25 else
26 since.once(function (upto) {
27 if(isReady.value && upto === sv.since.value) cb()
28 else waiting.push({seq: upto, cb: cb})
29 })
30 }
31
32 var wrapper = {
33 source: function (fn, name) {
34 return function (opts) {
35 console.log(name, JSON.stringify(opts))
36 meta[name] ++
37 return pull(PullCont(function (cb) {
38 ready(function () { cb(null, fn(opts)) })
39 }), pull.through(function () { meta[name] ++ }))
40 }
41 },
42 async: function (fn, name) {
43 return function (opts, cb) {
44 meta[name] ++
45 ready(function () {
46 fn(opts, cb)
47 })
48 }
49 },
50 sync: function (fn, name) {
51 //return function (a, b) {
52 //meta[name] ++
53 return fn//(a, b)
54 //}
55 }
56 }
57
58 var o = {ready: ready, since: sv.since, close: sv.close, meta: meta}
59 if(!sv.methods) throw new Error('a stream view must have methods property')
60
61 for(var key in sv.methods) {
62 var type = sv.methods[key]
63 var fn = sv[key]
64 if(typeof fn !== 'function') throw new Error('expected function named:'+key+'of type: '+type)
65 //type must be either source, async, or sync
66 meta[key] = 0
67 o[key] = wrapper[type](fn, key)
68 }
69
70 o.methods = sv.methods
71
72 return o
73}
74
75
76
77
78
79
80
81

Built with git-ssb-web