Commit 5e55c4b6b3322e83b1d154ca6044fcc656c1bb2c
automatically reconnect sbot api
Dominic Tarr committed on 6/23/2016, 12:34:04 PMParent: df5168eeea500dbe048c18ffc665591d65df85f9
Files changed
sbot-api.js | changed |
sbot-api.js | ||
---|---|---|
@@ -1,6 +1,7 @@ | ||
1 | 1 | var pull = require('pull-stream') |
2 | 2 | var crypto = require('crypto') |
3 | +var Reconnect = require('pull-reconnect') | |
3 | 4 | |
4 | 5 | function Hash (onHash) { |
5 | 6 | var hash = crypto.createHash('sha256') |
6 | 7 | return pull.through(function (data) { |
@@ -15,38 +16,51 @@ | ||
15 | 16 | }) |
16 | 17 | } |
17 | 18 | |
18 | 19 | |
19 | -module.exports = function (sbot) { | |
20 | +var createClient = require('ssb-client') | |
20 | 21 | |
22 | +module.exports = function () { | |
23 | + var sbot = null | |
24 | + var rec = Reconnect(function (isConn) { | |
25 | + createClient(function (err, _sbot) { | |
26 | + if(err) return isConn(err) | |
27 | + sbot = _sbot | |
28 | + sbot.on('closed', function () { | |
29 | + isConn(new Error('closed')) | |
30 | + }) | |
31 | + isConn() | |
32 | + }) | |
33 | + }) | |
34 | + | |
21 | 35 | return { |
22 | - sbot_blobs_add: function (cb) { | |
36 | + sbot_blobs_add: rec.sink(function (cb) { | |
23 | 37 | return pull( |
24 | 38 | Hash(cb), |
25 | 39 | sbot.blobs.add() |
26 | 40 | ) |
27 | - }, | |
28 | - sbot_links: function (query) { | |
41 | + }), | |
42 | + sbot_links: rec.source(function (query) { | |
29 | 43 | return sbot.links(query) |
30 | - }, | |
31 | - sbot_links2: function (query) { | |
44 | + }), | |
45 | + sbot_links2: rec.source(function (query) { | |
32 | 46 | return sbot.links2.read(query) |
33 | - }, | |
34 | - sbot_log: function (opts) { | |
47 | + }), | |
48 | + sbot_log: rec.source(function (opts) { | |
35 | 49 | return sbot.createLogStream(opts) |
36 | - }, | |
37 | - sbot_user_feed: function (opts) { | |
50 | + }), | |
51 | + sbot_user_feed: rec.source(function (opts) { | |
38 | 52 | return sbot.createUserStream(opts) |
39 | - }, | |
40 | - sbot_get: function (key, cb) { | |
53 | + }), | |
54 | + sbot_get: rec.async(function (key, cb) { | |
41 | 55 | sbot.get(key, cb) |
42 | - }, | |
43 | - sbot_publish: function (msg, cb) { | |
56 | + }), | |
57 | + sbot_publish: rec.async(function (msg, cb) { | |
44 | 58 | sbot.publish(msg, cb) |
45 | - }, | |
46 | - sbot_whoami: function (cb) { | |
59 | + }), | |
60 | + sbot_whoami: rec.async(function (cb) { | |
47 | 61 | sbot.whoami(cb) |
48 | - } | |
62 | + }) | |
49 | 63 | } |
50 | 64 | } |
51 | 65 | |
52 | 66 |
Built with git-ssb-web