ensure internal streams have same constructor as public streams
This commit is contained in:
parent
678e4912c7
commit
f201ea73ff
7 changed files with 39 additions and 33 deletions
3
index.js
3
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
|
||||
|
|
|
|||
54
mithril.js
54
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
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
module.exports = require("./request/request")(window, console.error.bind(console))
|
||||
var Stream = require("./stream")
|
||||
module.exports = require("./request/request")(window, Stream)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue