Commit 933ab4f9182c91eb147157a749e4181bfd0cc640
make functions chainable, and pass in config instead of address
Dominic Tarr committed on 3/6/2015, 9:49:57 AMParent: 2432ab835f42ded3c3a546abecd81a12a8de3d44
Files changed
index.js | changed |
index.js | |||
---|---|---|---|
@@ -3,20 +3,30 @@ | |||
3 | 3 … | var address = require('ssb-address') | |
4 | 4 … | var ws = require('pull-ws-server') | |
5 | 5 … | var Serializer = require('pull-serializer') | |
6 | 6 … | var ssbKeys = require('ssb-keys') | |
7 | -var manifest = require('ssb-manifest') | ||
7 … | +var loadManf = require('ssb-manifest/load') | ||
8 | 8 … | var createMsg = require('secure-scuttlebutt/message')(require('secure-scuttlebutt/defaults')) | |
9 | 9 … | ||
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)() | ||
12 | 19 … | client.keys = keys | |
13 | 20 … | ||
14 | 21 … | var wsStream | |
15 | 22 … | var rpcStream | |
16 | 23 … | ||
17 | 24 … | client.connect = function(addr, cb) { | |
18 | - addr = address(addr) | ||
25 … | + if(isFunction(addr)) | ||
26 … | + cb = addr, addr = null | ||
27 … | + | ||
28 … | + addr = address(config || addr) | ||
19 | 29 … | if (wsStream) { | |
20 | 30 … | wsStream.socket.close() | |
21 | 31 … | client._emit('reconnecting') | |
22 | 32 … | } | |
@@ -45,15 +55,18 @@ | |||
45 | 55 … | onclose_ && onclose_() | |
46 | 56 … | rpcStream.close(function(){}) | |
47 | 57 … | client._emit('close') | |
48 | 58 … | } | |
59 … | + | ||
60 … | + return client | ||
49 | 61 … | } | |
50 | 62 … | ||
51 | 63 … | client.close = function(cb) { | |
52 | 64 … | wsStream.socket.close() | |
53 | 65 … | rpcStream.close(function () { | |
54 | 66 … | cb && cb() | |
55 | 67 … | }) | |
68 … | + return client | ||
56 | 69 … | } | |
57 | 70 … | ||
58 | 71 … | client.reconnect = function(opts) { | |
59 | 72 … | opts = opts || {} | |
@@ -62,8 +75,9 @@ | |||
62 | 75 … | setTimeout(client.connect.bind(client, client.addr), opts.wait) | |
63 | 76 … | else | |
64 | 77 … | client.connect(client.addr) | |
65 | 78 … | }) | |
79 … | + return client | ||
66 | 80 … | } | |
67 | 81 … | ||
68 | 82 … | client.publish = function (content, cb) { | |
69 | 83 … | client.getLatest(client.keys.id, function (err, prev) { | |
@@ -82,8 +96,9 @@ | |||
82 | 96 … | var msg = createMsg(client.keys, null, content, prev||null) | |
83 | 97 … | client.add(msg, cb) | |
84 | 98 … | } | |
85 | 99 … | }) | |
100 … | + return client | ||
86 | 101 … | } | |
87 | 102 … | ||
88 | 103 … | var auth_ = client.auth | |
89 | 104 … | client.auth = function (cb) { | |
@@ -92,13 +107,13 @@ | |||
92 | 107 … | ts: Date.now(), | |
93 | 108 … | public: client.keys.public | |
94 | 109 … | }) | |
95 | 110 … | auth_.call(client, authReq, cb) | |
111 … | + return client | ||
96 | 112 … | } | |
97 | 113 … | ||
98 | - client.connect(addr, readyCb) | ||
99 | 114 … | return client | |
100 | 115 … | } | |
101 | 116 … | ||
102 | 117 … | function serialize (stream) { | |
103 | 118 … | return Serializer(stream, JSON, {split: '\n\n'}) | |
104 | -} | ||
119 … | +} |
Built with git-ssb-web