AJAX best practices?
1–10 of 20 posts
Re: AJAX best practices?
#2Re: AJAX best practices?
#3 next_request_id = 0;
next_response_id = 0;
// the following code assumes there's a PriorityQueue class implementing a
// priority queue with push(object, priority) and pop() operations
response_queue = new PriorityQueue();
function send_request(request) {
request.request_id = next_request_id;
next_request_id++;
ajax_send(request);
}
function receive_response(response) {
response_queue.push(response, response.request_id);
while(!response_queue.empty()
&& response_queue.top().request_id == next_response_id) {
handle_response(response_queue.pop());
next_response_id++;
}
}
The above code isn't very robust as it doesn't deal with requests that die in transit and never return a response, etc.
Re: AJAX best practices?
#4One way to handle it is to design your system so that it doesn't matter if the requests come back out of order. Could you give an example of where order matters, so we can work on designing it so that order doesn't matter?
Re: AJAX best practices?
#5What's the specific problem? Why do you need the results to be ordered? If you really do need to order the results, just attach a sequential ID to each request, then hold a priority queue of results sorted by request ID. Pseudo-code follows: next_request_id = 0; next_response_id = 0; // the following code assumes there's a PriorityQueue class implementing a // priority queue with push(object, priority) and pop() oper…
Re: AJAX best practices?
#6One way to handle it is to design your system so that it doesn't matter if the requests come back out of order. Could you give an example of where order matters, so we can work on designing it so that order doesn't matter?
I'd like to implement a note-taking system, with an auto-save feature (say, set by a timer). If the requests that go out to the server are received out of order, the wrong state will be auto-saved.
Re: AJAX best practices?
#7Earlier quoted context omitted.
I'd like to implement a note-taking system, with an auto-save feature (say, set by a timer). If the requests that go out to the server are received out of order, the wrong state will be auto-saved.
Don't save again until the first save has been registered. If it times out, try again.
Re: AJAX best practices?
#8What's the specific problem? Why do you need the results to be ordered? If you really do need to order the results, just attach a sequential ID to each request, then hold a priority queue of results sorted by request ID. Pseudo-code follows: next_request_id = 0; next_response_id = 0; // the following code assumes there's a PriorityQueue class implementing a // priority queue with push(object, priority) and pop() oper…
Right - this is what I'm about to try to implement (and I think I'd trash the entire queue if a response isn't returned within some timeout limit and display a subsequent error message). I don't know if this is the best practice though and as this seems like a pretty generic AJAX problem, it feels like there should be a pretty generic solution...
Re: AJAX best practices?
#9Earlier quoted context omitted.
Don't save again until the first save has been registered. If it times out, try again.
Right - so this would amount to creating queue? Nevermind, I see what you're saying... This is a very specific instance of something pretty general though. Lets say the user edits a note and then deletes it. Do I have the delete action wait for any saves that went out complete first? As the system gets more and more complex it would be something of a pain to try to work out dependencies every time I want to add a new…
Re: AJAX best practices?
#10Earlier quoted context omitted.
Right - so this would amount to creating queue? Nevermind, I see what you're saying... This is a very specific instance of something pretty general though. Lets say the user edits a note and then deletes it. Do I have the delete action wait for any saves that went out complete first? As the system gets more and more complex it would be something of a pain to try to work out dependencies every time I want to add a new…
Yes, queue up the deltas, then only send the full set of deltas every time the timer fires. This will also prevent you from hammering your server with a stream of requests backing up.