bluebird
Version:
Full featured Promises/A+ implementation with exceptionally good performance
109 lines (93 loc) • 3.69 kB
JavaScript
/**
* Copyright (c) 2013 Petka Antonov
*
* 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:</p>
*
* 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.
*/
"use strict";
var PromiseArray = require( "./promise_array.js" );
var util = require("./util.js");
var inherits = util.inherits;
var isArray = util.isArray;
function SomePromiseArray( values, caller, boundTo ) {
this.constructor$( values, caller, boundTo );
this._howMany = 0;
}
inherits( SomePromiseArray, PromiseArray );
SomePromiseArray.prototype._init = function SomePromiseArray$_init() {
this._init$( void 0, 1 );
var isArrayResolved = isArray( this._values );
this._holes = isArrayResolved
? this._values.length - this.length()
: 0;
if( !this._isResolved() && isArrayResolved ) {
this._howMany = Math.max(0, Math.min( this._howMany, this.length() ) );
if( this.howMany() > this._canPossiblyFulfill() ) {
this._reject( [] );
}
}
};
SomePromiseArray.prototype.howMany = function SomePromiseArray$howMany() {
return this._howMany;
};
SomePromiseArray.prototype.setHowMany =
function SomePromiseArray$setHowMany( count ) {
if( this._isResolved() ) return;
this._howMany = count;
};
SomePromiseArray.prototype._promiseFulfilled =
function SomePromiseArray$_promiseFulfilled( value ) {
if( this._isResolved() ) return;
this._addFulfilled( value );
if( this._fulfilled() === this.howMany() ) {
this._values.length = this.howMany();
this._fulfill( this._values );
}
};
SomePromiseArray.prototype._promiseRejected =
function SomePromiseArray$_promiseRejected( reason ) {
if( this._isResolved() ) return;
this._addRejected( reason );
if( this.howMany() > this._canPossiblyFulfill() ) {
if( this._values.length === this.length() ) {
this._reject([]);
}
else {
this._reject( this._values.slice( this.length() + this._holes ) );
}
}
};
SomePromiseArray.prototype._fulfilled = function SomePromiseArray$_fulfilled() {
return this._totalResolved;
};
SomePromiseArray.prototype._rejected = function SomePromiseArray$_rejected() {
return this._values.length - this.length() - this._holes;
};
SomePromiseArray.prototype._addRejected =
function SomePromiseArray$_addRejected( reason ) {
this._values.push( reason );
};
SomePromiseArray.prototype._addFulfilled =
function SomePromiseArray$_addFulfilled( value ) {
this._values[ this._totalResolved++ ] = value;
};
SomePromiseArray.prototype._canPossiblyFulfill =
function SomePromiseArray$_canPossiblyFulfill() {
return this.length() - this._rejected();
};
module.exports = SomePromiseArray;