git ssb

7+

dinoworm ๐Ÿ› / patchcore



Tree: 8949e8dbfc5ddb4b87f2c9508980de7c0f848e7b

Files: 8949e8dbfc5ddb4b87f2c9508980de7c0f848e7b / blob / obs / has.js

1287 bytesRaw
1var nest = require('depnest')
2var pull = require('pull-stream')
3var Value = require('mutant/value')
4var onceTrue = require('mutant/once-true')
5var defer = require('pull-defer')
6
7exports.needs = nest({
8 'sbot.obs.connection': 'first'
9})
10
11exports.gives = nest('blob.obs.has', true)
12
13exports.create = function (api) {
14 var cbs = null
15
16 return nest('blob.obs.has', function (id) {
17 var value = Value()
18 onceTrue(api.sbot.obs.connection, sbot => {
19 sbot.blobs.has(id, (_, has) => {
20 if (has) {
21 value.set(true)
22 } else {
23 value.set(false)
24 waitFor(id, () => value.set(true))
25 }
26 })
27 })
28 return value
29 })
30
31 function waitFor (id, cb) {
32 if (!cbs) {
33 cbs = {}
34 pull(
35 StreamWhenConnected(api.sbot.obs.connection, sbot => sbot.blobs.ls({old: false})),
36 pull.drain(blob => {
37 if (cbs[blob]) {
38 while (cbs[blob].length) {
39 cbs[blob].pop()()
40 }
41 delete cbs[blob]
42 }
43 })
44 )
45 }
46 if (!cbs[id]) cbs[id] = []
47 cbs[id].push(cb)
48 }
49}
50
51function StreamWhenConnected (connection, fn) {
52 var stream = defer.source()
53 onceTrue(connection, function (connection) {
54 stream.resolve(fn(connection))
55 })
56 return stream
57}
58

Built with git-ssb-web