-
Notifications
You must be signed in to change notification settings - Fork 0
/
flume-wrap.js
72 lines (62 loc) · 1.79 KB
/
flume-wrap.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
var PullCont = require('pull-cont')
var pull = require('pull-stream')
module.exports = function wrap(sv, since, isReady) {
var waiting = []
var meta = {}
sv.since(function (upto) {
if(!isReady.value) return
while(waiting.length && waiting[0].seq <= upto)
waiting.shift().cb()
})
isReady(function (ready) {
if(!ready) return
var upto = sv.since.value
if(upto == undefined) return
while(waiting.length && waiting[0].seq <= upto)
waiting.shift().cb()
})
function ready (cb) {
if(isReady.value && since.value != null && since.value <= sv.since.value) cb()
else
since.once(function (upto) {
if(isReady.value && upto <= sv.since.value) cb()
else waiting.push({seq: upto, cb: cb})
})
}
var wrapper = {
source: function (fn, name) {
return function (opts) {
meta[name] ++
return pull(PullCont(function (cb) {
ready(function () { cb(null, fn(opts)) })
}), pull.through(function () { meta[name] ++ }))
}
},
async: function (fn, name) {
return function (opts, cb) {
meta[name] ++
ready(function () {
fn(opts, cb)
})
}
},
sync: function (fn, name) {
//return function (a, b) {
//meta[name] ++
return fn//(a, b)
//}
}
}
var o = {ready: ready, since: sv.since, close: sv.close, meta: meta}
if(!sv.methods) throw new Error('a stream view must have methods property')
for(var key in sv.methods) {
var type = sv.methods[key]
var fn = sv[key]
if(typeof fn !== 'function') throw new Error('expected function named:'+key+'of type: '+type)
//type must be either source, async, or sync
meta[key] = 0
o[key] = wrapper[type](fn, key)
}
o.methods = sv.methods
return o
}