Skip to content

Commit

Permalink
fix: AutoResume.notIdle with LocalStorageExpiry wasn't working well
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielrousseaufilion committed Feb 10, 2017
1 parent 5b3d175 commit 935ade9
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 38 deletions.
10 changes: 5 additions & 5 deletions modules/core/src/idle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ describe('core/Idle', () => {
{ providers: [LocalStorageExpiry, LocalStorage, { provide: IdleExpiry, useExisting: LocalStorageExpiry }, Idle] });
});

it('setExpiryKey() should set expiry key name', inject([Idle, LocalStorageExpiry], (idle: Idle, exp: LocalStorageExpiry) => {
idle.setExpiryKey('newKeyName');
expect((exp as LocalStorageExpiry).getExpiryKey()).toBe('newKeyName');
it('setIdleName() should set idle name', inject([Idle, LocalStorageExpiry], (idle: Idle, exp: LocalStorageExpiry) => {
idle.setIdleName('demo');
expect((exp as LocalStorageExpiry).getIdleName()).toBe('demo');
}));
});

Expand Down Expand Up @@ -63,9 +63,9 @@ describe('core/Idle', () => {
expect(actual).toEqual(expected);
});

it('setExpiryKey() when expiry is not instance of LocalStorageExpiry should throw error', () => {
it('setIdleName() when expiry is not instance of LocalStorageExpiry should throw error', () => {
expect(() => {
instance.setExpiryKey('newKeyName');
instance.setIdleName('demo');
})
.toThrowError(
'Cannot set expiry key name because no LocalStorageExpiry has been provided.');
Expand Down
22 changes: 14 additions & 8 deletions modules/core/src/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export class Idle implements OnDestroy {
}

/*
* Sets the expiry key name for localStorage.
* @param The name of the expiry key.
* Sets the idle name for localStorage.
* Important to set if multiple instances of Idle with LocalStorageExpiry
* @param The name of the idle.
*/
setExpiryKey(key: string): void {
setIdleName(key: string): void {
if (this.expiry instanceof LocalStorageExpiry) {
this.expiry.setExpiryKey(key);
this.expiry.setIdleName(key);
} else {
throw new Error('Cannot set expiry key name because no LocalStorageExpiry has been provided.');
}
Expand Down Expand Up @@ -247,7 +248,7 @@ export class Idle implements OnDestroy {
this.safeClearInterval('idleHandle');
this.safeClearInterval('timeoutHandle');

this.idling = false;
this.setIdling(false);
this.running = false;

this.expiry.last(null);
Expand All @@ -264,7 +265,7 @@ export class Idle implements OnDestroy {
this.safeClearInterval('idleHandle');
this.safeClearInterval('timeoutHandle');

this.idling = true;
this.setIdling(true);
this.running = false;
this.countdown = 0;

Expand All @@ -288,13 +289,18 @@ export class Idle implements OnDestroy {
this.onInterrupt.emit(eventArgs);

if (force === true || this.autoResume === AutoResume.idle ||
(this.autoResume === AutoResume.notIdle && !this.idling)) {
(this.autoResume === AutoResume.notIdle && !this.expiry.idling())) {
this.watch(force);
}
}

private setIdling(value: boolean): void {
this.idling = value;
this.expiry.idling(value);
}

private toggleState(): void {
this.idling = !this.idling;
this.setIdling(!this.idling);

if (this.idling) {
this.onIdleStart.emit(null);
Expand Down
17 changes: 17 additions & 0 deletions modules/core/src/idleexpiry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('core/IdleExpiry', () => {
expect(actual).toEqual(expected);
});

it('idling() returns the current value', () => {
expect(instance.idling()).toBeFalsy();
});

it('idling() sets the specified value', () => {
let expected = true;
expect(instance.idling(expected)).toEqual(expected);
expect(instance.idling()).toEqual(expected);
});

it('idling() with null param return null', () => {
let expected = null;
expect(instance.idling(expected)).toEqual(expected);
expect(instance.idling()).toEqual(expected);
});

it('isExpired() returns true if last() is less than or equal to now', () => {
let date = new Date();
instance.last(date);
Expand All @@ -52,4 +68,5 @@ describe('core/IdleExpiry', () => {
instance.last(null);
expect(instance.isExpired()).toBe(false);
});

});
15 changes: 15 additions & 0 deletions modules/core/src/idleexpiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*/
export abstract class IdleExpiry {
protected idValue: any;
protected idlingValue: boolean;

constructor() {
this.idValue = new Date();
this.idlingValue = false;
}

/*
Expand All @@ -32,6 +34,19 @@ export abstract class IdleExpiry {
*/
abstract last(value?: Date): Date;

/*
* Gets or sets the idling value.
* @param value - The value to set.
* @return The idling value.
*/
idling(value?: boolean): boolean {
if (value !== void 0) {
this.idlingValue = value;
}

return this.idlingValue;
}

/*
* Returns the current Date.
* @return The current Date.
Expand Down
46 changes: 34 additions & 12 deletions modules/core/src/localstorageexpiry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,44 @@ describe('core/LocalStorageExpiry', () => {
expect(service.last()).toEqual(expected);
}));

it('setExpiryKey() sets the key name of expiry', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getExpiryKey()).toBe('expiry');
service.setExpiryKey('name');
expect(service.getExpiryKey()).toBe('name');
it('idling() returns the current value', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.idling()).toBeFalsy();
}));

it('setExpiryKey() doesn\'t set expiry key name if param is null', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getExpiryKey()).toBe('expiry');
service.setExpiryKey(null);
expect(service.getExpiryKey()).toBe('expiry');
it('idling() sets the specified value', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
let expected = true;
expect(service.idling(expected)).toEqual(expected);
expect(service.idling()).toEqual(expected);
}));

it('setExpiryKey() doesn\'t set expiry key name if param is empty', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getExpiryKey()).toBe('expiry');
service.setExpiryKey('');
expect(service.getExpiryKey()).toBe('expiry');
it('idling() with null param return false', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
let expected = false;
expect(service.idling(null)).toEqual(expected);
expect(service.idling()).toEqual(expected);
}));

it('last() return false if param is null', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
let expected = false;
expect(service.idling(null)).toEqual(expected);
expect(service.idling()).toEqual(expected);
}));

it('setIdleName() sets the key name of expiry', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getIdleName()).toBe('main');
service.setIdleName('demo');
expect(service.getIdleName()).toBe('demo');
}));

it('setIdleName() doesn\'t set expiry key name if param is null', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getIdleName()).toBe('main');
service.setIdleName(null);
expect(service.getIdleName()).toBe('main');
}));

it('setIdleName() doesn\'t set expiry key name if param is empty', inject([LocalStorageExpiry], (service: LocalStorageExpiry) => {
expect(service.getIdleName()).toBe('main');
service.setIdleName('');
expect(service.getIdleName()).toBe('main');
}));

});
Expand Down
48 changes: 36 additions & 12 deletions modules/core/src/localstorageexpiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { LocalStorage } from './localstorage';
@Injectable()
export class LocalStorageExpiry extends IdleExpiry {

private expiryKey: string = 'expiry';
private idleName: string = 'main';

constructor(private localStorage: LocalStorage) {
super();
Expand All @@ -28,26 +28,33 @@ export class LocalStorageExpiry extends IdleExpiry {
return this.getExpiry();
}

idling(value?: boolean): boolean {
if (value !== void 0) {
this.setIdling(value);
}
return this.getIdling();
}

/*
* Gets the expiry key name.
* @return The name of the expiry key.
* Gets the idle name.
* @return The name of the idle.
*/
getExpiryKey(): string {
return this.expiryKey;
getIdleName(): string {
return this.idleName;
}

/*
* Sets the expiry key name.
* @param The name of the expiry key.
* Sets the idle name.
* @param The name of the idle.
*/
setExpiryKey(key: string): void {
setIdleName(key: string): void {
if (key) {
this.expiryKey = key;
this.idleName = key;
}
}

private getExpiry(): Date {
let expiry: string = this.localStorage.getItem(this.expiryKey);
let expiry: string = this.localStorage.getItem(this.idleName + '.expiry');
if (expiry) {
return new Date(parseInt(expiry, 10));
} else {
Expand All @@ -57,9 +64,26 @@ export class LocalStorageExpiry extends IdleExpiry {

private setExpiry(value: Date) {
if (value) {
this.localStorage.setItem(this.expiryKey, value.getTime().toString());
this.localStorage.setItem(this.idleName + '.expiry', value.getTime().toString());
} else {
this.localStorage.removeItem(this.idleName + '.expiry');
}
}

private getIdling(): boolean {
let idling: string = this.localStorage.getItem(this.idleName + '.idling');
if (idling) {
return idling === 'true';
} else {
return false;
}
}

private setIdling(value: boolean) {
if (value) {
this.localStorage.setItem(this.idleName + '.idling', value.toString());
} else {
this.localStorage.removeItem(this.expiryKey);
this.localStorage.setItem(this.idleName + '.idling', 'false');
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/storageinterruptsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class StorageInterruptSource extends WindowInterruptSource {
* @return True if the event should be filtered (don't cause an interrupt); otherwise, false.
*/
filterEvent(event: StorageEvent): boolean {
if (event.key.indexOf('ng2Idle.') >= 0) {
if (event.key.indexOf('ng2Idle.') >= 0 && event.key.indexOf('.expiry') >= 0) {
return false;
}
return true;
Expand Down

0 comments on commit 935ade9

Please sign in to comment.