Transport
A hub for centralizing all your request handlers within your application. Implements the disposable pattern.
Installation
npm install transport
How to use
Require and initialize transport:
var Transport = transport = ;
Requests
Define a simpleHandler
to handle a request and send the result back:
// Object with all the request handlersvar handlers = { // do something with the payload var result = ; // send the result back ; };
Add the handler to Transport
. The handlers have to be stored in an observable-store
. This allows for sharing the same handlers between several implementations of Transport
and react accordingly when handlers are added and removed.
var Store = ; transport;
Make the request to simpleHandler
:
transport;
Open a channel
Let's define a handler that will publish several updates.
var handlers = { var stream = ; stream; stream; };
Now we can open the channel and receive updates:
transport;
Close/dispose an open channel
transport.listen
returns a function. When the handler
also returns a function, it will be called by executing the one returned by transport.listen
. It can be used to stop whatever the handler
started and do some cleanup too.
var handlers = { var stream = ; stream; stream; return { stream; stream; } };
Now, when calling transport.listen
, we can call the stop()
function returned by the handler
.
var stop = transport; // When calling stop, the stop() function returned by the handler will be executed.;
Managing errors
A convenient way to bubble up errors is to follow the error first convention:
var handlers = { var stream = ; stream; stream; stream; };
And now we can handle the errors in the onEnd callback.
transport;
LICENSE
MIT