From 3e31cb326b1170096cef989635f1b89b50d6383b Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sat, 29 Aug 2020 22:58:08 +0200 Subject: [PATCH] Provide additional extension storage db backend Related #159 --- lib/db.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/db.ts b/lib/db.ts index 05e60f9..ceaf7c9 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -6,6 +6,8 @@ import { BaseItem } from "./item"; // eslint-disable-next-line no-unused-vars import { Download } from "./manager/download"; import { RUNNING, QUEUED, RETRYING } from "./manager/state"; +import { storage } from "./browser"; +import { sort } from "./sorting"; const VERSION = 1; const STORE = "queue"; @@ -147,6 +149,48 @@ export class IDB implements Database { } } +class StorageDB implements Database { + private counter = 1; + + async init(): Promise { + const {db = null} = await storage.local.get("db"); + if (!db || !db.counter) { + return; + } + this.counter = db.counter; + } + + async saveItems(items: Download[]) { + const db: any = {items: []}; + for (const item of items) { + if (!item.dbId) { + item.dbId = ++this.counter; + } + db.items.push(item.toJSON()); + } + db.counter = this.counter; + await storage.local.set({db}); + } + + async deleteItems(items: any[]): Promise { + const gone = new Set(items.map(i => i.dbId)); + const {db = null} = await storage.local.get("db"); + if (!db) { + return; + } + db.items = db.items.filter((i: any) => !gone.has(i.dbId)); + await storage.local.set({db}); + } + + async getAll() { + const {db = null} = await storage.local.get("db"); + if (!db || !Array.isArray(db.items)) { + return []; + } + return sort(db.items, (i: any) => i.position) as BaseItem[]; + } +} + class MemoryDB implements Database { private counter = 1; @@ -206,9 +250,17 @@ export const DB = new class DBWrapper implements Database { } catch (ex) { console.warn( - "Failed to initialize idb backend, using memory db fallback", ex); - this.db = new MemoryDB(); - await this.db.init(); + "Failed to initialize idb backend, using storage db fallback", ex); + try { + this.db = new StorageDB(); + await this.db.init(); + } + catch (ex) { + console.warn( + "Failed to initialize storage backend, using memory db fallback", ex); + this.db = new MemoryDB(); + await this.db.init(); + } } } }();