call an async method with configurable timeout and when connected if reconnect is used
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.
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 = reconnectclient = multilevelclient;streampipeclientpipestream;connectPORT;var get = whenConnectedclientgetkeycb;reconnect:recon;var dumpDB = whenConnectedreturn clientcreateReadStream;reconnect:reconstream:true;// get a key.get'a'console.log'get called back';;var dump = dumpDB;// output a db dump.dumpon'data'console.log'db: 'data;;
this exports one function.
whenConnectedthe function to call when connectedoptions
- an EventEmitter that provides two events [connect],[disconnect] and has a [connected] property which will be used to understand the inital state of the connection.
- if no reconnect is passed this will still work but about the only other useful thing it provides is timeout callbacks.
- required if the wrapped method returns a stream.
- TODO duplex. if anyone needs it.
- optional default -1 / no timeout
- a maximum numbner of miliseconds to wait for...
- any callback method to call back
- or any stream method to get called and return a stream
- the error object will have a [code] property that equals 'E_TIMEDOUT'
- optional default 0
- the number of times the wrapped call should be retried if a 'disconnect' event is fired before the callback is complete.
- this does not increase the timeout for the call.
- if retires is exceded or not provided any disconnect while waiting for a call will callback with an E_DISCONNECT error
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.
- the time for callback to be called excedes specified value.
- the time to get a connection to a method that returns a stream took to long to get a connection.
- a disconnect event has been fired while a callback was pending.
- a disconnect event was fired before the end event was triggered on a stream.
- a method defined as returns streams did not return a stream. this is emitted on the proxy stream you have a handle too so bind error