1
0

refactor SessionRepo change tracking

It was gettig tedious to add the change tracking logic in all of the
methods. And since there likely will be many more such methods, I have
now extracted that bit into its own function.
This commit is contained in:
Adrian Wannenmacher 2026-02-27 22:58:34 +01:00
parent ded78b09e2
commit a7012657b3
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5

View File

@ -68,6 +68,25 @@ export default class SessionRepo {
SessionRepo.put(this, this[SessionRepo.#marker]); SessionRepo.put(this, this[SessionRepo.#marker]);
} }
/** Set up the change handling mechanism for the provided object. Also turn it
* into a `Session` model, if it isn't one yet.
*
* @param {Session | any} value The session to set up.
* @param {IDBDatabase} db The database to update the session in.
*
* @returns {Session} The change-handling session.
*/
static #setupChangeHandling(value, db) {
let session = (value instanceof Session) ? value : new Session(value);
if (session[SessionRepo.#marker] === undefined) {
session.addEventListener(Session.EVENT_CHANGE, SessionRepo.#handleChange);
session[SessionRepo.#marker] = db;
}
return session;
}
/** Put a session into the repository. /** Put a session into the repository.
* *
* If the passed session has no `id` set, the newly stored ID will be * If the passed session has no `id` set, the newly stored ID will be
@ -95,12 +114,8 @@ export default class SessionRepo {
if (session.id === null) if (session.id === null)
alt = alt.then((id) => session.id = id); alt = alt.then((id) => session.id = id);
// add change listener t oobject. // add change listener to object.
alt.then(() => { if (session[SessionRepo.#marker] === undefined) { alt.then(() => SessionRepo.#setupChangeHandling(session, transaction.db));
session.addEventListener(
Session.EVENT_CHANGE, SessionRepo.#handleChange);
session[SessionRepo.#marker] = transaction.db;
}});
// make sure alt is handled first // make sure alt is handled first
return req.then(res => res); return req.then(res => res);
@ -120,10 +135,7 @@ export default class SessionRepo {
let session = await requestToPromise(sessions.get(key)); let session = await requestToPromise(sessions.get(key));
if (session !== undefined) { if (session !== undefined) {
session = new Session(session); session = SessionRepo.#setupChangeHandling(session, transaction.db);
session.addEventListener(
Session.EVENT_CHANGE, SessionRepo.#handleChange);
session[SessionRepo.#marker] = transaction.db;
} }
return session; return session;
} }
@ -139,11 +151,7 @@ export default class SessionRepo {
let sessions = transaction.objectStore(WbDb.OS_SESSIONS); let sessions = transaction.objectStore(WbDb.OS_SESSIONS);
sessions = await requestToPromise(sessions.getAll()); sessions = await requestToPromise(sessions.getAll());
return sessions.map(function(session) { return sessions.map(
let res = new Session(session); (session) => SessionRepo.#setupChangeHandling(session, transaction.db));
res.addEventListener(Session.EVENT_CHANGE, SessionRepo.#handleChange);
res[SessionRepo.#marker] = transaction.db;
return res;
});
} }
} }