From 5c805e57a226c61159a7b2e241dfcf6887754c53 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sat, 19 Sep 2020 14:58:06 +0000 Subject: [PATCH] fix(core): update --- package-lock.json | 3 +-- package.json | 1 + ts/webstore.classes.webstore.ts | 12 ++++++++++++ ts/webstore.plugins.ts | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9e71618..0f46920 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1915,8 +1915,7 @@ "@pushrocks/smartpromise": { "version": "3.0.6", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz", - "integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg==", - "dev": true + "integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg==" }, "@pushrocks/smartpuppeteer": { "version": "1.0.15", diff --git a/package.json b/package.json index 361ddf6..5646777 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "tslint-config-prettier": "^1.15.0" }, "dependencies": { + "@pushrocks/smartpromise": "^3.0.6", "idb": "^5.0.6" }, "browserslist": [ diff --git a/ts/webstore.classes.webstore.ts b/ts/webstore.classes.webstore.ts index a240abf..5c62e3d 100644 --- a/ts/webstore.classes.webstore.ts +++ b/ts/webstore.classes.webstore.ts @@ -9,24 +9,33 @@ export class WebStore { public db: plugins.idb.IDBPDatabase; public objectStore: plugins.idb.IDBPObjectStore; public options: IWebStoreOptions; + private initCalled: boolean = false; + private readyDeferred = plugins.smartpromise.defer(); constructor(optionsArg: IWebStoreOptions) { this.options = optionsArg; } public async init() { + if (this.initCalled) { + return this.readyDeferred.promise; + } + this.initCalled = true; this.db = await plugins.idb.openDB(this.options.dbName, 1, { upgrade: (db) => { db.createObjectStore(this.options.storeName); }, }); + this.readyDeferred.resolve(); } async get(key: string): Promise { + await this.init(); return this.db.get(this.options.storeName, key); } async check(keyArg: string): Promise { + await this.init(); const result = await this.get(keyArg); return !!result; } @@ -36,14 +45,17 @@ export class WebStore { } async delete(key: string) { + await this.init(); return this.db.delete(this.options.storeName, key); } async clear() { + await this.init(); return this.db.clear(this.options.storeName); } async keys() { + await this.init(); return this.db.getAllKeys(this.options.storeName); } } diff --git a/ts/webstore.plugins.ts b/ts/webstore.plugins.ts index 9b49e95..8aaa929 100644 --- a/ts/webstore.plugins.ts +++ b/ts/webstore.plugins.ts @@ -1,3 +1,11 @@ +// pushrocks scope +import * as smartpromise from '@pushrocks/smartpromise'; + +export { + smartpromise +}; + +// thirdparty scope import * as idb from 'idb'; export { idb };