Prevent Infinite Loop in m.prop.combine

Passing a non-stream value to combine or merge will result in uncaught
errors that are not descriptive enough to be helpful.
This commit is contained in:
Douglas Brown 2016-09-09 18:05:25 -04:00
parent 67bb288e01
commit fd0d7059df
2 changed files with 16 additions and 0 deletions

View file

@ -116,6 +116,7 @@ module.exports = function(log) {
return initDependency(self, [stream], derive, recover)
}
function combine(fn, streams) {
if (streams.length > streams.filter(valid).length) throw new Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream")
return initDependency(createStream(), streams, function() {
var failed = streams.filter(errored)
if (failed.length > 0) throw {__error: failed[0]._state.error}
@ -177,6 +178,7 @@ module.exports = function(log) {
function valueOf() {return this._state.value}
function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value}
function valid(stream) {return stream._state }
function active(stream) {return stream._state.state === 1}
function changed(stream) {return stream._state.changed}
function notEnded(stream) {return stream._state.state !== 2}

View file

@ -171,6 +171,20 @@ o.spec("stream", function() {
o(b()).equals(undefined)
})
o("combine will throw with a helpful error if given non-stream values", function () {
var spy = o.spy()
var a = Stream(1)
var thrown = null;
try {
var b = Stream.combine(spy, [a, ''])
} catch (e) {
thrown = e
}
o(thrown).notEquals(null)
o(thrown.constructor === TypeError).equals(false)
o(spy.callCount).equals(0)
})
})
o.spec("merge", function() {
o("transforms an array of streams to an array of values", function() {