import Logger from "./Logger.js" const logger = new Logger('db') export default class BaseTable { constructor(o, s) { this.db = null; this.isCreatingTable = !1; this.hasCleared = !1; this.dbName = o, this.dbVersion = s } async clearDataBase(o) { return this.hasCleared || (o && (this.hasCleared = !0), !window.indexedDB.databases) ? Promise.resolve() : new Promise((s,c)=>{ const _ = window.indexedDB.deleteDatabase(this.dbName); _.onsuccess = ()=>{ s() } , _.onerror = c } ) } tableName() { throw new Error("Derived class have to override 'tableName', and set a proper table name!") } keyPath() { throw new Error("Derived class have to override 'keyPath', and set a proper index name!") } index() { throw new Error("Derived class have to override 'index', and set a proper index name!") } async checkAndOpenDatabase() { return this.db ? Promise.resolve(this.db) : new Promise((o,s)=>{ const _ = setTimeout(()=>{ logger.warn("wait db to open for", 200), this.db ? o(this.db) : o(this.checkAndOpenDatabase()), clearTimeout(_) } , 200); this.openDatabase(this.dbName, this.dbVersion || 1, ()=>{ this.db && !this.isCreatingTable && o(this.db), logger.warn(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`), clearTimeout(_) } , ()=>{ s(new Error("Failed to open database!")), clearTimeout(_) } , ()=>{ this.db && o(this.db), clearTimeout(_), logger.warn(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`) } ) } ) } openDatabase(o, s, c, _, b) { if (this.isCreatingTable) return; this.isCreatingTable = !0, logger.warn({ database: o, version: s }); const k = window.indexedDB.open(o, s) , j = this.tableName(); k.onsuccess = $=>{ this.db = k.result, logger.warn(`IndexedDb ${o} is opened.`), this.db.objectStoreNames.contains(j) && (this.isCreatingTable = !1), c && c($) } , k.onerror = $=>{ var _e; logger.error("Failed to open database", (_e = $ == null ? void 0 : $.srcElement) == null ? void 0 : _e.error), this.isCreatingTable = !1, _ && _($), this.clearDataBase(!0) } , k.onupgradeneeded = $=>{ const _e = $.target.result , et = this.index(); logger.warn(`Creating table ${j}.`); let tt = _e.objectStoreNames.contains(j); if (tt) tt = _e.transaction([j], "readwrite").objectStore(j); else { const rt = this.keyPath(); tt = _e.createObjectStore(j, { keyPath: rt }) } et.map(rt=>{ tt.createIndex(rt, rt, { unique: !1 }) } ), this.isCreatingTable = !1, logger.warn(`Table ${j} opened`), b && b($) } } async add(o) { const s = this.tableName() , b = (await this.checkAndOpenDatabase()).transaction([s], "readwrite").objectStore(s).add(o); return new Promise(function(k, j) { b.onsuccess = $=>{ k($) } , b.onerror = $=>{ var _e; logger.error((_e = $.srcElement) == null ? void 0 : _e.error), j($) } } ) } async put(o) { const s = this.tableName() , b = (await this.checkAndOpenDatabase()).transaction([s], "readwrite").objectStore(s).put(o); return new Promise(function(k, j) { b.onsuccess = $=>{ k($) } , b.onerror = $=>{ var _e; logger.error("db put error", (_e = $.srcElement) == null ? void 0 : _e.error), j($) } } ) } delete(o, s, c) { const _ = this.tableName(); this.checkAndOpenDatabase().then(b=>{ const j = b.transaction([_], "readwrite").objectStore(_).delete(o); j.onsuccess = s, j.onerror = c } ) } update() { this.checkAndOpenDatabase().then(o=>{} ) } async getAllKeys() { const o = this.tableName() , s = await this.checkAndOpenDatabase(); return new Promise((c,_)=>{ const k = s.transaction([o], "readonly").objectStore(o).getAllKeys(); k.onsuccess = j=>{ c(j.target.result) } , k.onerror = j=>{ logger.error("db getAllKeys error", j), _(j) } } ) } async query(o, s) { const c = this.tableName() , _ = await this.checkAndOpenDatabase(); return new Promise((b,k)=>{ const _e = _.transaction([c], "readonly").objectStore(c).index(o).get(s); _e.onsuccess = function(et) { var rt; const tt = (rt = et == null ? void 0 : et.target) == null ? void 0 : rt.result; b && b(tt) } , _e.onerror = et=>{ logger.error("db query error", et), k(et) } } ) } async sleep(o) { return new Promise(s=>{ setTimeout(()=>{ s("") } , o) } ) } }