streams implementation

This commit is contained in:
Leo Horie 2016-06-20 09:21:25 -04:00
parent 94a8be4fca
commit b9ce90765d
12 changed files with 1104 additions and 167 deletions

View file

@ -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}
}