From a24042bc7b7d7efee789071e4d5dcfc397ebc858 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 16 Apr 2021 22:56:10 +0200 Subject: [PATCH] Use facets to be more robust against moderator leaving and rejoining --- src/secure-chat-client.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/secure-chat-client.ts b/src/secure-chat-client.ts index 9bae5fd..d0d1008 100644 --- a/src/secure-chat-client.ts +++ b/src/secure-chat-client.ts @@ -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());