diff --git a/lib/index.js b/lib/index.js index 61a6402..9770296 100644 --- a/lib/index.js +++ b/lib/index.js @@ -535,7 +535,7 @@ function createStream() { } s.hasVal = false; s.val = undefined; - s.vals = []; + s.updaters = []; s.listeners = []; s.queued = false; s.end = undefined; @@ -610,9 +610,7 @@ function updateStream(s) { if (isEnded(s)) return; if (s.depsMet !== true && initialDepsNotMet(s)) return; if (inStream !== undefined) { - toUpdate.push(function() { - updateStream(s); - }); + updateLaterUsing(updateStream, s); return; } inStream = s; @@ -673,14 +671,21 @@ function findDeps(s) { } } +function updateLaterUsing(updater, stream) { + toUpdate.push(stream); + stream.updaters.push(updater); + stream.shouldUpdate = true; +} + /** * @private */ function flushUpdate() { flushingUpdateQueue = true; while (toUpdate.length > 0) { - var updater = toUpdate.shift(); - updater(); + var stream = toUpdate.shift(); + var nextUpdateFn = stream.updaters.shift(); + if (nextUpdateFn && stream.shouldUpdate) nextUpdateFn(stream); } flushingUpdateQueue = false; } @@ -701,12 +706,12 @@ function updateStreamValue(n, s) { } else if (inStream === s) { markListeners(s, s.listeners); } else { - toUpdate.push(function() { - updateStreamValue(n, s); - }); + updateLaterUsing(updateStreamValue_(n), s); } } +var updateStreamValue_ = curryN(2, updateStreamValue) + /** * @private */