Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions projects/common/src/preference/preference.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Injectable } from '@angular/core';
import { Observable, of, throwError } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { AbstractStorage } from '../utilities/browser/storage/abstract-storage';
import { InMemoryStorage } from '../utilities/browser/storage/in-memory-storage';
import { LocalStorage } from '../utilities/browser/storage/local-storage';
import { SessionStorage } from '../utilities/browser/storage/session-storage';
import { BooleanCoercer } from '../utilities/coercers/boolean-coercer';
import { NumberCoercer } from '../utilities/coercers/number-coercer';

export const enum StorageType {
Local = 'local',
Session = 'session'
Session = 'session',
InMemory = 'in-memory'
}

@Injectable({
Expand All @@ -24,7 +26,11 @@ export class PreferenceService {
private readonly numberCoercer: NumberCoercer = new NumberCoercer();
private readonly booleanCoercer: BooleanCoercer = new BooleanCoercer();

public constructor(private readonly localStorage: LocalStorage, private readonly sessionStorage: SessionStorage) {}
public constructor(
private readonly localStorage: LocalStorage,
private readonly sessionStorage: SessionStorage,
private readonly inMemoryStorage: InMemoryStorage
) {}

/**
* Returns the current storage value if defined, else the default value. The observable
Expand Down Expand Up @@ -94,6 +100,8 @@ export class PreferenceService {
switch (type) {
case StorageType.Session:
return this.sessionStorage;
case StorageType.InMemory:
return this.inMemoryStorage;
case StorageType.Local:
default:
return this.localStorage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { AbstractStorage } from './abstract-storage';

@Injectable({ providedIn: 'root' })
export class InMemoryStorage extends AbstractStorage {
public constructor() {
super(
new (class {
private readonly data: Map<string, string> = new Map();

public get length(): number {
return this.data.size;
}

public clear(): void {
this.data.clear();
}

public getItem(key: string): string | null {
// tslint:disable-next-line: no-null-keyword
return this.data.get(key) ?? null;
}

public key(index: number): string | null {
// tslint:disable-next-line: no-null-keyword
return Array.from(this.data.keys())[index] ?? null;
}

public removeItem(key: string): void {
this.data.delete(key);
}

public setItem(key: string, value: string): void {
this.data.set(key, value);
}
})()
);
}
}