Merge branch 'rewrite' into array-isArray
This commit is contained in:
commit
1050ade7c8
73 changed files with 2011 additions and 3136 deletions
|
|
@ -5,33 +5,40 @@ var buildQueryString = require("../querystring/build")
|
|||
module.exports = function($window, Promise) {
|
||||
var callbackCount = 0
|
||||
|
||||
var count = 0
|
||||
var oncompletion
|
||||
function setCompletionCallback(callback) {oncompletion = callback}
|
||||
function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()}
|
||||
function finalizer() {
|
||||
var count = 0
|
||||
function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()}
|
||||
|
||||
function finalize(promise) {
|
||||
var then = promise.then
|
||||
promise.then = function() {
|
||||
count++
|
||||
var next = then.apply(promise, arguments)
|
||||
next.then(complete, function(e) {
|
||||
complete()
|
||||
throw e
|
||||
})
|
||||
return finalize(next)
|
||||
return function finalize(promise) {
|
||||
var then = promise.then
|
||||
promise.then = function() {
|
||||
count++
|
||||
var next = then.apply(promise, arguments)
|
||||
next.then(complete, function(e) {
|
||||
complete()
|
||||
throw e
|
||||
})
|
||||
return finalize(next)
|
||||
}
|
||||
return promise
|
||||
}
|
||||
return promise
|
||||
}
|
||||
function normalize(args, extra) {
|
||||
if (typeof args === "string") {
|
||||
var url = args
|
||||
args = extra || {}
|
||||
if (args.url == null) args.url = url
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
function request(args, extra) {
|
||||
return finalize(new Promise(function(resolve, reject) {
|
||||
if (typeof args === "string") {
|
||||
var url = args
|
||||
args = extra || {}
|
||||
if (args.url == null) args.url = url
|
||||
}
|
||||
var finalize = finalizer()
|
||||
args = normalize(args, extra)
|
||||
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
if (args.method == null) args.method = "GET"
|
||||
args.method = args.method.toUpperCase()
|
||||
|
||||
|
|
@ -79,11 +86,15 @@ module.exports = function($window, Promise) {
|
|||
|
||||
if (useBody && (args.data != null)) xhr.send(args.data)
|
||||
else xhr.send()
|
||||
}))
|
||||
})
|
||||
return args.background === true ? promise : finalize(promise)
|
||||
}
|
||||
|
||||
function jsonp(args) {
|
||||
return finalize(new Promise(function(resolve, reject) {
|
||||
function jsonp(args, extra) {
|
||||
var finalize = finalizer()
|
||||
args = normalize(args, extra)
|
||||
|
||||
var promise = 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) {
|
||||
|
|
@ -101,7 +112,8 @@ module.exports = function($window, Promise) {
|
|||
args.data[args.callbackKey || "callback"] = callbackName
|
||||
script.src = assemble(args.url, args.data)
|
||||
$window.document.documentElement.appendChild(script)
|
||||
}))
|
||||
})
|
||||
return args.background === true? promise : finalize(promise)
|
||||
}
|
||||
|
||||
function interpolate(url, data) {
|
||||
|
|
|
|||
|
|
@ -7,10 +7,13 @@ var Promise = require("../../promise/promise")
|
|||
var parseQueryString = require("../../querystring/parse")
|
||||
|
||||
o.spec("jsonp", function() {
|
||||
var mock, jsonp, spy
|
||||
var mock, jsonp, spy, complete
|
||||
o.beforeEach(function() {
|
||||
mock = xhrMock()
|
||||
jsonp = new Request(mock, Promise).jsonp
|
||||
var requestService = Request(mock, Promise)
|
||||
jsonp = requestService.jsonp
|
||||
complete = o.spy()
|
||||
requestService.setCompletionCallback(complete)
|
||||
})
|
||||
|
||||
o("works", function(done) {
|
||||
|
|
@ -24,6 +27,20 @@ o.spec("jsonp", function() {
|
|||
o(data).deepEquals({a: 1})
|
||||
}).then(done)
|
||||
})
|
||||
o("first argument can be a string aliasing url property", function(done){
|
||||
var s = new Date
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify({a: 1}) + ")"}
|
||||
}
|
||||
})
|
||||
jsonp("/item").then(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).then(function() {
|
||||
done()
|
||||
})
|
||||
})
|
||||
o("works w/ other querystring params", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
|
|
@ -47,6 +64,64 @@ o.spec("jsonp", function() {
|
|||
o(data).deepEquals({a: 2})
|
||||
}).then(done)
|
||||
})
|
||||
o("works w/ custom callbackKey", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["cb"] + "(" + JSON.stringify({a: 2}) + ")"}
|
||||
}
|
||||
})
|
||||
jsonp({url: "/item", callbackKey: "cb"}).then(function(data) {
|
||||
o(data).deepEquals({a: 2})
|
||||
}).then(done)
|
||||
})
|
||||
o("requests don't block each other", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "([])"}
|
||||
}
|
||||
})
|
||||
jsonp("/item").then(function() {
|
||||
return jsonp("/item")
|
||||
})
|
||||
jsonp("/item").then(function() {
|
||||
return jsonp("/item")
|
||||
})
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(4)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
o("requests trigger finally once with a chained then", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "([])"}
|
||||
}
|
||||
})
|
||||
var promise = jsonp("/item")
|
||||
promise.then(function() {}).then(function() {})
|
||||
promise.then(function() {}).then(function() {})
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(1)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
o("requests does not trigger finally when background: true", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
var queryData = parseQueryString(request.query)
|
||||
return {status: 200, responseText: queryData["callback"] + "([])"}
|
||||
}
|
||||
})
|
||||
var promise = jsonp("/item", {background: true}).then(function() {})
|
||||
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(0)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
o("handles error", function(done) {
|
||||
jsonp({url: "/item", callbackKey: "cb"}).catch(function(e) {
|
||||
o(e.message).equals("JSONP request failed")
|
||||
|
|
|
|||
|
|
@ -6,10 +6,13 @@ var Request = require("../../request/request")
|
|||
var Promise = require("../../promise/promise")
|
||||
|
||||
o.spec("xhr", function() {
|
||||
var mock, xhr
|
||||
var mock, xhr, complete
|
||||
o.beforeEach(function() {
|
||||
mock = xhrMock()
|
||||
xhr = new Request(mock, Promise).request
|
||||
var requestService = Request(mock, Promise)
|
||||
xhr = requestService.request
|
||||
complete = o.spy()
|
||||
requestService.setCompletionCallback(complete)
|
||||
})
|
||||
|
||||
o.spec("success", function() {
|
||||
|
|
@ -295,6 +298,50 @@ o.spec("xhr", function() {
|
|||
o(typeof xhr.send).equals("function")
|
||||
}
|
||||
})
|
||||
o("requests don't block each other", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
return {status: 200, responseText: "[]"}
|
||||
}
|
||||
})
|
||||
xhr("/item").then(function() {
|
||||
return xhr("/item")
|
||||
})
|
||||
xhr("/item").then(function() {
|
||||
return xhr("/item")
|
||||
})
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(4)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
o("requests trigger finally once with a chained then", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
return {status: 200, responseText: "[]"}
|
||||
}
|
||||
})
|
||||
var promise = xhr("/item")
|
||||
promise.then(function() {}).then(function() {})
|
||||
promise.then(function() {}).then(function() {})
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(1)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
o("requests does not trigger finally when background: true", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function(request) {
|
||||
return {status: 200, responseText: "[]"}
|
||||
}
|
||||
})
|
||||
var promise = xhr("/item", {background: true}).then(function() {})
|
||||
|
||||
setTimeout(function() {
|
||||
o(complete.callCount).equals(0)
|
||||
done()
|
||||
}, 20)
|
||||
})
|
||||
})
|
||||
o.spec("failure", function() {
|
||||
o("rejects on server error", function(done) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue