import { injectable } from "tsyringe"; import MemoryStorage from "../infrastructures/MemoryStorage"; const FIND_GLOBAL_KEYWORD_KEY = "find-global-keyword"; const FIND_LOCAL_KEYWORD_KEY = "find-local-keyword"; export type FindState = { keyword: string; frameId: number; }; export default interface FindRepository { getGlobalKeyword(): Promise; setGlobalKeyword(keyword: string): Promise; getLocalState(tabId: number): Promise; setLocalState(tabId: number, state: FindState): Promise; deleteLocalState(tabId: number): Promise; } @injectable() export class FindRepositoryImpl implements FindRepository { private cache: MemoryStorage; constructor() { this.cache = new MemoryStorage(); } getGlobalKeyword(): Promise { return Promise.resolve(this.cache.get(FIND_GLOBAL_KEYWORD_KEY)); } setGlobalKeyword(keyword: string): Promise { this.cache.set(FIND_GLOBAL_KEYWORD_KEY, keyword); return Promise.resolve(); } getLocalState(tabId: number): Promise { let states = this.cache.get(FIND_LOCAL_KEYWORD_KEY); if (typeof states === "undefined") { states = {}; } return Promise.resolve(states[tabId]); } setLocalState(tabId: number, state: FindState): Promise { let states = this.cache.get(FIND_LOCAL_KEYWORD_KEY); if (typeof states === "undefined") { states = {}; } states[tabId] = state; this.cache.set(FIND_LOCAL_KEYWORD_KEY, states); return Promise.resolve(); } deleteLocalState(tabId: number): Promise { const states = this.cache.get(FIND_LOCAL_KEYWORD_KEY); if (typeof states === "undefined") { return Promise.resolve(); } delete states[tabId]; this.cache.set(FIND_LOCAL_KEYWORD_KEY, states); return Promise.resolve(); } }