From f201ea73ff5e72f646f415db2d69880a4360cda4 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Wed, 24 Aug 2016 01:08:20 -0400 Subject: [PATCH] ensure internal streams have same constructor as public streams --- index.js | 3 +- mithril.js | 54 +++++++++++++++++------------------ request.js | 3 +- request/request.js | 3 +- request/tests/test-jsonp.js | 3 +- request/tests/test-request.js | 3 +- tests/test-api.js | 3 ++ 7 files changed, 39 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 089d390b..6155ba71 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,14 @@ var requestService = require("./request") var redrawService = require("./redraw") var parseQueryString = require("./querystring/parse") var buildQueryString = require("./querystring/build") +var Stream = require("./stream") requestService.setCompletionCallback(redrawService.publish) m.route = require("./route") m.mount = require("./mount") m.withAttr = require("./util/withAttr") -m.prop = require("./stream") +m.prop = Stream m.render = renderService.render m.redraw = redrawService.publish m.request = requestService.request diff --git a/mithril.js b/mithril.js index a3e993da..9a391528 100644 --- a/mithril.js +++ b/mithril.js @@ -585,27 +585,6 @@ var renderService = function($window) { } return {render: render, setEventCallback: setEventCallback} }(window) -var buildQueryString = function(object) { - if (Object.prototype.toString.call(object) !== "[object Object]") return "" - var args = [] - for (var key in object) { - destructure(key, object[key]) - } - return args.join("&") - function destructure(key, value) { - if (value instanceof Array) { - for (var i = 0; i < value.length; i++) { - destructure(key + "[" + i + "]", value[i]) - } - } - else if (Object.prototype.toString.call(value) === "[object Object]") { - for (var i in value) { - destructure(key + "[" + i + "]", value[i]) - } - } - else args.push(encodeURIComponent(key) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : "")) - } -} var StreamFactory = function(log) { var guid = 0, noop = function() {}, HALT = {} function createStream() { @@ -794,14 +773,35 @@ var StreamFactory = function(log) { createStream.HALT = HALT return createStream } -var requestService = function($window, log) { - var Stream = StreamFactory(log) +var Stream = StreamFactory(console.log.bind(console)) +var buildQueryString = function(object) { + if (Object.prototype.toString.call(object) !== "[object Object]") return "" + var args = [] + for (var key in object) { + destructure(key, object[key]) + } + return args.join("&") + function destructure(key, value) { + if (value instanceof Array) { + for (var i = 0; i < value.length; i++) { + destructure(key + "[" + i + "]", value[i]) + } + } + else if (Object.prototype.toString.call(value) === "[object Object]") { + for (var i in value) { + destructure(key + "[" + i + "]", value[i]) + } + } + else args.push(encodeURIComponent(key) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : "")) + } +} +var requestService = function($window, Stream1) { var callbackCount = 0 var oncompletion function setCompletionCallback(callback) {oncompletion = callback} function request(args) { - var stream = Stream() + var stream = Stream1() if (args.initialValue !== undefined) stream(args.initialValue) var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE" @@ -852,7 +852,7 @@ var requestService = function($window, log) { return stream } function jsonp(args) { - var stream = Stream() + var stream = Stream1() if (args.initialValue !== undefined) stream(args.initialValue) var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ @@ -915,7 +915,7 @@ var requestService = function($window, log) { } return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback} -}(window, console.error.bind(console)) +}(window, Stream) var redrawService = function() { var callbacks = [] function unsubscribe(callback) { @@ -1150,7 +1150,7 @@ m.withAttr = function(attrName, callback, context) { return callback.call(context || this, attrName in e.currentTarget ? e.currentTarget[attrName] : e.currentTarget.getAttribute(attrName)) } } -m.prop = StreamFactory(console.log.bind(console)) +m.prop = Stream m.render = renderService.render m.redraw = redrawService.publish m.request = requestService.request diff --git a/request.js b/request.js index b1525c25..43c24cbb 100644 --- a/request.js +++ b/request.js @@ -1 +1,2 @@ -module.exports = require("./request/request")(window, console.error.bind(console)) +var Stream = require("./stream") +module.exports = require("./request/request")(window, Stream) diff --git a/request/request.js b/request/request.js index bb76c79e..eb8bf19e 100644 --- a/request/request.js +++ b/request/request.js @@ -3,8 +3,7 @@ var buildQueryString = require("../querystring/build") var StreamFactory = require("../util/stream") -module.exports = function($window, log) { - var Stream = StreamFactory(log) +module.exports = function($window, Stream) { var callbackCount = 0 var oncompletion diff --git a/request/tests/test-jsonp.js b/request/tests/test-jsonp.js index 994df034..a0aff58c 100644 --- a/request/tests/test-jsonp.js +++ b/request/tests/test-jsonp.js @@ -3,6 +3,7 @@ var o = require("../../ospec/ospec") var xhrMock = require("../../test-utils/xhrMock") var Request = require("../../request/request") +var StreamFactory = require("../../util/stream") var parseQueryString = require("../../querystring/parse") o.spec("jsonp", function() { @@ -10,7 +11,7 @@ o.spec("jsonp", function() { o.beforeEach(function() { mock = xhrMock() spy = o.spy() - jsonp = new Request(mock, spy).jsonp + jsonp = new Request(mock, StreamFactory(spy)).jsonp }) o("works", function(done) { diff --git a/request/tests/test-request.js b/request/tests/test-request.js index 31bcb68a..b39718f2 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -3,13 +3,14 @@ var o = require("../../ospec/ospec") var xhrMock = require("../../test-utils/xhrMock") var Request = require("../../request/request") +var StreamFactory = require("../../util/stream") o.spec("xhr", function() { var mock, xhr, spy o.beforeEach(function() { mock = xhrMock() spy = o.spy() - xhr = new Request(mock, spy).request + xhr = new Request(mock, StreamFactory(spy)).request }) o.spec("success", function() { diff --git a/tests/test-api.js b/tests/test-api.js index 584144f0..4d57d5cc 100644 --- a/tests/test-api.js +++ b/tests/test-api.js @@ -188,6 +188,9 @@ o.spec("api", function() { o("works", function() { o(typeof m.request).equals("function") // TODO improve }) + o("return value is stream", function() { + o(m.request({method: "GET", url: "[invalid]"}).constructor).equals(m.prop().constructor) + }) }) o.spec("m.jsonp", function() { o("works", function() {