Browser-Server Dialogue - Real-World Examples of Call Tracking
(Page 3 of 4 )
Ajax Client Engine (ACE) library
Li Shen's Ajax Client Engine (ACE) (http://www.lishen.name/) uses Call Tracking to ease development and harden the application in production. Among its many features are several related to Call Tracking:
- Long calls are timed out.
- Changes to XMLHttpRequest's response state are logged.
- A service can be periodically polled.
- The caller can declare exactly when the callback method should be invoked.
AjaxCaller library
The AjaxCaller library (http://ajaxify.com/run/Lib/js/ajaxCaller.js), used throughout the AjaxPatterns demos for Web Remoting, uses Call Tracking to pool Calls, which wrap XMLHttpRequest objects.
libXmlRequest library
libXmlRequest (http://www.whitefrost.com/reference/2005/09/09/ libXmlRequest.html) is another XMLHttpRequest wrapper. It keeps XMLHttpRequest objects in a pool so they can be reused, and tracks the response status in order to manage the pool.
Code Example: Ajax Client Engine (ACE)
In this example, well look at ACE's internal handling of call timeouts. The library consists of Requester objects, which wrap XMLHttpRequest objects. As explained earlier in the Solution, the wrapped XMLHttpRequest objects are created upon construction. When the wrapper is invoked to make a call, it creates a timer to cancel the request if it takes too long. The timer ID is held as an attribute of the wrapper.
timeoutId = window.setTimeout(endRequest, request.callbackTimeout * 1000);
To track the call, the wrapper registers itself as a request handler:
function beginRequest()
{
...
requester.onreadystatechange = readystatechangeHandler;
...
}
Its handler is therefore called upon each change, and in the case of a complete call, cancels the timer:
function readystatechangeHandler() ...
if (requester.readyState == Ace.ReadyState.Complete)
{
...
if (requester.status == Ace.Status.OK)
{
...
if (timeoutId)
{
window.clearTimeout(timeoutId);
timeoutId = undefined;
}
...
}
...
}
Next: Call Tracking Alternatives >>
More JavaScript Articles
More By O'Reilly Media
|
This article is excerpted from chapter 10 of the book Ajax Design Patterns, written by Michael Mahemoff (O'Reilly, 2006; ISBN: 0596101805). Check it out today at your favorite bookstore. Buy this book now.
|
|