Skip to content

Commit

Permalink
fix(idle,timeout): countdown idle and timeout based on time (#131)
Browse files Browse the repository at this point in the history
fixes #110 #112
  • Loading branch information
mychalhackman authored Oct 25, 2023
1 parent c4aea50 commit fb9a6bd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
32 changes: 1 addition & 31 deletions projects/core/src/lib/idle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,10 @@ describe('core/Idle', () => {

instance.setTimeout(3);
instance.setInterrupts([source]);

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

expect(source.isAttached).toBe(true);

expiry.mockNow = new Date(
expiry.now().getTime() + instance.getIdle() * 1000
);
tick(30000);
tick(1000);
tick(1000);
Expand Down Expand Up @@ -411,12 +406,9 @@ describe('core/Idle', () => {
spyOn(instance.onTimeout, 'emit').and.callThrough();

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

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

tick(3000);
expect(instance.isIdling()).toBe(true);

Expand All @@ -434,12 +426,8 @@ describe('core/Idle', () => {
spyOn(instance.onTimeout, 'emit').and.callThrough();

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

expiry.mockNow = new Date(
expiry.now().getTime() + instance.getIdle() * 1000
);
tick(3000);
expect(instance.isIdling()).toBe(true);

Expand All @@ -456,12 +444,8 @@ describe('core/Idle', () => {
spyOn(instance.onInterrupt, 'emit').and.callThrough();

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

expiry.mockNow = new Date(
expiry.now().getTime() + instance.getIdle() * 1000
);
tick(3000);
expect(instance.isIdling()).toBe(true);

Expand Down Expand Up @@ -499,12 +483,8 @@ describe('core/Idle', () => {
spyOn(instance.onTimeoutWarning, 'emit').and.callThrough();

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

expiry.mockNow = new Date(
expiry.now().getTime() + instance.getIdle() * 1000
);
tick(3000);
// we're going to check that it's idling, then force it to not be
expect(instance.isIdling()).toBe(true);
Expand Down Expand Up @@ -653,14 +633,9 @@ describe('core/Idle', () => {
instance.setAutoResume(AutoResume.notIdle);
instance.setIdle(3);

const now = new Date();
expiry.mockNow = now;
instance.watch();
spyOn(instance, 'watch').and.callThrough();

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

expect(instance.isIdling()).toBe(false);
Expand Down Expand Up @@ -827,13 +802,8 @@ describe('core/Idle', () => {
}));

it('should stop keepalive when timed out', fakeAsync(() => {
expiry.mockNow = new Date();
instance.watch();
expect(svc.isRunning).toBe(true);

expiry.mockNow = new Date(
expiry.now().getTime() + instance.getIdle() * 1000
);
tick(3000);
tick(1000);
tick(1000);
Expand Down
12 changes: 7 additions & 5 deletions projects/core/src/lib/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ export class Idle implements OnDestroy {
const diff = this.getExpiryDiff(timeout);
if (diff > 0) {
this.safeClearInterval('idleHandle');
this.setIdleIntervalOutsideOfZone(watchFn, diff);
this.setIdleIntervalOutsideOfZone(watchFn, 1000);
} else {
this.toggleState();
}
});
};

this.setIdleIntervalOutsideOfZone(watchFn, this.idle * 1000);
this.setIdleIntervalOutsideOfZone(watchFn, 1000);
}

/*
Expand Down Expand Up @@ -351,7 +351,7 @@ export class Idle implements OnDestroy {
if (this.timeoutVal > 0) {
this.countdown = this.timeoutVal;
this.doCountdown();
this.setTimoutIntervalOutsideZone(() => {
this.setTimeoutIntervalOutsideZone(() => {
this.doCountdownInZone();
}, 1000);
}
Expand All @@ -364,7 +364,7 @@ export class Idle implements OnDestroy {
this.safeClearInterval('idleHandle');
}

private setTimoutIntervalOutsideZone(
private setTimeoutIntervalOutsideZone(
intervalFn: () => void,
frequency: number
) {
Expand Down Expand Up @@ -415,7 +415,9 @@ export class Idle implements OnDestroy {
}

this.onTimeoutWarning.emit(this.countdown);
this.countdown--;

const countdownMs = ((this.timeoutVal - 1) * 1000) + diff;
this.countdown = Math.round(countdownMs / 1000);
}

private safeClearInterval(handleName: string): void {
Expand Down

0 comments on commit fb9a6bd

Please sign in to comment.