Skip to content

Commit

Permalink
fix(idle): use expiry.now() if expiry.last() returns null
Browse files Browse the repository at this point in the history
fixes #54
  • Loading branch information
grbsk committed May 30, 2017
1 parent a90c226 commit 06fcb36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions modules/core/src/idle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ describe('core/Idle', () => {
instance.stop();
}));

it('emits an onIdleStart event if there was no "last" expiry set.', fakeAsync(() => {
spyOn(instance.onIdleStart, 'emit').and.callThrough();

expiry.mockNow = new Date();
instance.watch();

expiry.mockNow = new Date(expiry.now().getTime() + instance.getIdle() * 1000);
expiry.last(null);
tick(3000);

expect(instance.onIdleStart.emit).toHaveBeenCalledTimes(1);

instance.stop();
}));

it('emits an onIdleEnd event when the user returns from idle', fakeAsync(() => {
spyOn(instance.onIdleEnd, 'emit').and.callThrough();

Expand Down
6 changes: 4 additions & 2 deletions modules/core/src/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ export class Idle implements OnDestroy {

let watchFn = () => {
let now: Date = this.expiry.now();
let diff: Number = this.expiry.last().getTime() - now.getTime() - (timeout * 1000);
let last: Date = this.expiry.last() || now;
let diff: Number = last.getTime() - now.getTime() - (timeout * 1000);
if (diff > 0) {
this.safeClearInterval('idleHandle');
this.idleHandle = setInterval(watchFn, diff);
Expand Down Expand Up @@ -346,7 +347,8 @@ export class Idle implements OnDestroy {
private doCountdown(): void {
let timeout = !this.timeoutVal ? 0 : this.timeoutVal;
let now: Date = this.expiry.now();
let diff: Number = this.expiry.last().getTime() - now.getTime() - (timeout * 1000);
let last: Date = this.expiry.last() || now;
let diff: Number = last.getTime() - now.getTime() - (timeout * 1000);
if (diff > 0) {
this.safeClearInterval('timeoutHandle');
this.interrupt(true);
Expand Down

0 comments on commit 06fcb36

Please sign in to comment.