soldair / node-when-connected

reconnection retry and stream continuation for multilevel
1 stars 0 forks source link

Build Status

when-connected

perform a callback when "connected" or fire callback with timeout error if its taken too long.

the most important thing this does is provides you with the errors required to manage callback and stream disconnection in necessary ways.

example

this lib is generic and ensures that wrapped calls are only made when connected. these examples use multilevel a package used to interact with leveldb over the network but it is not the only reason this is useful.

var mulitlevel = require('multilevel');
var reconnect = require('reconnect');
var whenConnected = require('when-connected')

var client;
var recon = reconnect(function(stream){
  client = multilevel.client();
  stream.pipe(client).pipe(stream); 
}).connect(PORT);

var get = whenConnected(function(key,cb){
  client.get(key,cb);
},{reconnect:recon});

var dumpDB = whenConnected(function(){
  return client.createReadStream();
},{reconnect:recon,stream:true});

// get a key.
get('a',function(err,data){
  console.log('get called back');
});

var dump = dumpDB();
// output a db dump.
dump.on('data',function(data){
  console.log('db: ',data);
});

api

this exports one function.

whenConnected([the function to call when connected],[options])

options

errors

error objects returned by this lib all have a code proerty that is one of these values. they are either emitted as errors onn streams or passed as the error to a callback.