Skip to content

Instantly share code, notes, and snippets.

@romeoh
Created July 30, 2013 04:58
Show Gist options
  • Save romeoh/6110342 to your computer and use it in GitHub Desktop.
Save romeoh/6110342 to your computer and use it in GitHub Desktop.
function map
<script type='text/javascript'>
var FuncPool = new function () {
this.buffer = new Object();
// create set key
this.createSetKey = function() {
var sKey = '';
while (true) {
sKey = 'set' + (Math.random() + '').split('.')[1];
if (this.buffer[sKey] == undefined) break;
}
return sKey;
}
this.addFunc = function(skey, func) {
if (this.buffer[skey] == undefined) {
this.buffer[skey] = {};
}
// create function key
var fkey = '';
while (true) {
fkey = 'func' + (Math.random() + '').split('.')[1];
if (this.buffer[skey][fkey] == undefined) break;
}
// create wrapper callback function
this.buffer[skey][fkey] = function () {
// invoke callback function
try {
func.apply(this, [].slice.call(arguments));
}
catch (e) {
console.log('Invoke callback function error : ', e);
}
// clear callback
try {
// is finish function then call back clear
if (typeof(func.isFinFunc) == 'undefined' || func.isFinFunc == true) {
var cbSet = FuncPool.buffer[skey];
if (cbSet != undefined && cbSet != null) {
for (var key in cbSet) {
if (cbSet.hasOwnProperty(key)) {
delete FuncPool.buffer[skey][key];
}
}
delete FuncPool.buffer[skey];
}
}
}
catch (e) {
console.log('fail to clear function : ', e);
}
};
return 'FuncPool.buffer[\'' + skey + '\'][\'' + fkey + '\']';
}
}
var kSet =FuncPool.createSetKey();
var cb_func = function(total,current) {
console.log('total : ' + total);
console.log('current : ' + current);
}
cb_func.isFinFunc = false
var kFunc = FuncPool.addFunc(kSet, cb_func);
console.log(kFunc);
console.log(FuncPool.buffer);
eval(kFunc)(123,456)
console.log(kFunc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment