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';
export default function (t: Turn, ds: Ref) {
observe(t, ds, $joinedUser, {
assert(t, j0) {
const j = asJoin(j0);
runSession(t, j.uid, j.handle);
}
});
observe(t, ds, $joinedUser, during((t, j0) => {
const j = asJoin(j0);
const facet = t.facet(t => runSession(t, j.uid, j.handle));
return t => t.stop(facet);
}));
}
function runSession(t: Turn, uid: UserId, session: Ref) {
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) {
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, {
retract(t) { t.stop(); },
retract(t) {
cleanShutdown = true;
t.stopActor();
},
message(t, line: string) {
if (line.toLowerCase().startsWith('/nick ')) {
updateUsername(t, line.slice(5).trimLeft());