Use facets to be more robust against moderator leaving and rejoining

This commit is contained in:
Tony Garnock-Jones 2021-04-16 22:56:10 +02:00
parent a865a2dddd
commit a24042bc7b
1 changed files with 16 additions and 7 deletions

View File

@ -4,16 +4,22 @@ import { Assertion, Ref, Turn } from "./actor.js";
import { attachReadline } from './readline.js'; import { attachReadline } from './readline.js';
export default function (t: Turn, ds: Ref) { export default function (t: Turn, ds: Ref) {
observe(t, ds, $joinedUser, { observe(t, ds, $joinedUser, during((t, j0) => {
assert(t, j0) { const j = asJoin(j0);
const j = asJoin(j0); const facet = t.facet(t => runSession(t, j.uid, j.handle));
runSession(t, j.uid, j.handle); return t => t.stop(facet);
} }));
});
} }
function runSession(t: Turn, uid: UserId, session: Ref) { function runSession(t: Turn, uid: UserId, session: Ref) {
let username: string | undefined; let username: string | undefined;
let cleanShutdown = false;
t.activeFacet.onStop(_t => {
if (!cleanShutdown) {
console.log('Moderator retracted room permission.');
}
});
function updateUsername(t: Turn, name: string) { function updateUsername(t: Turn, name: string) {
const q = t.assert(session, fromNickClaim(NickClaim({ uid, name, k: t.ref({ const q = t.assert(session, fromNickClaim(NickClaim({ uid, name, k: t.ref({
@ -53,7 +59,10 @@ function runSession(t: Turn, uid: UserId, session: Ref) {
}); });
attachReadline(t, { attachReadline(t, {
retract(t) { t.stop(); }, retract(t) {
cleanShutdown = true;
t.stopActor();
},
message(t, line: string) { message(t, line: string) {
if (line.toLowerCase().startsWith('/nick ')) { if (line.toLowerCase().startsWith('/nick ')) {
updateUsername(t, line.slice(5).trimLeft()); updateUsername(t, line.slice(5).trimLeft());