git ssb

16+

Dominic / patchbay



Commit 5e55c4b6b3322e83b1d154ca6044fcc656c1bb2c

automatically reconnect sbot api

Dominic Tarr committed on 6/23/2016, 12:34:04 PM
Parent: df5168eeea500dbe048c18ffc665591d65df85f9

Files changed

sbot-api.jschanged
sbot-api.jsView
@@ -1,6 +1,7 @@
11 var pull = require('pull-stream')
22 var crypto = require('crypto')
3+var Reconnect = require('pull-reconnect')
34
45 function Hash (onHash) {
56 var hash = crypto.createHash('sha256')
67 return pull.through(function (data) {
@@ -15,38 +16,51 @@
1516 })
1617 }
1718
1819
19-module.exports = function (sbot) {
20+var createClient = require('ssb-client')
2021
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+
2135 return {
22- sbot_blobs_add: function (cb) {
36+ sbot_blobs_add: rec.sink(function (cb) {
2337 return pull(
2438 Hash(cb),
2539 sbot.blobs.add()
2640 )
27- },
28- sbot_links: function (query) {
41+ }),
42+ sbot_links: rec.source(function (query) {
2943 return sbot.links(query)
30- },
31- sbot_links2: function (query) {
44+ }),
45+ sbot_links2: rec.source(function (query) {
3246 return sbot.links2.read(query)
33- },
34- sbot_log: function (opts) {
47+ }),
48+ sbot_log: rec.source(function (opts) {
3549 return sbot.createLogStream(opts)
36- },
37- sbot_user_feed: function (opts) {
50+ }),
51+ sbot_user_feed: rec.source(function (opts) {
3852 return sbot.createUserStream(opts)
39- },
40- sbot_get: function (key, cb) {
53+ }),
54+ sbot_get: rec.async(function (key, cb) {
4155 sbot.get(key, cb)
42- },
43- sbot_publish: function (msg, cb) {
56+ }),
57+ sbot_publish: rec.async(function (msg, cb) {
4458 sbot.publish(msg, cb)
45- },
46- sbot_whoami: function (cb) {
59+ }),
60+ sbot_whoami: rec.async(function (cb) {
4761 sbot.whoami(cb)
48- }
62+ })
4963 }
5064 }
5165
5266

Built with git-ssb-web