Attempt number two at ripping out Bluebird from rageshake.js

pull/21833/head
Michael Telatynski 2019-11-18 09:47:37 +00:00
parent 84f78ae726
commit 47948812b0
1 changed files with 11 additions and 8 deletions

View File

@ -136,6 +136,8 @@ class IndexedDBLogStore {
this.id = "instance-" + Math.random() + Date.now(); this.id = "instance-" + Math.random() + Date.now();
this.index = 0; this.index = 0;
this.db = null; this.db = null;
// these promises are cleared as soon as fulfilled
this.flushPromise = null; this.flushPromise = null;
// set if flush() is called whilst one is ongoing // set if flush() is called whilst one is ongoing
this.flushAgainPromise = null; this.flushAgainPromise = null;
@ -208,16 +210,16 @@ class IndexedDBLogStore {
*/ */
flush() { flush() {
// check if a flush() operation is ongoing // check if a flush() operation is ongoing
if (this.flushPromise && this.flushPromise.isPending()) { if (this.flushPromise) {
if (this.flushAgainPromise && this.flushAgainPromise.isPending()) { if (this.flushAgainPromise) {
// this is the 3rd+ time we've called flush() : return the same // this is the 3rd+ time we've called flush() : return the same promise.
// promise.
return this.flushAgainPromise; return this.flushAgainPromise;
} }
// queue up a flush to occur immediately after the pending one // queue up a flush to occur immediately after the pending one completes.
// completes.
this.flushAgainPromise = this.flushPromise.then(() => { this.flushAgainPromise = this.flushPromise.then(() => {
return this.flush(); return this.flush();
}).then(() => {
this.flushAgainPromise = null;
}); });
return this.flushAgainPromise; return this.flushAgainPromise;
} }
@ -225,8 +227,7 @@ class IndexedDBLogStore {
// a brand new one, destroying the chain which may have been built up. // a brand new one, destroying the chain which may have been built up.
this.flushPromise = new Promise((resolve, reject) => { this.flushPromise = new Promise((resolve, reject) => {
if (!this.db) { if (!this.db) {
// not connected yet or user rejected access for us to r/w to // not connected yet or user rejected access for us to r/w to the db.
// the db.
reject(new Error("No connected database")); reject(new Error("No connected database"));
return; return;
} }
@ -251,6 +252,8 @@ class IndexedDBLogStore {
objStore.add(this._generateLogEntry(lines)); objStore.add(this._generateLogEntry(lines));
const lastModStore = txn.objectStore("logslastmod"); const lastModStore = txn.objectStore("logslastmod");
lastModStore.put(this._generateLastModifiedTime()); lastModStore.put(this._generateLastModifiedTime());
}).then(() => {
this.flushPromise = null;
}); });
return this.flushPromise; return this.flushPromise;
} }