import { StorageService } from "jslib-common/abstractions/storage.service"; export default class BrowserStorageService implements StorageService { private chromeStorageApi: any; constructor() { this.chromeStorageApi = chrome.storage.local; } async get(key: string): Promise { return new Promise((resolve) => { this.chromeStorageApi.get(key, (obj: any) => { if (obj != null && obj[key] != null) { resolve(obj[key] as T); return; } resolve(null); }); }); } async has(key: string): Promise { return (await this.get(key)) != null; } async save(key: string, obj: any): Promise { if (obj == null) { // Fix safari not liking null in set return new Promise((resolve) => { this.chromeStorageApi.remove(key, () => { resolve(); }); }); } if (obj instanceof Set) { obj = Array.from(obj); } const keyedObj = { [key]: obj }; return new Promise((resolve) => { this.chromeStorageApi.set(keyedObj, () => { resolve(); }); }); } async remove(key: string): Promise { return new Promise((resolve) => { this.chromeStorageApi.remove(key, () => { resolve(); }); }); } }