git ssb

2+

Dominic / ssb-client



Commit 933ab4f9182c91eb147157a749e4181bfd0cc640

make functions chainable, and pass in config instead of address

Dominic Tarr committed on 3/6/2015, 9:49:57 AM
Parent: 2432ab835f42ded3c3a546abecd81a12a8de3d44

Files changed

index.jschanged
index.jsView
@@ -3,20 +3,30 @@
33 var address = require('ssb-address')
44 var ws = require('pull-ws-server')
55 var Serializer = require('pull-serializer')
66 var ssbKeys = require('ssb-keys')
7-var manifest = require('ssb-manifest')
7 +var loadManf = require('ssb-manifest/load')
88 var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults'))
99
10-module.exports = function (keys, addr, readyCb) {
11- var client = muxrpc(manifest, false, serialize)()
10 +function isFunction (f) {
11 + return 'function' === typeof f
12 +}
13 +
14 +module.exports = function (keys, config, readyCb) {
15 + var manifest
16 + //if we are in the browser
17 + config.host = config.host || 'localhost'
18 + var client = muxrpc(loadManf(config), false, serialize)()
1219 client.keys = keys
1320
1421 var wsStream
1522 var rpcStream
1623
1724 client.connect = function(addr, cb) {
18- addr = address(addr)
25 + if(isFunction(addr))
26 + cb = addr, addr = null
27 +
28 + addr = address(config || addr)
1929 if (wsStream) {
2030 wsStream.socket.close()
2131 client._emit('reconnecting')
2232 }
@@ -45,15 +55,18 @@
4555 onclose_ && onclose_()
4656 rpcStream.close(function(){})
4757 client._emit('close')
4858 }
59 +
60 + return client
4961 }
5062
5163 client.close = function(cb) {
5264 wsStream.socket.close()
5365 rpcStream.close(function () {
5466 cb && cb()
5567 })
68 + return client
5669 }
5770
5871 client.reconnect = function(opts) {
5972 opts = opts || {}
@@ -62,8 +75,9 @@
6275 setTimeout(client.connect.bind(client, client.addr), opts.wait)
6376 else
6477 client.connect(client.addr)
6578 })
79 + return client
6680 }
6781
6882 client.publish = function (content, cb) {
6983 client.getLatest(client.keys.id, function (err, prev) {
@@ -82,8 +96,9 @@
8296 var msg = createMsg(client.keys, null, content, prev||null)
8397 client.add(msg, cb)
8498 }
8599 })
100 + return client
86101 }
87102
88103 var auth_ = client.auth
89104 client.auth = function (cb) {
@@ -92,13 +107,13 @@
92107 ts: Date.now(),
93108 public: client.keys.public
94109 })
95110 auth_.call(client, authReq, cb)
111 + return client
96112 }
97113
98- client.connect(addr, readyCb)
99114 return client
100115 }
101116
102117 function serialize (stream) {
103118 return Serializer(stream, JSON, {split: '\n\n'})
104-}
119 +}

Built with git-ssb-web