1
0

implement session deletion on data layer

This commit is contained in:
Adrian Wannenmacher 2026-03-09 20:48:50 +01:00
parent 1ef08a29a4
commit fbe785c1c4
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
2 changed files with 40 additions and 0 deletions

View File

@ -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.

View File

@ -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 = {