From 56c9f4fecad6119d3354f0903a8e7a6dcdf33a7c Mon Sep 17 00:00:00 2001 From: Sergey Popov Date: Thu, 2 Sep 2021 15:52:41 +0300 Subject: [PATCH] Fix potential fallback db open race condition --- src/indexeddb/file-ops-fallback.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/indexeddb/file-ops-fallback.js b/src/indexeddb/file-ops-fallback.js index 5b1b1fb..7bf05e1 100644 --- a/src/indexeddb/file-ops-fallback.js +++ b/src/indexeddb/file-ops-fallback.js @@ -38,24 +38,24 @@ async function openDb(name) { // change class Persistance { constructor(onFallbackFailure) { - this._openDb = null; + this._openDbPromise = null; this.hasAlertedFailure = false; this.onFallbackFailure = onFallbackFailure; } - async getDb() { - if (this._openDb) { - return this._openDb; + getDb() { + if (this._openDbPromise) { + return this._openDbPromise; } - this._openDb = await openDb(this.dbName); - return this._openDb; + this._openDbPromise = openDb(this.dbName); + return this._openDbPromise; } closeDb() { - if (this._openDb) { - this._openDb.close(); - this._openDb = null; + if (this._openDbPromise) { + this._openDbPromise.then((db) => db.close()); + this._openDbPromise = null; } }