make database updating state trackable
This commit is contained in:
parent
5011354ddc
commit
78cf050a94
14
data/db.js
14
data/db.js
@ -80,6 +80,8 @@ export default class WbDb extends EventTarget {
|
|||||||
#blocked = false;
|
#blocked = false;
|
||||||
/** Whether opening the DB has failed. */
|
/** Whether opening the DB has failed. */
|
||||||
#failed = false;
|
#failed = false;
|
||||||
|
/** Whether the DB is currently being upgraded. */
|
||||||
|
#upgrading = false;
|
||||||
|
|
||||||
/** Get the actual database. */
|
/** Get the actual database. */
|
||||||
get db() {
|
get db() {
|
||||||
@ -109,6 +111,11 @@ export default class WbDb extends EventTarget {
|
|||||||
return this.#failed;
|
return this.#failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Check whether the database is currently being upgraded. */
|
||||||
|
get upgrading() {
|
||||||
|
return this.#upgrading;
|
||||||
|
}
|
||||||
|
|
||||||
/** Handle the `blocked` event for opening the DB.
|
/** Handle the `blocked` event for opening the DB.
|
||||||
* @param {IDBVersionChangeEvent} event The actual event.
|
* @param {IDBVersionChangeEvent} event The actual event.
|
||||||
*/
|
*/
|
||||||
@ -121,6 +128,10 @@ export default class WbDb extends EventTarget {
|
|||||||
* @param {IDBVersionChangeEvent} event The actual event.
|
* @param {IDBVersionChangeEvent} event The actual event.
|
||||||
*/
|
*/
|
||||||
#handleUpgrade(event) {
|
#handleUpgrade(event) {
|
||||||
|
this.#blocked = false;
|
||||||
|
this.#upgrading = true;
|
||||||
|
this.dispatchEvent(new CustomEvent(WbDb.EVENT_CHANGE));
|
||||||
|
|
||||||
let {
|
let {
|
||||||
oldVersion: old,
|
oldVersion: old,
|
||||||
newVersion: now,
|
newVersion: now,
|
||||||
@ -136,6 +147,8 @@ export default class WbDb extends EventTarget {
|
|||||||
*/
|
*/
|
||||||
#handleError(event) {
|
#handleError(event) {
|
||||||
this.#failed = true;
|
this.#failed = true;
|
||||||
|
this.#blocked = false;
|
||||||
|
this.#upgrading = false;
|
||||||
this.dispatchEvent(new CustomEvent(WbDb.EVENT_CHANGE));
|
this.dispatchEvent(new CustomEvent(WbDb.EVENT_CHANGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,6 +157,7 @@ export default class WbDb extends EventTarget {
|
|||||||
*/
|
*/
|
||||||
#handleSuccess(event) {
|
#handleSuccess(event) {
|
||||||
this.#blocked = false;
|
this.#blocked = false;
|
||||||
|
this.#upgrading = false;
|
||||||
this.#db = event.target.result;
|
this.#db = event.target.result;
|
||||||
this.dispatchEvent(new CustomEvent(WbDb.EVENT_CHANGE));
|
this.dispatchEvent(new CustomEvent(WbDb.EVENT_CHANGE));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,11 +25,13 @@ let inst = null;
|
|||||||
* Uses the `inst` global variable if no `instance` parameter is passed.
|
* Uses the `inst` global variable if no `instance` parameter is passed.
|
||||||
*
|
*
|
||||||
* @param {WbDb=} instance The instance to wait on.
|
* @param {WbDb=} instance The instance to wait on.
|
||||||
|
* @param {boolean=} anyChange Whether any change is enough (default only open).
|
||||||
*/
|
*/
|
||||||
function waitForChange(instance) {
|
function waitForChange(instance, anyChange) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
(instance ?? inst).addEventListener(WbDb.EVENT_CHANGE, function() {
|
(instance ?? inst).addEventListener(WbDb.EVENT_CHANGE, function() {
|
||||||
resolve();
|
if (anyChange === true || (instance ?? inst).open)
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -99,7 +101,7 @@ export default function() {
|
|||||||
assert.strictEqual(first.db.version, 1, "first instance is version 1");
|
assert.strictEqual(first.db.version, 1, "first instance is version 1");
|
||||||
|
|
||||||
inst = WbDb.get(true, 2);
|
inst = WbDb.get(true, 2);
|
||||||
await waitForChange();
|
await waitForChange(inst, true);
|
||||||
assert.true(inst.blocked, "second instance blocked");
|
assert.true(inst.blocked, "second instance blocked");
|
||||||
assert.false(inst.open, "second instance not open");
|
assert.false(inst.open, "second instance not open");
|
||||||
assert.false(inst.failed, "second instance not failed");
|
assert.false(inst.failed, "second instance not failed");
|
||||||
@ -120,9 +122,11 @@ export default function() {
|
|||||||
first.db.close();
|
first.db.close();
|
||||||
|
|
||||||
let second = WbDb.get(true, 1)
|
let second = WbDb.get(true, 1)
|
||||||
await waitForChange(second);
|
await waitForChange(second, true);
|
||||||
if (second.blocked)
|
if (second.blocked) {
|
||||||
await waitForChange(second);
|
await waitForChange(second, true);
|
||||||
|
assert.true(false, "opening of old db is never blocked");
|
||||||
|
}
|
||||||
|
|
||||||
assert.false(second.blocked, "second instance not blocked");
|
assert.false(second.blocked, "second instance not blocked");
|
||||||
assert.false(second.open, "second instance not open");
|
assert.false(second.open, "second instance not open");
|
||||||
|
|||||||
@ -22,10 +22,11 @@ let inst = null;
|
|||||||
*
|
*
|
||||||
* @param {WbDb=} instance The instance to wait on.
|
* @param {WbDb=} instance The instance to wait on.
|
||||||
*/
|
*/
|
||||||
function waitForChange(instance) {
|
function waitForOpen(instance) {
|
||||||
return new Promise(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
(instance ?? inst).addEventListener(WbDb.EVENT_CHANGE, function() {
|
(instance ?? inst).addEventListener(WbDb.EVENT_CHANGE, function() {
|
||||||
resolve();
|
if ((instance ?? inst).open)
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -59,14 +60,14 @@ export default function() {
|
|||||||
|
|
||||||
QUnit.test("initially no sessions", async function(assert) {
|
QUnit.test("initially no sessions", async function(assert) {
|
||||||
inst = WbDb.get(true);
|
inst = WbDb.get(true);
|
||||||
await waitForChange(inst);
|
await waitForOpen(inst);
|
||||||
let sessions = await SessionRepo.getAll(inst);
|
let sessions = await SessionRepo.getAll(inst);
|
||||||
assert.strictEqual(sessions.length, 0, "no sessions");
|
assert.strictEqual(sessions.length, 0, "no sessions");
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("store single session", async function(assert) {
|
QUnit.test("store single session", async function(assert) {
|
||||||
inst = WbDb.get(true);
|
inst = WbDb.get(true);
|
||||||
let req = waitForChange(inst);
|
let req = waitForOpen(inst);
|
||||||
|
|
||||||
let session = new Session();
|
let session = new Session();
|
||||||
session.ourTeam = "This is us!";
|
session.ourTeam = "This is us!";
|
||||||
@ -88,7 +89,7 @@ export default function() {
|
|||||||
|
|
||||||
QUnit.test("store two sessions", async function(assert) {
|
QUnit.test("store two sessions", async function(assert) {
|
||||||
inst = WbDb.get(true);
|
inst = WbDb.get(true);
|
||||||
let req = waitForChange(inst);
|
let req = waitForOpen(inst);
|
||||||
|
|
||||||
let first = new Session();
|
let first = new Session();
|
||||||
first.ourTeam = "Team A";
|
first.ourTeam = "Team A";
|
||||||
@ -130,7 +131,7 @@ export default function() {
|
|||||||
|
|
||||||
QUnit.test("new session reacts to changes", async function(assert) {
|
QUnit.test("new session reacts to changes", async function(assert) {
|
||||||
inst = WbDb.get(true);
|
inst = WbDb.get(true);
|
||||||
await waitForChange(inst);
|
await waitForOpen(inst);
|
||||||
|
|
||||||
let session = new Session();
|
let session = new Session();
|
||||||
let id = await SessionRepo.put(session, inst);
|
let id = await SessionRepo.put(session, inst);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user