mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
added outstanding request queue
This commit is contained in:
parent
0d41c86522
commit
aedf12f25e
3 changed files with 77 additions and 27 deletions
|
|
@ -8,6 +8,7 @@ function Browser(location, document) {
|
|||
this.urlListeners = [];
|
||||
this.hoverListener = noop;
|
||||
this.isMock = false;
|
||||
this.outstandingRequests = { count: 0, callbacks:[]};
|
||||
|
||||
this.XHR = window.XMLHttpRequest || function () {
|
||||
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
|
||||
|
|
@ -57,16 +58,42 @@ Browser.prototype = {
|
|||
callback = post;
|
||||
post = null;
|
||||
}
|
||||
var xhr = new this.XHR();
|
||||
var xhr = new this.XHR(),
|
||||
self = this;
|
||||
xhr.open(method, url, true);
|
||||
this.outstandingRequests.count ++;
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState == 4) {
|
||||
callback(xhr.status || 200, xhr.responseText);
|
||||
try {
|
||||
callback(xhr.status || 200, xhr.responseText);
|
||||
} finally {
|
||||
self.outstandingRequests.count--;
|
||||
self.processRequestCallbacks();
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send(post || '');
|
||||
},
|
||||
|
||||
processRequestCallbacks: function(){
|
||||
if (this.outstandingRequests.count === 0) {
|
||||
while(this.outstandingRequests.callbacks.length) {
|
||||
try {
|
||||
this.outstandingRequests.callbacks.pop()();
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
notifyWhenNoOutstandingRequests: function(callback){
|
||||
if (this.outstandingRequests.count === 0) {
|
||||
callback();
|
||||
} else {
|
||||
this.outstandingRequests.callbacks.push(callback);
|
||||
}
|
||||
},
|
||||
|
||||
watchUrl: function(fn){
|
||||
this.urlListeners.push(fn);
|
||||
},
|
||||
|
|
|
|||
48
test/BrowserSpecs.js
Normal file
48
test/BrowserSpecs.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
describe('browser', function(){
|
||||
|
||||
var browser, location;
|
||||
|
||||
beforeEach(function(){
|
||||
location = {href:"http://server", hash:""};
|
||||
browser = new Browser(location, {});
|
||||
browser.setTimeout = noop;
|
||||
});
|
||||
|
||||
it('should watch url', function(){
|
||||
browser.delay = 1;
|
||||
expectAsserts(2);
|
||||
browser.watchUrl(function(url){
|
||||
assertEquals('http://getangular.test', url);
|
||||
});
|
||||
browser.setTimeout = function(fn, delay){
|
||||
assertEquals(1, delay);
|
||||
location.href = "http://getangular.test";
|
||||
browser.setTimeout = function(fn, delay) {};
|
||||
fn();
|
||||
};
|
||||
browser.startUrlWatcher();
|
||||
});
|
||||
|
||||
describe('outstading requests', function(){
|
||||
it('should process callbacks immedietly with no outstanding requests', function(){
|
||||
var callback = jasmine.createSpy('callback');
|
||||
browser.notifyWhenNoOutstandingRequests(callback);
|
||||
expect(callback).wasCalled();
|
||||
});
|
||||
|
||||
it('should queue callbacks with outstanding requests', function(){
|
||||
var callback = jasmine.createSpy('callback');
|
||||
browser.outstandingRequests.count = 1;
|
||||
browser.notifyWhenNoOutstandingRequests(callback);
|
||||
expect(callback).not.wasCalled();
|
||||
|
||||
browser.processRequestCallbacks();
|
||||
expect(callback).not.wasCalled();
|
||||
|
||||
browser.outstandingRequests.count = 0;
|
||||
browser.processRequestCallbacks();
|
||||
expect(callback).wasCalled();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
BrowserTest = TestCase('BrowserTest');
|
||||
|
||||
BrowserTest.prototype.testUrlWatcher = function () {
|
||||
expectAsserts(2);
|
||||
var location = {href:"http://server", hash:""};
|
||||
var watcher = new Browser(location, {});
|
||||
watcher.delay = 1;
|
||||
watcher.watchUrl(function(url){
|
||||
assertEquals('http://getangular.test', url);
|
||||
});
|
||||
watcher.setTimeout = function(fn, delay){
|
||||
assertEquals(1, delay);
|
||||
location.href = "http://getangular.test";
|
||||
watcher.setTimeout = function(fn, delay) {
|
||||
};
|
||||
fn();
|
||||
};
|
||||
watcher.startUrlWatcher();
|
||||
};
|
||||
|
||||
FunctionTest = TestCase("FunctionTest");
|
||||
|
||||
FunctionTest.prototype.testEscapeHtml = function () {
|
||||
assertEquals("<div>&amp;</div>", escapeHtml('<div>&</div>'));
|
||||
};
|
||||
Loading…
Reference in a new issue