git ssb

7+

dinoworm 🐛 / patchcore



Commit a3f728451ea4f496b80c42138d030295e43de153

add blob.obs.has

Matt McKegg committed on 6/12/2017, 5:00:12 AM
Parent: 023b5287cd3877535d9f446f55f213582430a9d6

Files changed

blob/obs/has.jsadded
blob/obs/has.jsView
@@ -1,0 +1,57 @@
1 +var nest = require('depnest')
2 +var pull = require('pull-stream')
3 +var Value = require('mutant/value')
4 +var onceTrue = require('mutant/once-true')
5 +var defer = require('pull-defer')
6 +
7 +exports.needs = nest({
8 + 'sbot.obs.connection': 'first'
9 +})
10 +
11 +exports.gives = nest('blob.obs.has', true)
12 +
13 +exports.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 +
51 +function StreamWhenConnected (connection, fn) {
52 + var stream = defer.source()
53 + onceTrue(connection, function (connection) {
54 + stream.resolve(fn(connection))
55 + })
56 + return stream
57 +}

Built with git-ssb-web