As in Java, I tried to reproduce the concept of the interface "runnable".In this case it's an object, a singleton pattern to be exact.The function of this object is to pave the way for the thread.It consists of three methods available:
- set ()
- resume ()
- argumets ()
var RUNNABLE = new function(){
var _this = this ;
var runner = null ;
var args = [] ;
// Set function
this.set = function(func){
// func is a function reference without arguments
if(typeof(func) == 'function'){
runner = func ;
// func is a function with arguments passed like a string: 'example(arg1,arg2)'
}else if(typeof(func) == 'string'){
// Operations on string
func = func.replace('(',' ') ;
func = func.replace(')','');
var ref = func.split(' ')[0] ;
func = func.replace(ref,'');
args = func.split(',');
// Evaluate a function
try{runner = eval(ref);
}catch(e){throw ERROR.RUNNABLE.unset}
// Throw exception if unset
}else throw ERROR.RUNNABLE.unset ;
// Return a RUNNABLE object
return _this ;
};
// Resume function
// Returns a runner object that contains a runnable function
this.resume = function(){
if(runner != null){
return runner ;
// Throw exception if runner is null
}else throw ERROR.RUNNABLE.resume ;
}
// Returns always arguments associated to the function even if empty
this.arguments = function(){
return args ;
}
}
Nessun commento:
Posta un commento