fix stream uncaught error reporting

fix stream JSON stringification
rename internal render function to `use`
This commit is contained in:
Leo Horie 2016-08-12 00:41:44 -04:00
parent 499a9ccc6d
commit 2ee15e3639
5 changed files with 424 additions and 382 deletions

View file

@ -11,13 +11,13 @@ module.exports = function($window, renderer, pubsub) {
var replay = router.defineRoutes(routes, function(payload, args, path, route) { var replay = router.defineRoutes(routes, function(payload, args, path, route) {
if (typeof payload.view !== "function") { if (typeof payload.view !== "function") {
if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode} if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode}
var render = function(component) { var use = function(component) {
current.path = path, current.component = component current.path = path, current.component = component
renderer.render(root, payload.render(Vnode(component, null, args, undefined, undefined, undefined))) renderer.render(root, payload.render(Vnode(component, null, args, undefined, undefined, undefined)))
} }
if (typeof payload.resolve !== "function") payload.resolve = function() {render(current.component)} if (typeof payload.resolve !== "function") payload.resolve = function() {use(current.component)}
if (path !== current.path) payload.resolve(render, args, path, route) if (path !== current.path) payload.resolve(use, args, path, route)
else render(current.component) else use(current.component)
} }
else { else {
renderer.render(root, Vnode(payload, null, args, undefined, undefined, undefined)) renderer.render(root, Vnode(payload, null, args, undefined, undefined, undefined))

View file

@ -1,6 +1,6 @@
"use strict" "use strict"
var Stream = require("./util/stream") var Stream = require("./util/stream")(console.error.bind(console))
var m = require("./render/hyperscript") var m = require("./render/hyperscript")
var renderService = require("./render/render")(window) var renderService = require("./render/render")(window)
var requestService = require("./request/request")(window) var requestService = require("./request/request")(window)

View file

@ -1,4 +1,5 @@
new function() { new function() {
var Stream = function(log) {
var guid = 0, noop = function() {}, HALT = {} var guid = 0, noop = function() {}, HALT = {}
function createStream() { function createStream() {
function stream() { function stream() {
@ -74,8 +75,8 @@ function resolve(stream, update, shouldRecover) {
update(stream, value, undefined) update(stream, value, undefined)
} }
catch (e) { catch (e) {
update(stream, undefined, e) update(stream, undefined, e.__error != null ? e.__error : e)
reportUncaughtError(stream, e) if (e.__error == null) reportUncaughtError(stream, e)
} }
return true return true
} }
@ -91,9 +92,9 @@ function finalize(stream) {
for (var id in stream._state.deps) stream._state.deps[id]._state.changed = false for (var id in stream._state.deps) stream._state.deps[id]._state.changed = false
} }
function reportUncaughtError(stream, e) { function reportUncaughtError(stream, e) {
if (Object.keys(stream._state.deps).length === 0 && stream._state.derive == null) { if (Object.keys(stream._state.deps).length === 0) {
setTimeout(function() { setTimeout(function() {
if (Object.keys(stream._state.deps).length === 0) console.error(e) if (Object.keys(stream._state.deps).length === 0) log(e)
}, 0) }, 0)
} }
} }
@ -112,7 +113,7 @@ function doCatch(fn) {
function combine(fn, streams) { function combine(fn, streams) {
return initDependency(createStream(), streams, function() { return initDependency(createStream(), streams, function() {
var failed = streams.filter(errored) var failed = streams.filter(errored)
if (failed.length > 0) throw failed[0]._state.error if (failed.length > 0) throw {__error: failed[0]._state.error}
return fn.apply(this, streams.concat([streams.filter(changed)])) return fn.apply(this, streams.concat([streams.filter(changed)]))
}, undefined) }, undefined)
} }
@ -125,11 +126,11 @@ function absorb(stream, value) {
} }
absorbable.map(update).catch(function(e) { absorbable.map(update).catch(function(e) {
update() update()
throw e throw {__error: e}
}) })
if (absorbable._state.state === 0) return HALT if (absorbable._state.state === 0) return HALT
if (absorbable._state.error) throw absorbable._state.error if (absorbable._state.error) throw {__error: absorbable._state.error}
value = absorbable._state.value value = absorbable._state.value
} }
return value return value
@ -165,7 +166,7 @@ function unregisterStream(stream) {
function map(fn) {return combine(function(stream) {return fn(stream())}, [this])} function map(fn) {return combine(function(stream) {return fn(stream())}, [this])}
function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])} function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])}
function valueOf() {return this._state.value} function valueOf() {return this._state.value}
function toJSON() {return JSON.stringify(this._state.value)} function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value}
function active(stream) {return stream._state.state === 1} function active(stream) {return stream._state.state === 1}
function changed(stream) {return stream._state.changed} function changed(stream) {return stream._state.changed}
function notEnded(stream) {return stream._state.state !== 2} function notEnded(stream) {return stream._state.state !== 2}
@ -180,7 +181,8 @@ function merge(streams) {
return streams.map(function(s) {return s()}) return streams.map(function(s) {return s()})
}, streams) }, streams)
} }
var Stream = {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT} return {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT}
}(console.error.bind(console))
function Vnode(tag, key, attrs, children, text, dom) { function Vnode(tag, key, attrs, children, text, dom) {
return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined} return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined}
} }
@ -1087,13 +1089,13 @@ m.route = function($window, renderer, pubsub) {
var replay = router.defineRoutes(routes, function(payload, args, path, route) { var replay = router.defineRoutes(routes, function(payload, args, path, route) {
if (typeof payload.view !== "function") { if (typeof payload.view !== "function") {
if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode} if (typeof payload.render !== "function") payload.render = function(vnode) {return vnode}
var render = function(component) { var use = function(component) {
current.path = path, current.component = component current.path = path, current.component = component
renderer.render(root, payload.render(Vnode(component, null, args, undefined, undefined, undefined))) renderer.render(root, payload.render(Vnode(component, null, args, undefined, undefined, undefined)))
} }
if (typeof payload.resolve !== "function") payload.resolve = function() {render(current.component)} if (typeof payload.resolve !== "function") payload.resolve = function() {use(current.component)}
if (path !== current.path) payload.resolve(render, args, path, route) if (path !== current.path) payload.resolve(use, args, path, route)
else render(current.component) else use(current.component)
} }
else { else {
renderer.render(root, Vnode(payload, null, args, undefined, undefined, undefined)) renderer.render(root, Vnode(payload, null, args, undefined, undefined, undefined))

View file

@ -1,5 +1,6 @@
"use strict" "use strict"
module.exports = function(log) {
var guid = 0, noop = function() {}, HALT = {} var guid = 0, noop = function() {}, HALT = {}
function createStream() { function createStream() {
function stream() { function stream() {
@ -78,8 +79,8 @@ function resolve(stream, update, shouldRecover) {
update(stream, value, undefined) update(stream, value, undefined)
} }
catch (e) { catch (e) {
update(stream, undefined, e) update(stream, undefined, e.__error != null ? e.__error : e)
reportUncaughtError(stream, e) if (e.__error == null) reportUncaughtError(stream, e)
} }
return true return true
} }
@ -95,9 +96,9 @@ function finalize(stream) {
for (var id in stream._state.deps) stream._state.deps[id]._state.changed = false for (var id in stream._state.deps) stream._state.deps[id]._state.changed = false
} }
function reportUncaughtError(stream, e) { function reportUncaughtError(stream, e) {
if (Object.keys(stream._state.deps).length === 0 && stream._state.derive == null) { if (Object.keys(stream._state.deps).length === 0) {
setTimeout(function() { setTimeout(function() {
if (Object.keys(stream._state.deps).length === 0) console.error(e) if (Object.keys(stream._state.deps).length === 0) log(e)
}, 0) }, 0)
} }
} }
@ -117,7 +118,7 @@ function doCatch(fn) {
function combine(fn, streams) { function combine(fn, streams) {
return initDependency(createStream(), streams, function() { return initDependency(createStream(), streams, function() {
var failed = streams.filter(errored) var failed = streams.filter(errored)
if (failed.length > 0) throw failed[0]._state.error if (failed.length > 0) throw {__error: failed[0]._state.error}
return fn.apply(this, streams.concat([streams.filter(changed)])) return fn.apply(this, streams.concat([streams.filter(changed)]))
}, undefined) }, undefined)
} }
@ -130,11 +131,11 @@ function absorb(stream, value) {
} }
absorbable.map(update).catch(function(e) { absorbable.map(update).catch(function(e) {
update() update()
throw e throw {__error: e}
}) })
if (absorbable._state.state === 0) return HALT if (absorbable._state.state === 0) return HALT
if (absorbable._state.error) throw absorbable._state.error if (absorbable._state.error) throw {__error: absorbable._state.error}
value = absorbable._state.value value = absorbable._state.value
} }
return value return value
@ -174,7 +175,7 @@ function unregisterStream(stream) {
function map(fn) {return combine(function(stream) {return fn(stream())}, [this])} function map(fn) {return combine(function(stream) {return fn(stream())}, [this])}
function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])} function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])}
function valueOf() {return this._state.value} function valueOf() {return this._state.value}
function toJSON() {return JSON.stringify(this._state.value)} function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value}
function active(stream) {return stream._state.state === 1} function active(stream) {return stream._state.state === 1}
function changed(stream) {return stream._state.changed} function changed(stream) {return stream._state.changed}
@ -193,4 +194,5 @@ function merge(streams) {
}, streams) }, streams)
} }
module.exports = {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT} return {stream: createStream, merge: merge, combine: combine, reject: reject, HALT: HALT}
}

View file

@ -2,9 +2,15 @@
var o = require("../../ospec/ospec") var o = require("../../ospec/ospec")
var callAsync = require("../../test-utils/callAsync") var callAsync = require("../../test-utils/callAsync")
var Stream = require("../../util/stream") var StreamFactory = require("../../util/stream")
o.spec("stream", function() { o.spec("stream", function() {
var Stream, spy
o.beforeEach(function() {
spy = o.spy()
Stream = StreamFactory(spy)
})
o.spec("stream", function() { o.spec("stream", function() {
o("works as getter/setter", function() { o("works as getter/setter", function() {
var stream = Stream.stream(1) var stream = Stream.stream(1)
@ -518,10 +524,11 @@ o.spec("stream", function() {
o(mapped()).equals(1) o(mapped()).equals(1)
}) })
o("works with errored stream", function() { o("works with errored stream", function() {
var rejected
var stream = Stream.stream(undefined) var stream = Stream.stream(undefined)
var mapped = stream.run(function(value) {return Stream.reject(new Error("error"))}) var mapped = stream.run(function(value) {
return Stream.reject(new Error("error"))
mapped.catch(function() {}) //silence reportUncaughtException })
o(mapped()).equals(undefined) o(mapped()).equals(undefined)
o(mapped.error().message).equals("error") o(mapped.error().message).equals("error")
@ -803,14 +810,45 @@ o.spec("stream", function() {
}) })
o.spec("toJSON", function() { o.spec("toJSON", function() {
o("works", function() { o("works", function() {
o(Stream.stream(1).toJSON()).equals("1") o(Stream.stream(1).toJSON()).equals(1)
o(Stream.stream("a").toJSON()).equals("\"a\"") o(Stream.stream("a").toJSON()).equals("a")
o(Stream.stream(true).toJSON()).equals("true") o(Stream.stream(true).toJSON()).equals(true)
o(Stream.stream(null).toJSON()).equals("null") o(Stream.stream(null).toJSON()).equals(null)
o(Stream.stream(undefined).toJSON()).equals(undefined) o(Stream.stream(undefined).toJSON()).equals(undefined)
o(Stream.stream({a: 1}).toJSON()).deepEquals("{\"a\":1}") o(Stream.stream({a: 1}).toJSON()).deepEquals({a: 1})
o(Stream.stream([1, 2, 3]).toJSON()).deepEquals("[1,2,3]") o(Stream.stream([1, 2, 3]).toJSON()).deepEquals([1, 2, 3])
o(Stream.stream().toJSON()).equals(undefined) o(Stream.stream().toJSON()).equals(undefined)
o(Stream.stream(new Date(0)).toJSON()).equals(new Date(0).toJSON())
})
o("works w/ JSON.stringify", function() {
o(JSON.stringify(Stream.stream(1))).equals(JSON.stringify(1))
o(JSON.stringify(Stream.stream("a"))).equals(JSON.stringify("a"))
o(JSON.stringify(Stream.stream(true))).equals(JSON.stringify(true))
o(JSON.stringify(Stream.stream(null))).equals(JSON.stringify(null))
o(JSON.stringify(Stream.stream(undefined))).equals(JSON.stringify(undefined))
o(JSON.stringify(Stream.stream({a: 1}))).deepEquals(JSON.stringify({a: 1}))
o(JSON.stringify(Stream.stream([1, 2, 3]))).deepEquals(JSON.stringify([1, 2, 3]))
o(JSON.stringify(Stream.stream())).equals(JSON.stringify(undefined))
o(JSON.stringify(Stream.stream(new Date(0)))).equals(JSON.stringify(new Date(0)))
})
})
o.spec("uncaught exception reporting", function() {
o("reports thrown errors", function(done) {
Stream.stream(1).map(function() {throw new Error("error")})
setTimeout(function() {
o(spy.callCount).equals(1)
o(spy.args[0].message).equals("error")
done()
}, 0)
})
o("does not report explicit rejections", function(done) {
Stream.reject(1)
setTimeout(function() {
o(spy.callCount).equals(0)
done()
}, 0)
}) })
}) })
o.spec("map", function() { o.spec("map", function() {