3

In my jQuery web app I'm using events pretty much.
My component for doing ajax client requests (the name is "comm") to the server has a queue.
This queue is similar to a Backbone.js collection.

So every time I put a new request object into the collection of "comm", the comm object gets an "add" event and the "worker" method of "comm" looks, if there is currently an ongoing request. If there is no one, the worker method processes the request and finally, when everything works fine, triggers a "complete" event on the request object.

In this comm component only one request at a time is possible. If I add an request object to "comm" and the "add" event gets triggered and the worker method sees, that there is already a request, than nothing happens.

My question is: In this situation, what is the best approach for handling this unprocessed request object IN REGARD TO "AN EVENT INFRASTRUCTURE"?

Until now I had 2 different approaches:

1) I could do the following: If my ongoing request is finished, the worker method can check if there is an unprocessed request object in the collection and process it. This is simple, no event is envolved.

2) Or something different: If the worker method starts due to an "add" event and sees, that there is already an ongoing request, I could implement something like: "I can not respond adeqately to this add event, I place you back. Please trigger yourself in 200 milliseconds again, maybe I have time then."

Maybe somebody already had a similar problem and had a very good solution to this?

1
  • @MarcellFülöp: That is very good. Why don't you make an answer? Commented Feb 7, 2013 at 10:30

2 Answers 2

2

I think a good approach to this problem would be to implement a "next" event that is fired on the queue. Each time a "complete" event is triggered, it should fire the next event on the queue which then would see if there's any unprocessed requests and if so picks one according to your logic (I assume FIFO)

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correct, you want to chain requests or other asynchronous events? Then the promises pattern is an elegant solution.

JQuery already implements it with the Deferred object.

With JQuery 1.8+ request chaining can be as simple as:

$.ajax(...).then( function() { return $.ajax(...); } ).then( function() { return $.ajax(...); } ); 

But you also can build a more dynamic and complex architecture around it. For example you could implement your queue such that it always stores the last unresolved promise so that you can attach new handlers to it while it is already active.

Generic Example:

var Queue = function() { // start with resolved promise var promise = $.Deferred().resolve(); this.add = function(handler) { // chain and replace latest with next promise promise = promise.then(function() { var deferred = $.Deferred(); handler(deferred); return deferred; }).promise(); } }; var q = new Queue(); q.add(function(deferred) { setTimeout(function() { alert('handler 1'); deferred.resolve(); }, 1000); }); q.add(function(deferred) { setTimeout(function() { alert('handler 2'); deferred.resolve(); }, 1000); }); 

JSFiddle demo

Here the handlers get a deferred object as parameter and are responsible to resolve (or reject) it if they are "done". Another, more flexible, possibility would be that the handlers have to create the deferred object by themselves and return its promise, this way you also can use other promises like those which are returned by $.ajax

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.