update stored session when the model changes
This commit is contained in:
parent
ffd3529055
commit
ad9f9035a1
@ -69,16 +69,29 @@ export default class SessionRepo {
|
|||||||
static put(session, transaction) {
|
static put(session, transaction) {
|
||||||
if (!(session instanceof Session))
|
if (!(session instanceof Session))
|
||||||
throw new TypeError("session to put in must be an actual Session");
|
throw new TypeError("session to put in must be an actual Session");
|
||||||
|
|
||||||
transaction = toTransaction(transaction, [WbDb.OS_SESSIONS], "readwrite");
|
transaction = toTransaction(transaction, [WbDb.OS_SESSIONS], "readwrite");
|
||||||
let sessions = transaction.objectStore(WbDb.OS_SESSIONS);
|
let sessions = transaction.objectStore(WbDb.OS_SESSIONS);
|
||||||
|
|
||||||
let struct = session.toStruct();
|
let struct = session.toStruct();
|
||||||
let req = requestToPromise(sessions.put(struct));
|
let req = requestToPromise(sessions.put(struct));
|
||||||
|
|
||||||
if (session.id === null)
|
// promise with which the session object can be altered
|
||||||
req.then((id) => session.id = id);
|
let alt = req;
|
||||||
|
|
||||||
return req;
|
// add id to original object if it is new
|
||||||
|
if (session.id === null)
|
||||||
|
alt = alt.then((id) => session.id = id);
|
||||||
|
|
||||||
|
// add change listener t oobject.
|
||||||
|
alt.then(() => { if (session[SessionRepo.#marker] === undefined) {
|
||||||
|
session.addEventListener(
|
||||||
|
Session.EVENT_CHANGE, SessionRepo.#handleChange);
|
||||||
|
session[SessionRepo.#marker] = transaction.db;
|
||||||
|
}});
|
||||||
|
|
||||||
|
// make sure alt is handled first
|
||||||
|
return req.then(res => res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get all sessions in the repository.
|
/** Get all sessions in the repository.
|
||||||
@ -92,6 +105,23 @@ 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((session) => new Session(session));
|
return sessions.map(function(session) {
|
||||||
|
let res = new Session(session);
|
||||||
|
res.addEventListener(Session.EVENT_CHANGE, SessionRepo.#handleChange);
|
||||||
|
res[SessionRepo.#marker] = transaction.db;
|
||||||
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -128,5 +128,29 @@ export default function() {
|
|||||||
assert.deepEqual(session.toStruct(), expected, "sessions match");
|
assert.deepEqual(session.toStruct(), expected, "sessions match");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test("new session reacts to changes", async function(assert) {
|
||||||
|
inst = WbDb.get(true);
|
||||||
|
await waitForChange(inst);
|
||||||
|
|
||||||
|
let session = new Session();
|
||||||
|
let id = await SessionRepo.put(session, inst);
|
||||||
|
assert.strictEqual(session.id, id, "session id has been updated");
|
||||||
|
|
||||||
|
session.ourTeam = "This is us!";
|
||||||
|
session.theirTeam = "This is them!";
|
||||||
|
session.goal = 2;
|
||||||
|
session.anotherGame();
|
||||||
|
session.currentGame.currentRound.winner = Team.We;
|
||||||
|
console.log("too late");
|
||||||
|
|
||||||
|
// give the change events a chance to execute
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||||
|
|
||||||
|
let sessions = await SessionRepo.getAll(inst);
|
||||||
|
assert.strictEqual(sessions.length, 1, "exactly one stored session");
|
||||||
|
assert.deepEqual(
|
||||||
|
sessions[0].toStruct(), session.toStruct(), "sessions match");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user