From 36959020416d0a4de8772cf831b4c51095f6b18b Mon Sep 17 00:00:00 2001 From: Adrian Wannenmacher Date: Fri, 27 Feb 2026 23:06:38 +0100 Subject: [PATCH] add time ordered session db accessor --- data/session_repo.js | 18 ++++++++++++++ data/session_repo.test.js | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/data/session_repo.js b/data/session_repo.js index eba0cef..146f1aa 100644 --- a/data/session_repo.js +++ b/data/session_repo.js @@ -155,6 +155,24 @@ export default class SessionRepo { (session) => SessionRepo.#setupChangeHandling(session, transaction.db)); } + /** Get all sessions in the repository, most recently updated first. + * + * @param {Transactable=} transaction A transaction to use. + * + * @returns {Promise} + * A promise containing the sorted stored sessions. + */ + static async getAllFromNewest(transaction) { + transaction = toTransaction(transaction, [WbDb.OS_SESSIONS], "readonly"); + let sessions = transaction + .objectStore(WbDb.OS_SESSIONS) + .index(WbDb.IDX_SESSIONS_UPDATED); + + sessions = await requestToPromise(sessions.getAll()); + return sessions.reverse().map( + (session) => SessionRepo.#setupChangeHandling(session, transaction.db)); + } + /** 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 1e7e4ed..bffee20 100644 --- a/data/session_repo.test.js +++ b/data/session_repo.test.js @@ -157,6 +157,55 @@ export default function() { sessions[0].toStruct(), session.toStruct(), "sessions match"); }); + QUnit.test("get sessions by age", async function(assert) { + inst = WbDb.get(true); + let req = waitForOpen(inst); + + let first = new Session(); + first.ourTeam = "Team A"; + first.theirTeam = "Team 1"; + first.goal = 2; + first.anotherGame(); + first.currentGame.currentRound.winner = Team.We; + first.anotherGame(); + + // ensure the timestamp is no longer the same + await new Promise((resolve) => setTimeout(resolve, 1)); + + let second = new Session(); + second.ourTeam = "Team B"; + second.theirTeam = "Team 2"; + second.goal = 3; + second.anotherGame(); + second.currentGame.currentRound.raise(Team.We); + second.currentGame.currentRound.winner = Team.They; + + await req; + await Promise.all([ + SessionRepo.put(first, inst), + SessionRepo.put(second, inst), + new Promise((resolve) => setTimeout(resolve, 1)), + ]); + + // check after initial storage + let sessions = await SessionRepo.getAllFromNewest(inst); + assert.strictEqual(sessions.length, 2, "exactly two sessions stored"); + assert.strictEqual(sessions[0].id, second.id, "second is newer"); + assert.strictEqual(sessions[1].id, first.id, "first is older"); + + // change older session + first.currentGame.currentRound.winner = Team.They; + first.anotherGame(); + + // give the change events a chance to execute + await new Promise((resolve) => setTimeout(resolve, 10)); + + // check whether order has changed + sessions = await SessionRepo.getAllFromNewest(inst); + assert.strictEqual(sessions.length, 2, "exactly two sessions stored"); + assert.strictEqual(sessions[0].id, first.id, "first is newer"); + assert.strictEqual(sessions[1].id, second.id, "second is older"); + }); QUnit.test("reinserting all sessions", async function(assert) { // old structurized session (see v1 tests of session model)