1
0

add time ordered session db accessor

This commit is contained in:
Adrian Wannenmacher 2026-02-27 23:06:38 +01:00
parent 0065514885
commit 3695902041
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
2 changed files with 67 additions and 0 deletions

View File

@ -155,6 +155,24 @@ export default class SessionRepo {
(session) => SessionRepo.#setupChangeHandling(session, transaction.db)); (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<Session[]>}
* 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. /** Load all sessions, parse them, then reinsert them.
* *
* Does not set the intermediate objects up for change handling. * Does not set the intermediate objects up for change handling.

View File

@ -157,6 +157,55 @@ export default function() {
sessions[0].toStruct(), session.toStruct(), "sessions match"); 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) { QUnit.test("reinserting all sessions", async function(assert) {
// old structurized session (see v1 tests of session model) // old structurized session (see v1 tests of session model)