Created
July 3, 2013 06:41
-
-
Save phucat/5915908 to your computer and use it in GitHub Desktop.
Defer method for Javascript.
This file contains 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
/* | |
DEFER OBJECT | |
- Mimic some parts of reactor on twistd python | |
AUTHOR : Sugar Ray Tenorio | |
METHODS : | |
defer._init : set / initialize variables | |
defer._run : start reactor | |
defer._add(function, params obj) : add process in reactor. | |
defer._stop : stops reactor | |
EVENTS : | |
defer.onStop : triggers when reactor stops | |
PROPERTY : | |
defer._process : serves as process holder | |
defer.timeout : length of time the reactor takes another loop | |
CONSTANT : | |
defer.interval : sets as the main variable where the loop takes place. | |
*/ | |
var Defer = { | |
init: function (AB) { | |
this._process = []; | |
this.timeout = 40; | |
this.interval = ''; | |
this.counter = 0; | |
//this._run(); | |
this._run(); | |
if (consoleCheck) console.log('defer running status : ', this.running); | |
}, | |
_add: function (_method, param1, param2, param3, param4, param5, param6, param7) { | |
var o = { | |
"_method": _method, | |
"param1": param1 || '', | |
"param2": param2 || '', | |
"param3": param3 || '', | |
"param4": param4 || '', | |
"param5": param5 || '', | |
"param6": param6 || '', | |
"param7": param7 || '' | |
} | |
this._process.push(o); | |
}, | |
_run: function () { | |
//alert('run'); | |
deferObj = this; | |
this.interval = setInterval(function () { | |
//if(consoleCheck)console.log('start loop'); | |
if (deferObj) { | |
// if has _process | |
try { | |
if (deferObj._process.length > 0 && (deferObj.counter != deferObj._process.length)) { | |
if (consoleCheck) { | |
//console.log('defer cycle [ counter : %s ] [ _process length : %s ]', deferObj.counter, deferObj._process.length); | |
console.log("METHOD : ", deferObj._process[deferObj.counter]._method); | |
console.log("PARAM1 : ", deferObj._process[deferObj.counter].param1); | |
console.log("PARAM2 : ", deferObj._process[deferObj.counter].param2); | |
console.log("PARAM3 : ", deferObj._process[deferObj.counter].param3); | |
} | |
try { | |
if (deferObj._process[deferObj.counter]._method) { | |
//var F = eval(deferObj._process[deferObj.counter]._method); | |
//F(deferObj._process[deferObj.counter].param1, deferObj._process[deferObj.counter].param2, deferObj._process[deferObj.counter].param3); | |
eval(deferObj._process[deferObj.counter]._method + "('" + deferObj._process[deferObj.counter].param1 + "', '" + deferObj._process[deferObj.counter].param2 + "', '" + deferObj._process[deferObj.counter].param3 + "', '" + deferObj._process[deferObj.counter].param4 + "', '" + deferObj._process[deferObj.counter].param5 + "', '" + deferObj._process[deferObj.counter].param6 + "', '" + deferObj._process[deferObj.counter].param7 + "')"); | |
} | |
//check if last in array, if last then stop else add counter | |
if (deferObj.counter >= deferObj._process.length) { | |
console.debug("DEFER process length is less than the counter"); | |
deferObj._stop(); | |
} else { | |
deferObj.counter++; | |
} | |
} catch (e) { | |
if (consoleCheck) console.debug('Defer error catch 2 : ', e); | |
if (consoleCheck) console.debug('Defer error catch 2 [ counter : %s ] [ _process length : %s ] ', deferObj.counter, deferObj._process.length); | |
deferObj._stop(); | |
} | |
} else { | |
//console.log("DEFER last in array "); | |
deferObj._stop(); | |
} | |
} catch (e) { | |
if (consoleCheck) console.log('Defer error catch 1 : ', e); | |
if (consoleCheck) console.log('Defer obj :', deferObj); | |
AB.defer._stop(); | |
} | |
} else { | |
console.log("DEFER no defer object."); | |
AB.defer._stop(); | |
} | |
//if(consoleCheck)console.log('end loop'); | |
}, this.timeout); | |
deferObj.running = true; | |
//if(consoleCheck)console.log('defer started'); | |
}, | |
_stop: function () { | |
//if(consoleCheck)console.log('defer STOPPED [ counter : %s ] [ _process length : %s ] ', this.counter, this._process.length); | |
//if(consoleCheck)console.log('PROCESSES REMAINING : ', this._process); | |
clearInterval(this.interval); | |
this._process = []; | |
//reset counter | |
this.counter = 0; | |
this.running = false; | |
this.onStop(); | |
}, | |
onStop: function () { | |
// this.timeout = 5000; | |
this._run(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment