import { createStore, del, get, keys, set } from 'idb-keyval'; export type ConfigStore = { get: (key: string) => Promise; set: (key: string, value: T) => Promise; keys: () => Promise; delete: (key: string) => Promise; }; const initialIndexedDB = (database: string): ConfigStore => { const store = createStore(`affine:${database}`, 'database'); return { get: (key: string) => get(key, store), set: (key: string, value: T) => set(key, value, store), keys: () => keys(store), delete: (key: string) => del(key, store), }; }; const scopedIndexedDB = () => { const idb = initialIndexedDB('global'); const storeCache = new Map>(); return (scope: string): Readonly> => { if (!storeCache.has(scope)) { const store = { get: (key: string) => idb.get(`${scope}:${key}`), set: (key: string, value: T) => idb.set(`${scope}:${key}`, value), keys: () => idb .keys() .then(keys => keys .filter(k => k.startsWith(`${scope}:`)) .map(k => k.replace(`${scope}:`, '')) ), delete: (key: string) => del(`${scope}:${key}`), }; storeCache.set(scope, store); } return storeCache.get(scope) as ConfigStore; }; }; export const getKVConfigure = scopedIndexedDB();