Skip to content

Commit

Permalink
Fixes #17
Browse files Browse the repository at this point in the history
added idle.setTimeout() and idle.setIdle() to change options dynamically
  • Loading branch information
moribvndvs committed Dec 25, 2014
1 parent 45b7861 commit 145e8bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/angular-idle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
* Sets the number of seconds a user can be idle before they are considered timed out.
* @param {Number|Boolean} seconds A positive number representing seconds OR 0 or false to disable this feature.
*/
this.timeout = function(seconds) {
var setTimeout = this.timeout = function(seconds) {
if (seconds === false) options.timeout = 0;
else if (angular.isNumber(seconds) && seconds >= 0) options.timeout = seconds;
else throw new Error('Timeout must be zero or false to disable the feature, or a positive integer (in seconds) to enable it.');
Expand All @@ -99,8 +99,8 @@
options.interrupt = events;
};

this.idle = function(seconds) {
if (seconds <= 0) throw new Error("Idle must be a value in seconds, greater than 0.");
var setIdle = this.idle = function(seconds) {
if (seconds <= 0) throw new Error('Idle must be a value in seconds, greater than 0.');

options.idle = seconds;
};
Expand Down Expand Up @@ -180,13 +180,27 @@
$rootScope.$broadcast('$idleTimeout');
}

function changeOption(self, fn, value) {
var reset = self.running();

self.unwatch();
fn(value);
if (reset) self.watch();
}

var svc = {
_options: function() {
return options;
},
_getNow: function() {
return new Date();
},
setIdle: function(seconds) {
changeOption(this, setIdle, seconds);
},
setTimeout: function(seconds) {
changeOption(this, setTimeout, seconds);
},
isExpired: function() {
return state.expiry && state.expiry <= this._getNow();
},
Expand Down
24 changes: 24 additions & 0 deletions src/angular-idle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ describe('ngIdle', function() {
$idle = create();
});

it ('setIdle() should update option.idle and restart', function() {
spyOn($idle, 'watch');
spyOn($idle, 'unwatch');
spyOn($idle, 'running').andCallFake(function() {return true;});

$idle.setIdle(100);

expect($idle._options().idle).toBe(100);
expect($idle.unwatch).toHaveBeenCalled();
expect($idle.watch).toHaveBeenCalled();
});

it ('setTimeout() should update option.timeout and restart', function() {
spyOn($idle, 'watch');
spyOn($idle, 'unwatch');
spyOn($idle, 'running').andCallFake(function() {return true;});

$idle.setTimeout(100);

expect($idle._options().timeout).toBe(100);
expect($idle.unwatch).toHaveBeenCalled();
expect($idle.watch).toHaveBeenCalled();
});

it('watch() should clear timeouts and start running', function() {
spyOn($interval, 'cancel');

Expand Down

0 comments on commit 145e8bc

Please sign in to comment.