blob: d3b89d49947d209140992203fd15d8b858dcde78 [file] [log] [blame]
/**
* Copyright (c) 2014 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";
module.exports = function (PromiseArray) {
var util = require("./util.js");
var RangeError = require("./errors.js").RangeError;
var inherits = util.inherits;
var isArray = util.isArray;
function SomePromiseArray(values, caller, boundTo) {
this.constructor$(values, caller, boundTo);
this._howMany = 0;
this._unwrap = false;
this._initialized = false;
}
inherits(SomePromiseArray, PromiseArray);
SomePromiseArray.prototype._init = function SomePromiseArray$_init() {
if (!this._initialized) {
return;
}
if (this._howMany === 0) {
this._resolve([]);
return;
}
this._init$(void 0, -2);
var isArrayResolved = isArray(this._values);
this._holes = isArrayResolved ? this._values.length - this.length() : 0;
if (!this._isResolved() &&
isArrayResolved &&
this._howMany > this._canPossiblyFulfill()) {
var message = "(Promise.some) input array contains less than " +
this._howMany + " promises";
this._reject(new RangeError(message));
}
};
SomePromiseArray.prototype.init = function SomePromiseArray$init() {
this._initialized = true;
this._init();
};
SomePromiseArray.prototype.setUnwrap = function SomePromiseArray$setUnwrap() {
this._unwrap = true;
};
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();
if (this.howMany() === 1 && this._unwrap) {
this._resolve(this._values[0]);
}
else {
this._resolve(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();
};
return SomePromiseArray;
};