Skip to content

Commit

Permalink
perf(Idle): interrupts are detached when state is idle or not running
Browse files Browse the repository at this point in the history
During states where interrupts cannot interrupt, we want to avoid any further processing of
interrupt activity
  • Loading branch information
grbsk committed Feb 23, 2016
1 parent 7dd63ef commit e0b2a1e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
76 changes: 75 additions & 1 deletion src/idle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function main() {
let actual = subs[0];
expect(actual.source).toBe(source);
expect(source.onInterrupt.subscribe).toHaveBeenCalled();
expect(source.attach).toHaveBeenCalled();
expect(source.attach).not.toHaveBeenCalled();
});

it('getInterrupts() should return current subscriptions', () => {
Expand Down Expand Up @@ -176,6 +176,65 @@ export function main() {
new Date(now.getTime() + ((instance.getIdle() + instance.getTimeout()) * 1000)));
});

it('watch() should attach all interrupts', () => {
let source = new MockInterruptSource();

instance.setInterrupts([source]);
expect(source.isAttached).toBe(false);

instance.watch();

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

instance.stop();
});

it('watch() should detach all interrupts', () => {
let source = new MockInterruptSource();

instance.setInterrupts([source]);
instance.watch();

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

instance.stop();

expect(source.isAttached).toBe(false);
});

it('watch() should attach all interrupts when resuming after idle',
<any>fakeAsync((): void => {
let source = new MockInterruptSource();

instance.setInterrupts([source]);
instance.watch();

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

tick(3000);

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

instance.watch();

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

instance.stop();
}));

it('timeout() should detach all interrupts', () => {
let source = new MockInterruptSource();

instance.setInterrupts([source]);
instance.watch();

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

instance.stop();

expect(source.isAttached).toBe(false);
});

it('watch(true) should not set expiry', () => {
instance.watch(true);
expect(expiry.last()).toBeUndefined();
Expand All @@ -194,6 +253,21 @@ export function main() {
expect(instance.isIdling()).toBe(false);
}));

it('should pause interrupts when idle', <any>fakeAsync((): void => {
let source = new MockInterruptSource();

instance.setInterrupts([source]);
instance.watch();

tick(3000);

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

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

instance.stop();
}));

it('emits an onIdleStart event when the user becomes idle', <any>fakeAsync((): void => {
spyOn(instance.onIdleStart, 'emit').and.callThrough();

Expand Down
18 changes: 17 additions & 1 deletion src/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class Idle implements OnDestroy {
for (let source of sources) {
let sub = new Interrupt(source);
sub.subscribe((args: InterruptArgs) => { self.interrupt(args.force, args.innerArgs); });
sub.resume();

this.interrupts.push(sub);
}
Expand Down Expand Up @@ -193,6 +192,7 @@ export class Idle implements OnDestroy {
}
if (!this.running) {
this.startKeepalive();
this.toggleInterrupts(true);
}

this.running = true;
Expand All @@ -206,6 +206,8 @@ export class Idle implements OnDestroy {
stop(): void {
this.stopKeepalive();

this.toggleInterrupts(false);

this.safeClearInterval('idleHandle');
this.safeClearInterval('timeoutHandle');

Expand All @@ -221,6 +223,8 @@ export class Idle implements OnDestroy {
timeout(): void {
this.stopKeepalive();

this.toggleInterrupts(false);

this.safeClearInterval('idleHandle');
this.safeClearInterval('timeoutHandle');

Expand Down Expand Up @@ -257,6 +261,7 @@ export class Idle implements OnDestroy {
this.idling = !this.idling;

if (this.idling) {
this.toggleInterrupts(false);
this.onIdleStart.emit(null);
this.stopKeepalive();

Expand All @@ -266,13 +271,24 @@ export class Idle implements OnDestroy {
this.timeoutHandle = setInterval(() => { this.doCountdown(); }, 1000);
}
} else {
this.toggleInterrupts(true);
this.onIdleEnd.emit(null);
this.startKeepalive();
}

this.safeClearInterval('idleHandle');
}

private toggleInterrupts(resume: boolean): void {
for (let interrupt of this.interrupts) {
if (resume) {
interrupt.resume();
} else {
interrupt.pause();
}
}
}

private doCountdown(): void {
if (!this.idling) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/interrupt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Interrupt {
resume(): void { this.source.attach(); }

/*
* Keeps the subscription but halts interrupt events.
* Keeps the subscription but pauses interrupt events.
*/
pause(): void { this.source.detach(); }
}

0 comments on commit e0b2a1e

Please sign in to comment.