-
Notifications
You must be signed in to change notification settings - Fork 1
/
object-pool.min.js
2 lines (2 loc) · 2.46 KB
/
object-pool.min.js
1
2
(function(n){var i,t,u={boolean:!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},f=Array.isArray||function(n){return Object.prototype.toString.call(n)=="[object Array]"},r=function(n){return Object.prototype.toString.call(n)=="[object Function]"},e=function(n){return i=typeof n,!!(n&&u[i])},o=function(n){if(!e(n))return n;if(f(n))return n.slice();var i={};for(t in n)i[t]=n[t];return i};n.$Pool=function(){this._items=[],this._head=0},n.$Pool.prototype.get=function(){if(this._head===0)return null;this._head--;var n=this._items[this._head];return this._items.length=this._head,n},n.$Pool.prototype.release=function(n){this._items[this._head]=n,this._head++,this._items.length=this._head},n.$Pool.prototype.delete=function(n){this._head=Math.max(0,this._head-n),this._items.length=this._head},n.$Pool.prototype.count=function(){return this._head},n.$Generator=function(t,i){if(this.options=i||{},r(t)&&!this.options.functionObject?this.itemConstructor=t:(this.item=t,this.clone=this.options.clone||o),this.pool=new n.$Pool,this.options.data){for(this.tempValue in this.options.data)this.pool.release(this.tempValue);this.options.data=null}this.options.count&&this.create(this.options.count-this.pool.count())},n.$Generator.prototype.tryRegenerate=function(){this.tempValue=r(this.options.regenerate)?this.options.regenerate.apply(this):this.options.regenerate.valueOf(),this.create(this.tempValue)},n.$Generator.prototype.get=function(){return(this.tempItem=this.pool.get(),this.tempItem===null&&(this.options.regenerate&&(this.tryRegenerate(),this.tempItem=this.pool.get()),this.tempItem===null))?null:(this.options.init&&this.options.init.apply(this.tempItem,arguments),this.tempItem)},n.$Generator.prototype.release=function(n){this.options.clear&&this.options.clear.apply(n),this.pool.release(n)},n.$Generator.prototype.delete=function(n){this.tempCounter=n,n<1&&(this.tempCounter=this.pool.count()*n),this.tempCounter=Math.floor(this.tempCounter),this.pool.delete(this.tempCounter)},n.$Generator.prototype.create=function(n){for(this.tempCounter=n;this.tempCounter>0;)this.itemConstructor?this.pool.release(new this.itemConstructor):this.pool.release(this.clone(this.item)),this.tempCounter--},n.$Generator.prototype.count=function(){return this.pool.count()},n.generate=function(t,i){return new n.$Generator(t,i)},n.array=n.generate(function(){return[]},{clear:function(){this.length=0},count:60})})(typeof exports=="undefined"?this.ObjectPool={}:exports);
//# sourceMappingURL=object-pool.min.js.map