5.4. Summary

In this chapter, you learned about the challenges in managing Ajax requests from a single application. The two-connection HTTP 1.1 limit was discussed as it pertains to XHR objects, and a discussion of why this limitation is important followed.

Next, an alternate approach to create XHR objects was discussed, involving developer-defined prioritizations to ensure that important requests are executed before lower priority communication takes place. You were led through the creation of a priority queue data structure in JavaScript, making use of the Array object's built-in sort() method for prioritizing values in the queue. This generic PriorityQueue object became the basis for a request management object.

With the basic data structure created, you began creating the RequestManager object, which uses the priority queue to determine the requests to execute next. The RequestManager object expects request description objects to be provided that describe the request that should take place. It then uses this description to make the request at an appropriate time, without tying functions to XHR objects (which can cause memory leaks).

Four different callback functions were made available on each request description object, one for success (HTTP code in the 200–299 range), one for "not modified" (HTTP code 304), one for "failure" (all other codes), and one to call if the request is canceled before being sent. Each of these callback functions can be a standalone function or an object method whose scope can be provided using the scope property.

Last, you learned how to implement an age-based promotion system so that lower-priority requests will be ensured of execution. This works by automatically promoting requests to the next priority after they've been in the queue for a specified amount of time. By adding this to the RequestManager object, you now have a robust Ajax connection management object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset