import { globalState } from '../../app/global-state'; export class MemoryCacheConfig { static windowRootName: string = 'powerduck'; static cacheObjectKey: string = '_cache'; } class MemoryCacheImpl { private readonly _repository: string; constructor(repository: string) { this._repository = repository; } private getCacheRoot(): any { const repository = this._repository; if (globalState[repository] == null) { globalState[repository] = {}; } if (globalState[repository][MemoryCacheConfig.cacheObjectKey] == null) { globalState[repository][MemoryCacheConfig.cacheObjectKey] = {}; } return globalState[repository][MemoryCacheConfig.cacheObjectKey]; } getItem(key: string): T { return this.getCacheRoot()[key]; } setItem(key: string, value: T): void { this.getCacheRoot()[key] = value; } } export default class MemoryCache { static get(repository: string): MemoryCacheImpl { return new MemoryCacheImpl(repository); } static get default() { return new MemoryCacheImpl(MemoryCacheConfig.windowRootName); } }