fix request redrawing semantics
This commit is contained in:
parent
f54f15d4ce
commit
0080d1dff0
2 changed files with 64 additions and 24 deletions
|
|
@ -5,33 +5,36 @@ var buildQueryString = require("../querystring/build")
|
||||||
module.exports = function($window, Promise) {
|
module.exports = function($window, Promise) {
|
||||||
var callbackCount = 0
|
var callbackCount = 0
|
||||||
|
|
||||||
var count = 0
|
|
||||||
var oncompletion
|
var oncompletion
|
||||||
function setCompletionCallback(callback) {oncompletion = callback}
|
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) {
|
return function finalize(promise) {
|
||||||
var then = promise.then
|
var then = promise.then, catcher = promise.catch
|
||||||
promise.then = function() {
|
promise.then = function() {
|
||||||
count++
|
count++
|
||||||
var next = then.apply(promise, arguments)
|
var next = then.apply(promise, arguments)
|
||||||
next.then(complete, function(e) {
|
next.then(complete, function(e) {
|
||||||
complete()
|
complete()
|
||||||
throw e
|
throw e
|
||||||
})
|
})
|
||||||
return finalize(next)
|
return finalize(next)
|
||||||
|
}
|
||||||
|
return promise
|
||||||
}
|
}
|
||||||
return promise
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function request(args, extra) {
|
function request(args, extra) {
|
||||||
return finalize(new Promise(function(resolve, reject) {
|
var finalize = finalizer()
|
||||||
if (typeof args === "string") {
|
if (typeof args === "string") {
|
||||||
var url = args
|
var url = args
|
||||||
args = extra || {}
|
args = extra || {}
|
||||||
if (args.url == null) args.url = url
|
if (args.url == null) args.url = url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
if (args.method == null) args.method = "GET"
|
if (args.method == null) args.method = "GET"
|
||||||
args.method = args.method.toUpperCase()
|
args.method = args.method.toUpperCase()
|
||||||
|
|
||||||
|
|
@ -79,11 +82,13 @@ module.exports = function($window, Promise) {
|
||||||
|
|
||||||
if (useBody && (args.data != null)) xhr.send(args.data)
|
if (useBody && (args.data != null)) xhr.send(args.data)
|
||||||
else xhr.send()
|
else xhr.send()
|
||||||
}))
|
})
|
||||||
|
return args.redraw === false ? promise : finalize(promise)
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonp(args) {
|
function jsonp(args) {
|
||||||
return finalize(new Promise(function(resolve, reject) {
|
var finalize = finalizer()
|
||||||
|
var promise = new Promise(function(resolve, reject) {
|
||||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||||
var script = $window.document.createElement("script")
|
var script = $window.document.createElement("script")
|
||||||
$window[callbackName] = function(data) {
|
$window[callbackName] = function(data) {
|
||||||
|
|
@ -101,7 +106,8 @@ module.exports = function($window, Promise) {
|
||||||
args.data[args.callbackKey || "callback"] = callbackName
|
args.data[args.callbackKey || "callback"] = callbackName
|
||||||
script.src = assemble(args.url, args.data)
|
script.src = assemble(args.url, args.data)
|
||||||
$window.document.documentElement.appendChild(script)
|
$window.document.documentElement.appendChild(script)
|
||||||
}))
|
})
|
||||||
|
return args.redraw === false? promise : finalize(promise)
|
||||||
}
|
}
|
||||||
|
|
||||||
function interpolate(url, data) {
|
function interpolate(url, data) {
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,13 @@ var Request = require("../../request/request")
|
||||||
var Promise = require("../../promise/promise")
|
var Promise = require("../../promise/promise")
|
||||||
|
|
||||||
o.spec("xhr", function() {
|
o.spec("xhr", function() {
|
||||||
var mock, xhr
|
var mock, xhr, complete
|
||||||
o.beforeEach(function() {
|
o.beforeEach(function() {
|
||||||
mock = xhrMock()
|
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() {
|
o.spec("success", function() {
|
||||||
|
|
@ -295,6 +298,37 @@ o.spec("xhr", function() {
|
||||||
o(typeof xhr.send).equals("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.spec("failure", function() {
|
o.spec("failure", function() {
|
||||||
o("rejects on server error", function(done) {
|
o("rejects on server error", function(done) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue