From 353c441722e5f3de2d12bdd219cb663e2a8253bd Mon Sep 17 00:00:00 2001 From: Adrian Wannenmacher Date: Tue, 17 Feb 2026 20:41:08 +0100 Subject: [PATCH] add repo method to load specific session --- data/session_repo.js | 46 +++++++++++++++++++++++++++++---------- data/session_repo.test.js | 7 +++--- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/data/session_repo.js b/data/session_repo.js index 31fb5f5..790e2a0 100644 --- a/data/session_repo.js +++ b/data/session_repo.js @@ -56,6 +56,18 @@ export default class SessionRepo { throw new TypeError("SessionRepo cannot be constructed"); } + /** The symbol used to attach needed data to sessions. */ + static #marker = Symbol("data/session_store"); + + /** Handle the Session.EVENT_CHANGE event, by storing the changed session in + * the DB. + */ + static async #handleChange() { + if (!(this instanceof Session)) + throw new TypeError("session to put in must be an actual Session"); + SessionRepo.put(this, this[SessionRepo.#marker]); + } + /** Put a session into the repository. * * If the passed session has no `id` set, the newly stored ID will be @@ -94,6 +106,28 @@ export default class SessionRepo { return req.then(res => res); } + /** Get a specific session from the repository. + * + * @param {number} key The ID of the session to retrieve. + * @param {Transactable} transaction A transaction to use. + * + * @returns {Promise} + * The requested session, or undefined if it does not exist. + */ + static async get(key, transaction) { + transaction = toTransaction(transaction, [WbDb.OS_SESSIONS], "readonly"); + let sessions = transaction.objectStore(WbDb.OS_SESSIONS); + + let session = await requestToPromise(sessions.get(key)); + if (session !== undefined) { + session = new Session(session); + session.addEventListener( + Session.EVENT_CHANGE, SessionRepo.#handleChange); + session[SessionRepo.#marker] = transaction.db; + } + return session; + } + /** Get all sessions in the repository. * * @param {Transactable} transaction A transaction to use. @@ -112,16 +146,4 @@ export default class SessionRepo { return res; }); } - - /** The symbol used to attach needed data to sessions. */ - static #marker = Symbol("data/session_store"); - - /** Handle the Session.EVENT_CHANGE event, by storing the changed session in - * the DB. - */ - static async #handleChange() { - if (!(this instanceof Session)) - throw new TypeError("session to put in must be an actual Session"); - SessionRepo.put(this, this[SessionRepo.#marker]); - } } diff --git a/data/session_repo.test.js b/data/session_repo.test.js index 049eae6..b6012ab 100644 --- a/data/session_repo.test.js +++ b/data/session_repo.test.js @@ -81,10 +81,9 @@ export default function() { let id = await SessionRepo.put(session, inst); assert.strictEqual(session.id, id, "session id has been updated"); - let sessions = await SessionRepo.getAll(inst); - assert.strictEqual(sessions.length, 1, "one stored session"); + let stored = await SessionRepo.get(id, inst); assert.deepEqual( - sessions[0].toStruct(), session.toStruct(), "sessions match"); + stored.toStruct(), session.toStruct(), "sessions match"); }); QUnit.test("store two sessions", async function(assert) { @@ -152,5 +151,7 @@ export default function() { assert.deepEqual( sessions[0].toStruct(), session.toStruct(), "sessions match"); }); + + }); }