deferred-open
Make methods wait for async constructors.
For example, database libraries like LevelUp or node-redis expose an API that you can use immediately, although the connection hasn't been established yet. This greatly helps reduce callback depth.
In most cases you rather want this:
var db = ;db;
than this:
;
So, all calls to member functions have to be deffered until the connection is up. This module helps by mixing in to an Object and deferring operations until the constructor is done doing its async stuff.
Usage
Install Deferred
onto a function and make doAThing
wait until the
constructor finished doing its async operations.
var Deferred = ; { var self = this; Deferred; // => self._ready, self._queue selfloaded = false; // fake internal state ;} SomethingprototypedoAThing = ;
And use it like this:
var something = ;something; // "success" after 500ms
Non deferred open
If you want to support non deferred open too, use this._queue
:
{ var self = this; if !self instanceof Something return cb; Deferred; if cb self; // allow async api too selfloaded = false; ;}
Now you can use it like this:
// Deferredvar something = ;something; // Or non deferred;
API
Deferred.install(fn)
Call this in fn
's constructor in order to install the _ready
and _queue
member functions.
this._ready([err])
Call this when you finished doing your async stuff.
If you pass something as the err
parameter,
- a deferred call will throw
- a non deferred call will pass that as first argument to the callback
this._queue(fn)
Call fn
with (err, this)
as soon as this._ready()
has been
called (with the optional error argument).
Deferred(fn)
Returns a function that gets called only after the internal deferred object
has been signaled by this._ready()
. Returns this
when not ready, so it
works with chained/fluent apis.
Deferred.stream(fn[, opts])
Returns a function the gets called only after the constructor is ready, but returns a paused stream immediately, that will later be hooked up with the real stream.
opts
is an optional options object that supports
readable (Boolean)
: The returned stream should bereadable
. Default: true.writable (Boolean)
: The returned stream should bewritable
. Default: true.
Installation
With npm do
$ npm install deferred-open
License
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.