import MemoryStorage from "../infrastructures/MemoryStorage"; const REPOSITORY_KEY = "readyFrameRepository"; type State = { [tabId: number]: number[] }; export default interface ReadyFrameRepository { clearFrameIds(tabId: number): Promise; addFrameId(tabId: number, fraemId: number): Promise; getFrameIds(tabId: number): Promise; } export class ReadyFrameRepositoryImpl implements ReadyFrameRepository { private cache: MemoryStorage; constructor() { this.cache = new MemoryStorage(); } clearFrameIds(tabId: number): Promise { let state: State | undefined = this.cache.get(REPOSITORY_KEY); if (typeof state === "undefined") { state = {}; } delete state[tabId]; this.cache.set(REPOSITORY_KEY, state); return Promise.resolve(); } addFrameId(tabId: number, fraemId: number): Promise { let state: State | undefined = this.cache.get(REPOSITORY_KEY); if (typeof state === "undefined") { state = {}; } state[tabId] = (state[tabId] || []).concat(fraemId); this.cache.set(REPOSITORY_KEY, state); return Promise.resolve(); } getFrameIds(tabId: number): Promise { const state: State | undefined = this.cache.get(REPOSITORY_KEY); if (typeof state === "undefined") { return Promise.resolve(undefined); } return Promise.resolve(state[tabId]); } }