|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"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; |
|
}; |
|
|