diff --git a/data/session_repo.js b/data/session_repo.js index 146f1aa..d216423 100644 --- a/data/session_repo.js +++ b/data/session_repo.js @@ -65,6 +65,8 @@ export default class SessionRepo { static async #handleChange() { if (!(this instanceof Session)) throw new TypeError("session to put in must be an actual Session"); + if (!(SessionRepo.#marker in this)) + return; SessionRepo.put(this, this[SessionRepo.#marker]); } @@ -173,6 +175,29 @@ export default class SessionRepo { (session) => SessionRepo.#setupChangeHandling(session, transaction.db)); } + /** Delete a specific session. + * + * This also tears down the auto updating if the passed session is an + * instance of `Session`. + * + * @param {number | Session} session The session or session ID to delete. + * @param {Transactable=} transaction A transaction to use. + */ + static async delete(session, transaction) { + transaction = toTransaction(transaction, [WbDb.OS_SESSIONS], "readwrite"); + let sessions = transaction.objectStore(WbDb.OS_SESSIONS); + + if (session instanceof Session) { + session.removeEventListener(Session.EVENT_CHANGE, SessionRepo.#handleChange); + delete this[SessionRepo.#marker]; + session = session.id; + } + if (typeof session !== "number") + throw new TypeError("session to delete must be session or session ID"); + + return requestToPromise(sessions.delete(session)); + } + /** Load all sessions, parse them, then reinsert them. * * Does not set the intermediate objects up for change handling. diff --git a/data/session_repo.test.js b/data/session_repo.test.js index bffee20..291e3b1 100644 --- a/data/session_repo.test.js +++ b/data/session_repo.test.js @@ -207,6 +207,21 @@ export default function() { assert.strictEqual(sessions[1].id, second.id, "second is older"); }); + QUnit.test("delete session", async function(assert) { + inst = WbDb.get(true); + await waitForOpen(inst); + + let session = new Session(); + await SessionRepo.put(session, inst); + let stored = await SessionRepo.get(session.id, inst); + assert.notStrictEqual(stored, undefined, "session stored"); + assert.strictEqual(stored.id, session.id, "IDs match"); + + await SessionRepo.delete(session.id, inst); + stored = await SessionRepo.get(session.id, inst); + assert.strictEqual(stored, undefined, "no longer stored"); + }); + QUnit.test("reinserting all sessions", async function(assert) { // old structurized session (see v1 tests of session model) const old = {