// 本地存储 import Dexie from 'dexie' import { formatData } from './utils' class IndexDb { private db:any constructor() { this.db = new Dexie('collectInfo-page-info'); this.db.version(1).stores({ infoTables: '++id' }); } addItemToDb(data: any, success: Function) { const maxNum = (window as any).CollectCurrentPageInfoIndexDbSizeRecordNum || 2000; const add = () => { this.db.infoTables.add({info: formatData([data])}).then(() => { success(); }).catch(() => { success(); }) }; this.db.infoTables.count((num: number) => { console.log(num, maxNum) if (num < maxNum) { add(); } else { // 先删除最早的一条记录 this.db.infoTables.toCollection().first((item: any) => { this.removeItemFromDb(item.id, () => { add(); }); }); } }) } removeItemFromDb(id: number, callback: Function) { this.db.infoTables.delete(id).then(() => { callback(); }); } clear() { this.db.infoTables.clear(); } getAllItemToDb(callback: Function) { this.db.infoTables.toArray().then((res: any)=> { console.log(res); callback && callback(res); }) } } export default IndexDb