One page multiple XMLHttpRequest object _javascript skill

Source: Internet
Author: User
I just posted a post in AJACN about providing an object pool for XMLHttpRequest. May be able to help you, posted as follows:

On the internet, some comrades have referred to providing an object pool for Ajax XMLHttpRequest, as well as reading their implementation code. The feeling is not particularly ideal, so the idea of imitating the objectpool in the Commons of Apache wrote a simple JavaScript version.
Hope advice:


Code
function Objectpool (poolableobjectfactory) {
This._poolableobjectfactory = poolableobjectfactory;
This._idlepool = [];
This._activepool = [];
}
Leases an object from the object pool, and if no idle objects are currently available, create a poolableobjectfactory through the
Since it is borrowed, use the finish remember must also Oh!
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 currently activated objects
ObjectPool.prototype.getNumActive = function () {
return this._activepool.length;
}
Returns the number of current idle objects
ObjectPool.prototype.getNumIdle = function () {
return this._idlepool.length;
}
Destroying an object pool and all objects in it
If the object in the object pool needs to be destructor. The Destroyobject method in Poolableobjectfactory must be implemented, while ensuring that the Objectpool destroy method is invoked when needed (for example, in the Unload event of window).
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;
}

The poolableobjectfactory of the constructor parameters of Objectpool in the above code is as follows:

Code
Note: This is just a description, not real code!
var poolableobjectfactory = {
Makeobject:function () {},//Create a new object. (Must be declared)

Activateobject:function (object) {},//The method that fires when an object is activated (that is, when it is lent). (optional)

Passivateobject:function (object) {},//The method that is triggered when an object is deactivated (that is, when it is returned). (optional)

Destroyobject:function (object) {}//destroys an object. (optional)
};

A rough example of the XMLHttpRequest creation process:

Code
To declare a XMLHttpRequest creation factory
var factory = {
Makeobject:function () {
Creating Xmlhttprequset Objects
Note: The creation method here is not strong enough to learn!
if (window. ActiveXObject) {
return new ActiveXObject ("Microsoft.XMLHTTP");
}
else {
return new XMLHttpRequest ();
}
},
Passivateobject:function (XHR) {
Resetting Xmlhttprequset objects
Xhr.onreadystatechange = {};
Xhr.abort ();
}
};
var pool = new Objectpool (factory); Creating object Pools
// ......
var xhr = Pool.borrowobject (); Get a XMLHttpRequest Object
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
// ......
Pool.returnobject (XHR); Return XMLHttpRequest Object
}
};
Xhr.open (method, URL, true);
// ......

Finally, attach the Jsunit test case:

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.