Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds SC.Request.manager functions isPending and isInFlight.

Allows checking if a response is in the pending or inflight queues.
  • Loading branch information...
commit acd6478ae8133ac8a97db5d62b0ebc4001499b5b 1 parent 99e1086
@fairbanksg fairbanksg authored dcporter committed
View
23 frameworks/ajax/system/request.js
@@ -639,7 +639,7 @@ SC.Request.manager = SC.Object.create(
Cancels a specific request. If the request is pending it will simply
be removed. Otherwise it will actually be cancelled.
- @param {Object} response a response object
+ @param {SC.Response} response a response object
@returns {Boolean} YES if cancelled
*/
cancel: function(response) {
@@ -717,8 +717,29 @@ SC.Request.manager = SC.Object.create(
the transport from the queue and kicks off the next one.
*/
transportDidClose: function(response) {
+ this.get('pending').removeObject(response);
this.get('inflight').removeObject(response);
this.fireRequestIfNeeded();
+ },
+
+ /**
+ Checks if the response is in the pending queue.
+
+ @param {SC.Response} response a response object
+ @return {Boolean} is response in pending queue
+ */
+ isPending: function(response) {
+ return this.get('pending').contains(response);
+ },
+
+ /**
+ Checks if the response is in the inflight queue.
+
+ @param {SC.Response} response a response object
+ @return {Boolean} is response in inflight queue
+ */
+ isInFlight: function(response) {
+ return this.get('inflight').contains(response);
}
});
View
78 frameworks/ajax/tests/system/request.js
@@ -618,3 +618,81 @@ test("Test manager.cancelAll.", function() {
equals(manager.getPath('inflight.length'), 0, "There must be 0 inflight requests".fmt(max));
equals(manager.getPath('pending.length'), 0, "There must be 0 pending requests".fmt(max));
});
+
+test("Test responses moving between pending and inflight states", function() {
+ var prevMaxRequests = SC.Request.manager.get('maxRequests'),
+ request2 = SC.Request.getUrl(url),
+ response,
+ response2;
+
+ SC.Request.manager.set('maxRequests', 1);
+
+ request.notify('loadstart', function (evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is no longer pending.');
+ ok(SC.Request.manager.isInFlight(response), 'First request is now inflight.');
+ ok(SC.Request.manager.isPending(response2), 'Second request is still pending.');
+ ok(!SC.Request.manager.isInFlight(response2), 'Second request is not inflight.');
+ });
+
+ request.notify('progress', this, function(evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(SC.Request.manager.isInFlight(response), 'First request is still inflight.');
+ ok(SC.Request.manager.isPending(response2), 'Second request is still pending.');
+ ok(!SC.Request.manager.isInFlight(response2), 'Second request is not inflight.');
+ });
+
+ request.notify('loadend', this, function (evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(SC.Request.manager.isInFlight(response), 'First request is still inflight.');
+ ok(SC.Request.manager.isPending(response2), 'Second request is still pending.');
+ ok(!SC.Request.manager.isInFlight(response2), 'Second request is not inflight.');
+ });
+
+ request.notify(this, function (evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(SC.Request.manager.isInFlight(response), 'First request is still inflight.');
+ ok(SC.Request.manager.isPending(response2), 'Second request is still pending.');
+ ok(!SC.Request.manager.isInFlight(response2), 'Second request is not inflight.');
+ });
+
+ request2.notify('loadstart', this, function(evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(!SC.Request.manager.isInFlight(response), 'First request is no longer inflight.');
+ ok(!SC.Request.manager.isPending(response2), 'Second request is no longer pending.');
+ ok(SC.Request.manager.isInFlight(response2), 'Second request is now inflight.');
+ });
+
+ request2.notify('progress', this, function(evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(!SC.Request.manager.isInFlight(response), 'First request is not inflight.');
+ ok(!SC.Request.manager.isPending(response2), 'Second request is not pending.');
+ ok(SC.Request.manager.isInFlight(response2), 'Second request is still inflight.');
+ });
+
+ request2.notify('loadend', this, function(evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(!SC.Request.manager.isInFlight(response), 'First request is not inflight.');
+ ok(!SC.Request.manager.isPending(response2), 'Second request is not pending.');
+ ok(SC.Request.manager.isInFlight(response2), 'Second request is still inflight.');
+ });
+
+ request2.notify(this, function (evt) {
+ ok(!SC.Request.manager.isPending(response), 'First request is not pending.');
+ ok(!SC.Request.manager.isInFlight(response), 'First request is not inflight.');
+ ok(!SC.Request.manager.isPending(response2), 'Second request is not pending.');
+ ok(SC.Request.manager.isInFlight(response2), 'Second request is still inflight.');
+
+ SC.Request.manager.set('maxRequests', prevMaxRequests);
+ window.start();
+ });
+
+ response = request.send();
+ response2 = request2.send();
+
+ ok(SC.Request.manager.isPending(response), 'First request is pending.');
+ ok(!SC.Request.manager.isInFlight(response), 'First request is not inflight.');
+ ok(SC.Request.manager.isPending(response2), 'Second request is pending.');
+ ok(!SC.Request.manager.isInFlight(response2), 'Second request is not inflight.');
+
+ stop();
+});
Please sign in to comment.
Something went wrong with that request. Please try again.