I just posted a post in ajacn about providing an object pool for XMLHttpRequest. It may help you. The post is as follows:
Some comrades mentioned on the Internet that they provided an object pool for Ajax XMLHttpRequest and read the implementation code they provided. It is not ideal, so I wrote a simple JavaScript version to imitate the ObjectPool idea in apache commons.
Hope you can advise:
Code
Function ObjectPool (poolableObjectFactory ){
This. _ poolableObjectFactory = poolableObjectFactory;
This. _ idlePool = [];
This. _ activePool = [];
}
// Rent an object from the object pool. If no idle object is available currently, create one through poolableObjectFactory
// Since it is borrowed, remember to pay it back when you use it!
ObjectPool. prototype. borrowObject = function (){
Var object = null;
Var idlePool = this. _ idlePool;
Var factory = this. _ poolableObjectFactory;
If (idlePool. length> 0 ){
Object = idlePool. pop ();
}
Else {
Object = factory. makeObject ();
}
If (object! = Null ){
This. _ activePool. push (object );
If (factory. activateObject ){
Factory. activateObject (object );
}
}
Return object;
}
// Return an object
ObjectPool. prototype. returnObject = function (object ){
Function indexOf (array, object ){
For (var I = 0; I <array. length; I ++ ){
If (array [I] = object) return I;
}
Return-1;
}
If (object! = Null ){
Var activePool = this. _ activePool;
Var factory = this. _ poolableObjectFactory;
Var I = indexOf (activePool, object );
If (I <0) return;
If (factory. passivateObject ){
Factory. passivateObject (object );
}
ActivePool. splice (I, 1 );
This. _ idlePool. push (object );
}
}
// Returns the number of active objects.
ObjectPool. prototype. getNumActive = function (){
Return this. _ activePool. length;
}
// Returns the number of idle objects.
ObjectPool. prototype. getNumIdle = function (){
Return this. _ idlePool. length;
}
// Destroy the object pool and all objects in it
// If the object in the object pool needs to be destructed. The destroyObject method in poolableObjectFactory must be implemented, and the destroy method of ObjectPool must be called as needed (such as the Window unload event ).
ObjectPool. prototype. destroy = function (){
Var factory = this. _ poolableObjectFactory;
Function returnObject (object ){
If (factory. passivateObject ){
Factory. passivateObject (object );
}
}
Function destroyObject (object ){
If (factory. destroyObject ){
Factory. destroyObject (object );
}
}
Var activePool = this. _ activePool;
For (var I = 0; I <activePool. length; I ++ ){
Var object = activePool [I];
ReturnObject (object );
DestroyObject (object );
}
Var idlePool = this. _ idlePool;
For (var I = 0; I <idlePool. length; I ++ ){
Var object = idlePool [I];
DestroyObject (object );
}
This. _ idlePool = null;
This. _ activePool = null;
This. _ poolableObjectFactory = null;
}
In the code above, the declaration of the ObjectPool constructor poolableObjectFactory is as follows:
Code
// Note: This is just a description, not the real code!
Var PoolableObjectFactory = {
MakeObject: function () {}, // create a new object. (Must be declared)
ActivateObject: function (object) {}, // method triggered when an object is activated (that is, when it is lent. (Optional)
PassivateObject: function (object) {}, // method triggered when an object is deactivated (that is, when it is returned. (Optional)
DestroyObject: function (object) {}// destroy an object. (Optional)
};
Simple Example of XMLHttpRequest creation process:
Code
// Declare the creation factory of XMLHttpRequest
Var factory = {
MakeObject: function (){
// Create an XMLHttpRequset object
// Note: The creation method here is not strong enough. Do not learn it!
If (window. ActiveXObject ){
Return new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else {
Return new XMLHttpRequest ();
}
},
PassivateObject: function (xhr ){
// Reset the XMLHttpRequset object
Xhr. onreadystatechange = {};
Xhr. abort ();
}
};
Var pool = new ObjectPool (factory); // create an object pool
//......
Var xhr = pool. borrowObject (); // get an XMLHttpRequest object
Xhr. onreadystatechange = function (){
If (xhr. readyState = 4 ){
//......
Pool. returnObject (xhr); // return the XMLHttpRequest object
}
};
Xhr. open (method, url, true );
//......
The following is a test case of jsUnit:
Code
Function test_pool (){
Var factory = {
Counter: 0,
MakeObject: function (){
Return {id: ++ this. counter };
},
ActivateObject: function (object ){
Object. activated = true;
},
PassivateObject: function (object ){
Object. activated = false;
},
DestroyObject: function (object ){
Object. destroyed = true;
}
};
Var pool = new ObjectPool (factory );
// BorrowObject object1
Var object1 = pool. borrowObject ();
AssertEquals (object1.id, 1 );
AssertTrue (object1.activated );
AssertEquals (factory. counter, 1 );
AssertEquals (pool. getNumActive (), 1 );
AssertEquals (pool. getNumIdle (), 0 );
// BorrowObject object2
Var object2 = pool. borrowObject ();
AssertEquals (object2.id, 2 );
AssertTrue (object2.activated );
AssertEquals (factory. counter, 2 );
AssertEquals (pool. getNumActive (), 2 );
AssertEquals (pool. getNumIdle (), 0 );
// BorrowObject object3
Var object3 = pool. borrowObject ();
AssertEquals (object3.id, 3 );
AssertTrue (object3.activated );
AssertEquals (factory. counter, 3 );
AssertEquals (pool. getNumActive (), 3 );
AssertEquals (pool. getNumIdle (), 0 );
// ReturnObject object2
Pool. returnObject (object2 );
AssertFalse (object2.activated );
AssertEquals (factory. counter, 3 );
AssertEquals (pool. getNumActive (), 2 );
AssertEquals (pool. getNumIdle (), 1 );
// ReturnObject object3
Pool. returnObject (object3 );
AssertFalse (object3.activated );
AssertEquals (pool. getNumActive (), 1 );
AssertEquals (pool. getNumIdle (), 2 );
// ReturnObject object1
Pool. returnObject (object1 );
AssertFalse (object1.activated );
AssertEquals (pool. getNumActive (), 0 );
AssertEquals (pool. getNumIdle (), 3 );
// Destroy the pool
Pool. destroy ();
AssertTrue (object1.destroyed );
AssertTrue (object2.destroyed );
AssertTrue (object3.destroyed );
}