streams implementation
This commit is contained in:
parent
94a8be4fca
commit
b9ce90765d
12 changed files with 1104 additions and 167 deletions
|
|
@ -1,83 +1,94 @@
|
|||
"use strict"
|
||||
|
||||
var buildQueryString = require("../querystring/build")
|
||||
var Stream = require("../util/stream")
|
||||
|
||||
module.exports = function($window, Promise) {
|
||||
module.exports = function($window) {
|
||||
var callbackCount = 0
|
||||
|
||||
var oncompletion
|
||||
function setCompletionCallback(callback) {oncompletion = callback}
|
||||
|
||||
function xhr(args) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
||||
|
||||
if (typeof args.serialize !== "function") args.serialize = JSON.stringify
|
||||
if (typeof args.deserialize !== "function") args.deserialize = deserialize
|
||||
if (typeof args.extract !== "function") args.extract = extract
|
||||
|
||||
args.url = interpolate(args.url, args.data)
|
||||
if (useBody) args.data = args.serialize(args.data)
|
||||
else args.url = assemble(args.url, args.data)
|
||||
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
|
||||
|
||||
if (args.serialize === JSON.stringify && useBody) {
|
||||
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
||||
}
|
||||
if (args.deserialize === deserialize) {
|
||||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||
}
|
||||
|
||||
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
try {
|
||||
var response = args.deserialize(args.extract(xhr, args))
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
if (typeof args.type === "function") {
|
||||
if (response instanceof Array) {
|
||||
for (var i = 0; i < response.length; i++) {
|
||||
response[i] = new args.type(response[i])
|
||||
}
|
||||
var stream = Stream.stream()
|
||||
|
||||
var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
||||
|
||||
if (typeof args.serialize !== "function") args.serialize = JSON.stringify
|
||||
if (typeof args.deserialize !== "function") args.deserialize = deserialize
|
||||
if (typeof args.extract !== "function") args.extract = extract
|
||||
|
||||
args.url = interpolate(args.url, args.data)
|
||||
if (useBody) args.data = args.serialize(args.data)
|
||||
else args.url = assemble(args.url, args.data)
|
||||
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
|
||||
|
||||
if (args.serialize === JSON.stringify && useBody) {
|
||||
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
||||
}
|
||||
if (args.deserialize === deserialize) {
|
||||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||
}
|
||||
|
||||
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
try {
|
||||
var response = args.deserialize(args.extract(xhr, args))
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
if (typeof args.type === "function") {
|
||||
if (response instanceof Array) {
|
||||
for (var i = 0; i < response.length; i++) {
|
||||
response[i] = new args.type(response[i])
|
||||
}
|
||||
else response = new args.type(response)
|
||||
}
|
||||
|
||||
resolve(response)
|
||||
else response = new args.type(response)
|
||||
}
|
||||
else reject(new Error(xhr.responseText))
|
||||
}
|
||||
catch (e) {
|
||||
reject(e)
|
||||
|
||||
stream(response)
|
||||
}
|
||||
else stream.error(new Error(xhr.responseText))
|
||||
}
|
||||
catch (e) {
|
||||
stream.error(e)
|
||||
}
|
||||
if (typeof oncompletion === "function") oncompletion()
|
||||
}
|
||||
|
||||
if (useBody) xhr.send(args.data)
|
||||
else xhr.send()
|
||||
})
|
||||
}
|
||||
|
||||
if (useBody) xhr.send(args.data)
|
||||
else xhr.send()
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
function jsonp(args) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||
var script = $window.document.createElement("script")
|
||||
$window[callbackName] = function(data) {
|
||||
script.parentNode.removeChild(script)
|
||||
resolve(data)
|
||||
delete $window[callbackName]
|
||||
}
|
||||
script.onerror = function() {
|
||||
script.parentNode.removeChild(script)
|
||||
reject(new Error("JSONP request failed"))
|
||||
delete $window[callbackName]
|
||||
}
|
||||
if (args.data == null) args.data = {}
|
||||
args.url = interpolate(args.url, args.data)
|
||||
args.data[args.callbackKey || "callback"] = callbackName
|
||||
script.src = assemble(args.url, args.data)
|
||||
$window.document.documentElement.appendChild(script)
|
||||
})
|
||||
var stream = Stream.stream()
|
||||
|
||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||
var script = $window.document.createElement("script")
|
||||
$window[callbackName] = function(data) {
|
||||
script.parentNode.removeChild(script)
|
||||
stream(data)
|
||||
if (typeof oncompletion === "function") oncompletion()
|
||||
delete $window[callbackName]
|
||||
}
|
||||
script.onerror = function() {
|
||||
script.parentNode.removeChild(script)
|
||||
stream.error(new Error("JSONP request failed"))
|
||||
if (typeof oncompletion === "function") oncompletion()
|
||||
delete $window[callbackName]
|
||||
}
|
||||
if (args.data == null) args.data = {}
|
||||
args.url = interpolate(args.url, args.data)
|
||||
args.data[args.callbackKey || "callback"] = callbackName
|
||||
script.src = assemble(args.url, args.data)
|
||||
$window.document.documentElement.appendChild(script)
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
function interpolate(url, data) {
|
||||
|
|
@ -110,5 +121,5 @@ module.exports = function($window, Promise) {
|
|||
|
||||
function extract(xhr) {return xhr.responseText}
|
||||
|
||||
return {xhr: xhr, jsonp: jsonp}
|
||||
return {xhr: xhr, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,10 @@
|
|||
<script src="../../querystring/parse.js"></script>
|
||||
<script src="../../test-utils/parseURL.js"></script>
|
||||
<script src="../../test-utils/callAsync.js"></script>
|
||||
<script src="../../test-utils/ajaxMock.js"></script>
|
||||
<script src="../../test-utils/xhrMock.js"></script>
|
||||
|
||||
<script src="../../querystring/build.js"></script>
|
||||
<script src="../../util/stream.js"></script>
|
||||
<script src="../../request/request.js"></script>
|
||||
<script src="test-xhr.js"></script>
|
||||
<script src="test-jsonp.js"></script>
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ o.spec("jsonp", function() {
|
|||
var mock, jsonp
|
||||
o.beforeEach(function() {
|
||||
mock = xhrMock()
|
||||
jsonp = new Request(mock, Promise).jsonp
|
||||
jsonp = new Request(mock).jsonp
|
||||
})
|
||||
|
||||
o("works", function(done) {
|
||||
|
|
@ -19,9 +19,9 @@ o.spec("jsonp", function() {
|
|||
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify({a: 1}) + ")"}
|
||||
}
|
||||
})
|
||||
jsonp({url: "/item"}).then(function(data) {
|
||||
jsonp({url: "/item"}).map(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ other querystring params", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -30,10 +30,10 @@ o.spec("jsonp", function() {
|
|||
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify(queryData) + ")"}
|
||||
}
|
||||
})
|
||||
jsonp({url: "/item", data: {a: "b", c: "d"}}).then(function(data) {
|
||||
jsonp({url: "/item", data: {a: "b", c: "d"}}).map(function(data) {
|
||||
delete data["callback"]
|
||||
o(data).deepEquals({a: "b", c: "d"})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ custom callbackKey", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -42,9 +42,9 @@ o.spec("jsonp", function() {
|
|||
return {status: 200, responseText: queryData["cb"] + "(" + JSON.stringify({a: 2}) + ")"}
|
||||
}
|
||||
})
|
||||
jsonp({url: "/item", callbackKey: "cb"}).then(function(data) {
|
||||
jsonp({url: "/item", callbackKey: "cb"}).map(function(data) {
|
||||
o(data).deepEquals({a: 2})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("handles error", function(done) {
|
||||
jsonp({url: "/item", callbackKey: "cb"}).catch(function(e) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ o.spec("xhr", function() {
|
|||
var mock, xhr
|
||||
o.beforeEach(function() {
|
||||
mock = xhrMock()
|
||||
xhr = new Request(mock, Promise).xhr
|
||||
xhr = new Request(mock).xhr
|
||||
})
|
||||
|
||||
o.spec("success", function() {
|
||||
|
|
@ -19,9 +19,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item"}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item"}).map(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).then(function() {
|
||||
}).map(function() {
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
|
@ -31,9 +31,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item"}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item"}).map(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized data via GET", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -41,9 +41,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.query})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", data: {x: "y"}}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", data: {x: "y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: "?x=y"})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized data via POST", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -51,9 +51,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", data: {x: "y"}}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item", data: {x: "y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: {x: "y"}})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized data containing colon via GET", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -61,9 +61,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.query})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", data: {x: ":y"}}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", data: {x: ":y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: "?x=%3Ay"})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized data containing colon via POST", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -71,9 +71,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", data: {x: ":y"}}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item", data: {x: ":y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: {x: ":y"}})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized url via GET", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -81,9 +81,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.url, b: request.query})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: "/item/y", b: {}})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("works w/ parameterized url via POST", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -91,9 +91,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.url, b: JSON.parse(request.body)})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).map(function(data) {
|
||||
o(data).deepEquals({a: "/item/y", b: {}})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("ignores unresolved parameter via GET", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -101,9 +101,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.url})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item/:x"}).map(function(data) {
|
||||
o(data).deepEquals({a: "/item/:x"})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("ignores unresolved parameter via POST", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -111,9 +111,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({a: request.url})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item/:x"}).map(function(data) {
|
||||
o(data).deepEquals({a: "/item/:x"})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("type parameter works for Array responses", function(done) {
|
||||
var Entity = function(args) {
|
||||
|
|
@ -125,9 +125,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify([{id: 1}, {id: 2}, {id: 3}])}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", type: Entity}).map(function(data) {
|
||||
o(data).deepEquals([{_id: 1}, {_id: 2}, {_id: 3}])
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("type parameter works for Object responses", function(done) {
|
||||
var Entity = function(args) {
|
||||
|
|
@ -139,9 +139,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({id: 1})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", type: Entity}).map(function(data) {
|
||||
o(data).deepEquals({_id: 1})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("serialize parameter works in GET", function(done) {
|
||||
var serialize = function(data) {
|
||||
|
|
@ -153,9 +153,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({body: request.query})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).map(function(data) {
|
||||
o(data.body).equals("?id=1")
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("serialize parameter works in POST", function(done) {
|
||||
var serialize = function(data) {
|
||||
|
|
@ -167,9 +167,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({body: request.body})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).map(function(data) {
|
||||
o(data.body).equals("id=1")
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("deserialize parameter works in GET", function(done) {
|
||||
var deserialize = function(data) {
|
||||
|
|
@ -181,9 +181,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({test: 123})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", deserialize: deserialize}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", deserialize: deserialize}).map(function(data) {
|
||||
o(data).equals("{\"test\":123}")
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("deserialize parameter works in POST", function(done) {
|
||||
var deserialize = function(data) {
|
||||
|
|
@ -195,9 +195,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: JSON.stringify({test: 123})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", deserialize: deserialize}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item", deserialize: deserialize}).map(function(data) {
|
||||
o(data).equals("{\"test\":123}")
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("extract parameter works in GET", function(done) {
|
||||
var extract = function(data) {
|
||||
|
|
@ -209,9 +209,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: ""}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item", extract: extract}).then(function(data) {
|
||||
xhr({method: "GET", url: "/item", extract: extract}).map(function(data) {
|
||||
o(data).deepEquals({test: 123})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("extract parameter works in POST", function(done) {
|
||||
var extract = function(data) {
|
||||
|
|
@ -223,9 +223,9 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: ""}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", extract: extract}).then(function(data) {
|
||||
xhr({method: "POST", url: "/item", extract: extract}).map(function(data) {
|
||||
o(data).deepEquals({test: 123})
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("config parameter works", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -233,7 +233,7 @@ o.spec("xhr", function() {
|
|||
return {status: 200, responseText: ""}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", config: config}).then(done)
|
||||
xhr({method: "POST", url: "/item", config: config}).map(done)
|
||||
|
||||
function config(xhr) {
|
||||
o(typeof xhr.setRequestHeader).equals("function")
|
||||
|
|
@ -251,7 +251,7 @@ o.spec("xhr", function() {
|
|||
})
|
||||
xhr({method: "GET", url: "/item"}).catch(function(e) {
|
||||
o(e.message).equals(JSON.stringify({error: "error"}))
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
o("rejects on non-JSON server error", function(done) {
|
||||
mock.$defineRoutes({
|
||||
|
|
@ -261,7 +261,7 @@ o.spec("xhr", function() {
|
|||
})
|
||||
xhr({method: "GET", url: "/item"}).catch(function(e) {
|
||||
o(e.message).equals("error")
|
||||
}).then(done)
|
||||
}).map(done)
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue