Last active
August 29, 2015 14:05
-
-
Save sTiLL-iLL/53629d86992b0dcf0ed4 to your computer and use it in GitHub Desktop.
Object Pool microframework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// object pool | |
var ObjectPool = function (klas) { | |
this.clas = klas; | |
this.status = {}; | |
this.flush(); | |
this.pool = []; | |
}; | |
ObjectPool.prototype.getObject = function getObject() { | |
var obj; | |
if (pool.length == 0) { | |
obj = new clas(); | |
status.totalAssigned++; | |
} | |
else { | |
obj = pool.pop(); | |
status.totalReady--; | |
} | |
obj.init.apply(obj, argz); | |
return obj; | |
}; | |
ObjectPool.prototype.releaseObject = function (obj) { | |
var l; | |
pool.push(obj); | |
status.totalReady++; | |
for (l in obj) { | |
delete obj[l]; | |
} | |
obj.init.call(obj); | |
}; | |
ObjectPool.prototype.flush = function (clas) { | |
pool = []; | |
var busyLst = status.totalAssigned - status.totalReady; | |
flush(busyLst); | |
}; | |
ObjectPool.prototype.wipe = function (busy) { | |
status.totalAssigned = busy || 0; | |
status.totalReady = 0; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (context) { | |
var createKontxt = function (nameStr, baseLine) { | |
var x, j, r, baseKnTxt, name; | |
var wdgts = nameStr.split('.'); | |
j = baseKnTxt = baseLine ? baseLine : (Function('return this')()); | |
for (x = 0; x < wdgts.length; x++) { | |
name = wdgts[x]; | |
r = typeof j[name]; | |
if (r == "undefined") { | |
j[name] = {}; | |
} | |
else if (r != "object") { | |
throw 'domain "' + name + '" not an object'; | |
} | |
j = j[name]; | |
} | |
return baseKnTxt[wdgts[0]]; | |
} | |
createKontxt('Kontxt', context); | |
Kntxt.createKontxt = createKontxt; | |
})(Function('return this')()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment