Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
refactored instance members to private vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Nicholls committed Oct 13, 2014
1 parent dbf4962 commit 78cc397
Showing 1 changed file with 68 additions and 69 deletions.
137 changes: 68 additions & 69 deletions lib/AsyncTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,77 @@ function AsyncTimeout(config) {
*/

var instance = this;
instance._config = config ? config : {};

instance._active = false;
instance._timer = null;
instance._paused = false;
instance._began = null;
instance._autostart = false;
instance._delay = 1000;

instance._init = function() {
instance._reset();
if(instance._autostart && !instance._active) {
var _config = config ? config : {};

var _active = false;
var _timer = null;
var _paused = false;
var _began = null;
var _autostart = true;
var _delay = 1000;

var _init = function() {
_reset();
if(_autostart && !_active) {
instance.start();
}
};

var _onComplete = function() {
if(instance.isAlive()) {
var began = _began;
_reset();
instance.emit('timeout', instance);
}
};

// do a complete reset of internal state
// only used on initialization & when a
// timer expires.
var _reset = function() {
_active = false;
_timer = null;

_paused = false;
_began = null;

_autostart = (typeof(_config.autostart) !== 'undefined' ? _config.autostart : true);
_delay = (typeof(_config.delay) !== 'undefined' ? _config.delay : 1000);
};

var _invalidate = function() {
clearTimeout(_timer);
_reset();
};

// start a timer (use with autostart set to false)
instance.start = function() {
if(!instance._active) {
if(instance._paused) {
if(!_active) {
if(_paused) {
instance.resume();
} else {
instance._active = true;
instance._paused = false;
instance._began = new Date();
instance._timer = setTimeout(instance._onComplete, instance._delay);
_active = true;
_paused = false;
_began = new Date();
_timer = setTimeout(_onComplete, _delay);
instance.emit('start', instance);
}
}
};

// stop the timer
instance.stop = function() {
if(instance._active) {
instance._active = false;
instance._paused = false;
clearTimeout(instance._timer);
if(_active) {
_active = false;
_paused = false;
clearTimeout(_timer);
instance.emit('stop', instance);
}
};

// restart the timer
instance.restart = function() {
if(instance._active) {
if(_active) {
instance.stop();
}
instance.start();
Expand All @@ -74,22 +101,20 @@ function AsyncTimeout(config) {
// pause the timer
instance.pause = function() {
if(instance.isAlive()) {
instance._paused = true;
instance._delay -= (new Date() - instance._began);
clearTimeout(instance._timer);
_paused = true;
_delay -= (new Date() - _began);
clearTimeout(_timer);
instance.emit('pause', instance);
}
};

// resume a paused timer
instance.resume = function() {
if(instance._paused) {
instance._active = true;
instance._paused = false;
instance._began = new Date();
instance._timer = setTimeout(instance._onComplete, instance._delay);

instance.emit('start', instance);
if(_paused) {
_active = true;
_paused = false;
_began = new Date();
_timer = setTimeout(_onComplete, _delay);
instance.emit('resume', instance);
}
};
Expand All @@ -101,34 +126,29 @@ function AsyncTimeout(config) {

// clear the timer
instance.clear = function() {
return instance._invalidate();
};

instance._invalidate = function() {
clearTimeout(instance._timer);
instance._reset();
return _invalidate();
};

instance.isStarted = function() {
return instance._active;
return _active;
};

instance.isStopped = function() {
return !instance._active;
return !_active;
};

instance.isPaused = function() {
return instance._paused;
return _paused;
};

instance.isAlive = function() {
return (instance.isStarted() && !instance.isPaused());
};

instance.getElapsedTime = function() {
if(instance._began) {
if(_began) {
var now = new Date();
return now - instance._began;
return now - _began;
} else {
return 0;
}
Expand All @@ -149,37 +169,16 @@ function AsyncTimeout(config) {
};

instance.getDelay = function() {
return instance._delay;
return _delay;
};

instance.setDelay = function(delay) {
instance._config.delay = delay;
instance._delay = delay;
_config.delay = delay;
_delay = delay;
};

// do a complete reset of internal state
// only used on initialization & when a
// timer expires.
instance._reset = function() {
instance._active = false;
instance._timer = null;

instance._paused = false;
instance._began = null;

instance._autostart = (typeof(instance._config.autostart) !== 'undefined' ? instance._config.autostart : true);
instance._delay = (typeof(instance._config.delay) !== 'undefined' ? instance._config.delay : 1000);
};

instance._onComplete = function() {
if(instance.isAlive()) {
var began = instance._began;
instance._reset();
instance.emit('timeout', instance);
}
};

instance._init();
_init();

};

Expand Down

0 comments on commit 78cc397

Please sign in to comment.