by jey · 19y ago · 0 comments
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.