Comparing version 0.8.3 to 0.8.4
{ | ||
"name": "most", | ||
"main": "most.js", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"homepage": "https://github.com/cujojs/most", | ||
@@ -14,8 +14,16 @@ "authors": [ | ||
"keywords": [ | ||
"stream", | ||
"promise", | ||
"promises", | ||
"async", | ||
"monad", | ||
"cujo" | ||
"reactive", | ||
"reactive programming", | ||
"reactive streams", | ||
"stream", | ||
"streams", | ||
"event stream", | ||
"promise", | ||
"promises", | ||
"monad", | ||
"monadic", | ||
"functional", | ||
"async", | ||
"cujojs", | ||
"cujo" | ||
], | ||
@@ -22,0 +30,0 @@ "license": "MIT", |
@@ -7,2 +7,3 @@ var WhenPromise = require('when/es6-shim/Promise'); | ||
exports.when = when; | ||
exports.raceIndex = raceIndex; | ||
exports.never = WhenPromise.never; | ||
@@ -56,1 +57,28 @@ exports.getStatus = WhenPromise._handler; | ||
/** | ||
* Like Promise.race, but calls f with the value *and index* of the first | ||
* fulfilled promise, and returns a promise for the result. | ||
* @param {function(x:*, i:Number):*} f function to apply to first | ||
* fulfilled value and its index | ||
* @param {Array} promises | ||
* @returns {Promise} | ||
*/ | ||
function raceIndex(f, promises) { | ||
var done = false; | ||
return new WhenPromise(runRaceIndex); | ||
function runRaceIndex(resolve, reject) { | ||
for(var i= 0, l=promises.length; i<l; ++i) { | ||
settleOne(resolve, reject, i, promises[i]); | ||
} | ||
} | ||
function settleOne(resolve, reject, i, p) { | ||
WhenPromise.resolve(p).then(function(x) { | ||
if(!done) { | ||
done = true; | ||
resolve(f(x, i)); | ||
} | ||
}, reject); | ||
} | ||
} |
@@ -10,4 +10,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var race = promise.Promise.race; | ||
var resolve = promise.Promise.resolve; | ||
var raceIndex = promise.raceIndex; | ||
var getStatus = promise.getStatus; | ||
@@ -108,13 +107,7 @@ | ||
function unamb(f, steps) { | ||
if(steps.length === 1) { | ||
return resolve(steps[0]).then(function(x) { | ||
return f(x, 0); | ||
}); | ||
} | ||
var winner = decide(steps); | ||
if(winner === null) { | ||
return race(steps).then(function() { | ||
return decideWith(f, steps); | ||
}); | ||
return raceIndex(function(winner, index) { | ||
return decideWith(f, steps, winner, index); | ||
}, steps); | ||
} | ||
@@ -125,12 +118,13 @@ | ||
function decideWith(f, steps) { | ||
function decideWith(f, steps, maybeWinner, index) { | ||
var winner = decide(steps); | ||
return f(winner.value, winner.index); | ||
return winner === null ? f(maybeWinner, index) | ||
: f(winner.value, winner.index); | ||
} | ||
function decide(steps) { | ||
var index = -1; | ||
var winner; | ||
var index = -1, t = Infinity, i = 0; | ||
var winner, h; | ||
for(var i=0, t=Infinity, h; i<steps.length; ++i) { | ||
for(; i<steps.length; ++i) { | ||
h = getStatus(steps[i]); | ||
@@ -137,0 +131,0 @@ if(h.state() > 0 && h.value.time < t) { |
@@ -16,2 +16,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var when = promise.when; | ||
var resolve = promise.Promise.resolve; | ||
var neverPromise = promise.never; | ||
@@ -99,3 +100,3 @@ var identity = base.identity; | ||
Stream.fromPromise = function(p) { | ||
return new Stream(identity, p.then(once)); | ||
return new Stream(identity, resolve(p).then(once)); | ||
}; | ||
@@ -102,0 +103,0 @@ |
{ | ||
"name": "most", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"description": "Monadic streams", | ||
@@ -14,10 +14,16 @@ "main": "most.js", | ||
"keywords": [ | ||
"cujojs", | ||
"cujo", | ||
"reactive", | ||
"reactive programming", | ||
"reactive streams", | ||
"stream", | ||
"streams", | ||
"event stream", | ||
"promise", | ||
"promises", | ||
"monad", | ||
"monadic", | ||
"functional" | ||
"functional", | ||
"async", | ||
"cujojs", | ||
"cujo" | ||
], | ||
@@ -24,0 +30,0 @@ "author": "brian@hovercraftstudios.com", |
111988
2552