From 4195a64c597fb312add193b5a6f6c19414323f9b Mon Sep 17 00:00:00 2001 From: Andrew-David Jahchan Date: Mon, 26 May 2014 10:08:53 -0600 Subject: [PATCH 1/4] Added Exists Method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + ‘exists’ method ^ stop method now uses exists method before attempting to stop interval ^ changed objects on line 18 & 19 to use dot notation --- poll.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/poll.js b/poll.js index 99d3206..95cecff 100644 --- a/poll.js +++ b/poll.js @@ -15,8 +15,8 @@ var Poll = { } else { Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(function(){ config.action(); - Poll.timers[config.name]["value"] = setInterval(config.action, config.interval); - Poll.timers[config.name]["type"] = "interval"; + Poll.timers[config.name].value = setInterval(config.action, config.interval); + Poll.timers[config.name].type = "interval"; }, config.start)}; } } else { @@ -61,13 +61,28 @@ var Poll = { fn(); } }, - "stop": function(name){ - var instance = Poll.timers[name]; - if(instance.type == "interval"){ - clearInterval(instance.value); - } else { - clearTimeout(instance.value); - } + "exists": function(name){ + if(Poll.timers[name] !== undefined) + { + return true; + } + else + { + return false; + } }, - "timers":{} + "stop": function(name){ + if(Poll.exists(name)) + { + var instance = Poll.timers[name]; + if(instance.type == "interval"){ + clearInterval(instance.value); + } else { + clearTimeout(instance.value); + } + } else { + throw "PollJS: The interval you are trying to stop does not exist."; + } + }, + "timers":{} }; From ee90adc2578d969885e90225755e416a0827bb02 Mon Sep 17 00:00:00 2001 From: Andrew-David Jahchan Date: Mon, 26 May 2014 11:14:47 -0600 Subject: [PATCH 2/4] Re-activate Intervals + you can re-activate a previously created interval ^ exists method also checks whether interval is active --- poll.js | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/poll.js b/poll.js index 95cecff..1788a4e 100644 --- a/poll.js +++ b/poll.js @@ -1,34 +1,49 @@ var Poll = { "version": "0.3", "start": function(config){ - action = config.action; - config.internal_action = config.action; - config.action = function(){ - Poll.util.attempts(config.name, config.internal_action); - }; + if(typeof config === 'string') + { + if(Poll.exists(config, true)) + { + config = Poll.timers[config].config; + } + else + { + throw "PollJS: The interval you are trying to re-activate does not exist."; + } + } + else + { + action = config.action; + config.internal_action = config.action; + config.action = function(){ + Poll.util.attempts(config.name, config.internal_action); + }; + } + if(config.start){ if(config.interval){ if(config.increment){ - Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(function(){ + Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "active": true, "value": setTimeout(function(){ Poll.util.timeout(config.name, config.action, config.interval); }, config.start)}; } else { - Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(function(){ + Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "active": true, "value": setTimeout(function(){ config.action(); Poll.timers[config.name].value = setInterval(config.action, config.interval); Poll.timers[config.name].type = "interval"; }, config.start)}; } } else { - Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "value": setTimeout(config.action, config.start)}; + Poll.timers[config.name] = {"type": "timeout", "config": config, "attempts": 0, "active": true, "value": setTimeout(config.action, config.start)}; } } else if(config.interval){ if(config.increment){ - Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "value": setTimeout(function(){ + Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "active": true, "value": setTimeout(function(){ Poll.util.timeout(config.name, config.action, (config.interval + config.increment)); }, config.interval)}; } else { - Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "value": setInterval(config.action, config.interval)}; + Poll.timers[config.name] = {"type": "interval", "config": config, "attempts": 0, "active": true, "value": setInterval(config.action, config.interval)}; } } else { throw "PollJS: You need to define a start, an interval, or both."; @@ -61,8 +76,8 @@ var Poll = { fn(); } }, - "exists": function(name){ - if(Poll.timers[name] !== undefined) + "exists": function(name, ignore_active_state){ + if(Poll.timers[name] !== undefined && (ignore_active_state === true || Poll.timers[name].active === true)) { return true; } @@ -77,11 +92,12 @@ var Poll = { var instance = Poll.timers[name]; if(instance.type == "interval"){ clearInterval(instance.value); + instance.active = false; + } else { clearTimeout(instance.value); + instance.active = false; } - } else { - throw "PollJS: The interval you are trying to stop does not exist."; } }, "timers":{} From 4858d9f7997575136d7f96e308309781c9baee08 Mon Sep 17 00:00:00 2001 From: Andrew-David Jahchan Date: Mon, 26 May 2014 11:16:39 -0600 Subject: [PATCH 3/4] Minified ^ minified changes --- poll.min.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/poll.min.js b/poll.min.js index 5c7518d..a6e34d2 100644 --- a/poll.min.js +++ b/poll.min.js @@ -1 +1,13 @@ -var Poll={version:"0.3",start:function(a){action=a.action;a.internal_action=a.action;a.action=function(){Poll.util.attempts(a.name,a.internal_action)};if(a.start){if(a.interval){if(a.increment){Poll.timers[a.name]={type:"timeout",config:a,attempts:0,value:setTimeout(function(){Poll.util.timeout(a.name,a.action,a.interval)},a.start)}}else{Poll.timers[a.name]={type:"timeout",config:a,attempts:0,value:setTimeout(function(){a.action();Poll.timers[a.name]["value"]=setInterval(a.action,a.interval);Poll.timers[a.name]["type"]="interval"},a.start)}}}else{Poll.timers[a.name]={type:"timeout",config:a,attempts:0,value:setTimeout(a.action,a.start)}}}else if(a.interval){if(a.increment){Poll.timers[a.name]={type:"interval",config:a,attempts:0,value:setTimeout(function(){Poll.util.timeout(a.name,a.action,a.interval+a.increment)},a.interval)}}else{Poll.timers[a.name]={type:"interval",config:a,attempts:0,value:setInterval(a.action,a.interval)}}}else{throw"PollJS: You need to define a start, an interval, or both."}},util:{attempts:function(a,b){var c,d=Poll.timers[a];Poll.timers[a].attempts+=1;c=b();if(c===false){Poll.stop(a)}if(d.config.attempts){if(d.attempts==d.config.attempts){Poll.stop(a);d.config.fallback()}}},timeout:function(a,b,c){var d,e=Poll.timers[a].config;d=c+(e.increment||0);Poll.timers[a].value=setTimeout(function(){Poll.util.timeout(e.name,b,d)},d);Poll.timers[a].type="timeout";b()}},stop:function(a){var b=Poll.timers[a];if(b.type=="interval"){clearInterval(b.value)}else{clearTimeout(b.value)}},timers:{}} \ No newline at end of file +var Poll={"version":"0.3","start":function(config){if(typeof config==='string') +{if(Poll.exists(config,true)) +{config=Poll.timers[config].config;} +else +{throw"PollJS: The interval you are trying to re-activate does not exist.";}} +else +{action=config.action;config.internal_action=config.action;config.action=function(){Poll.util.attempts(config.name,config.internal_action);};} +if(config.start){if(config.interval){if(config.increment){Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(function(){Poll.util.timeout(config.name,config.action,config.interval);},config.start)};}else{Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(function(){config.action();Poll.timers[config.name].value=setInterval(config.action,config.interval);Poll.timers[config.name].type="interval";},config.start)};}}else{Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(config.action,config.start)};}}else if(config.interval){if(config.increment){Poll.timers[config.name]={"type":"interval","config":config,"attempts":0,"active":true,"value":setTimeout(function(){Poll.util.timeout(config.name,config.action,(config.interval+config.increment));},config.interval)};}else{Poll.timers[config.name]={"type":"interval","config":config,"attempts":0,"active":true,"value":setInterval(config.action,config.interval)};}}else{throw"PollJS: You need to define a start, an interval, or both.";}},"util":{"attempts":function(name,fn){var ret,instance=Poll.timers[name];Poll.timers[name].attempts+=1;ret=fn();if(ret===false){Poll.stop(name);} +if(instance.config.attempts){if(instance.attempts==instance.config.attempts){Poll.stop(name);instance.config.fallback();}}},"timeout":function(name,fn,start){var time,config=Poll.timers[name].config;time=(start+(config.increment||0));Poll.timers[name].value=setTimeout(function(){Poll.util.timeout(config.name,fn,time);},time);Poll.timers[name].type="timeout";fn();}},"exists":function(name,ignore_active_state){if(Poll.timers[name]!==undefined&&(ignore_active_state===true||Poll.timers[name].active===true)) +{return true;} +else +{return false;}},"stop":function(name){if(Poll.exists(name)) +{var instance=Poll.timers[name];if(instance.type=="interval"){clearInterval(instance.value);instance.active=false;}else{clearTimeout(instance.value);instance.active=false;}}},"timers":{}}; \ No newline at end of file From f2bbeb78dbaf3d6247048108ce8461fc418b0999 Mon Sep 17 00:00:00 2001 From: Andrew-David Jahchan Date: Mon, 26 May 2014 11:24:04 -0600 Subject: [PATCH 4/4] Minified Update ^ properly minified poll.min.js --- poll.min.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/poll.min.js b/poll.min.js index a6e34d2..dfd02c3 100644 --- a/poll.min.js +++ b/poll.min.js @@ -1,13 +1 @@ -var Poll={"version":"0.3","start":function(config){if(typeof config==='string') -{if(Poll.exists(config,true)) -{config=Poll.timers[config].config;} -else -{throw"PollJS: The interval you are trying to re-activate does not exist.";}} -else -{action=config.action;config.internal_action=config.action;config.action=function(){Poll.util.attempts(config.name,config.internal_action);};} -if(config.start){if(config.interval){if(config.increment){Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(function(){Poll.util.timeout(config.name,config.action,config.interval);},config.start)};}else{Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(function(){config.action();Poll.timers[config.name].value=setInterval(config.action,config.interval);Poll.timers[config.name].type="interval";},config.start)};}}else{Poll.timers[config.name]={"type":"timeout","config":config,"attempts":0,"active":true,"value":setTimeout(config.action,config.start)};}}else if(config.interval){if(config.increment){Poll.timers[config.name]={"type":"interval","config":config,"attempts":0,"active":true,"value":setTimeout(function(){Poll.util.timeout(config.name,config.action,(config.interval+config.increment));},config.interval)};}else{Poll.timers[config.name]={"type":"interval","config":config,"attempts":0,"active":true,"value":setInterval(config.action,config.interval)};}}else{throw"PollJS: You need to define a start, an interval, or both.";}},"util":{"attempts":function(name,fn){var ret,instance=Poll.timers[name];Poll.timers[name].attempts+=1;ret=fn();if(ret===false){Poll.stop(name);} -if(instance.config.attempts){if(instance.attempts==instance.config.attempts){Poll.stop(name);instance.config.fallback();}}},"timeout":function(name,fn,start){var time,config=Poll.timers[name].config;time=(start+(config.increment||0));Poll.timers[name].value=setTimeout(function(){Poll.util.timeout(config.name,fn,time);},time);Poll.timers[name].type="timeout";fn();}},"exists":function(name,ignore_active_state){if(Poll.timers[name]!==undefined&&(ignore_active_state===true||Poll.timers[name].active===true)) -{return true;} -else -{return false;}},"stop":function(name){if(Poll.exists(name)) -{var instance=Poll.timers[name];if(instance.type=="interval"){clearInterval(instance.value);instance.active=false;}else{clearTimeout(instance.value);instance.active=false;}}},"timers":{}}; \ No newline at end of file +var Poll={version:"0.3",start:function(e){if(typeof e==="string"){if(Poll.exists(e,true)){e=Poll.timers[e].config}else{throw"PollJS: The interval you are trying to re-activate does not exist."}}else{action=e.action;e.internal_action=e.action;e.action=function(){Poll.util.attempts(e.name,e.internal_action)}}if(e.start){if(e.interval){if(e.increment){Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(function(){Poll.util.timeout(e.name,e.action,e.interval)},e.start)}}else{Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(function(){e.action();Poll.timers[e.name].value=setInterval(e.action,e.interval);Poll.timers[e.name].type="interval"},e.start)}}}else{Poll.timers[e.name]={type:"timeout",config:e,attempts:0,active:true,value:setTimeout(e.action,e.start)}}}else if(e.interval){if(e.increment){Poll.timers[e.name]={type:"interval",config:e,attempts:0,active:true,value:setTimeout(function(){Poll.util.timeout(e.name,e.action,e.interval+e.increment)},e.interval)}}else{Poll.timers[e.name]={type:"interval",config:e,attempts:0,active:true,value:setInterval(e.action,e.interval)}}}else{throw"PollJS: You need to define a start, an interval, or both."}},util:{attempts:function(e,t){var n,r=Poll.timers[e];Poll.timers[e].attempts+=1;n=t();if(n===false){Poll.stop(e)}if(r.config.attempts){if(r.attempts==r.config.attempts){Poll.stop(e);r.config.fallback()}}},timeout:function(e,t,n){var r,i=Poll.timers[e].config;r=n+(i.increment||0);Poll.timers[e].value=setTimeout(function(){Poll.util.timeout(i.name,t,r)},r);Poll.timers[e].type="timeout";t()}},exists:function(e,t){if(Poll.timers[e]!==undefined&&(t===true||Poll.timers[e].active===true)){return true}else{return false}},stop:function(e){if(Poll.exists(e)){var t=Poll.timers[e];if(t.type=="interval"){clearInterval(t.value);t.active=false}else{clearTimeout(t.value);t.active=false}}},timers:{}} \ No newline at end of file