import * as _ from "lodash"; import { AppConfigType } from '../../entities/AppConfig'; import { StorageService } from './StorageService'; import { Parser } from './Parsers'; import { Marshaller } from './Marshallers'; export class CachedStorageService implements StorageService { constructor(private appConfig: AppConfigType, private localStorage: StorageService) { } contains(key: string): boolean { return !!this[`_${key}`] || this.localStorage.contains(key); } get(key: string): T; get(key: string, parse?: Parser): T { // Use cached value if there is one if (_.isNil(this[`_${key}`])) this[`_${key}`] = this.localStorage.get(key, parse); return this[`_${key}`]; } set(key: string, value: T): void; set(key: string, value: T, marshal?: Marshaller): void { this[`_${key}`] = value; // Don't put into local storage, if in CSA mode if (!this.appConfig.csa) this.localStorage.set(key, value, marshal); } }