Skip to content

Commit d1399f1

Browse files
committed
First pass at refresh on every websocket reconnect
1 parent 9be7f24 commit d1399f1

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

html.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ export const Page = ({ body, title, mixpanelToken, myRcUserId }) => html`
6565
</html>
6666
`;
6767

68-
export const RootBody = ({ rooms, whoIsInTheHub, personalizations }) => {
68+
export const RootBody = ({ roomListContent, personalizations, sort }) => {
6969
let body = "";
70-
body += `<main hx-ext="ws" ws-connect="/websocket">`;
70+
// Propogate settings back to the websocket connection which are not stored in
71+
// a cookie or are otherwise stateless
72+
body += `<main hx-ext="ws" ws-connect="/websocket?sort=${escapeHtml(sort ?? "")}" id="main">`;
7173
body += html`<h1>RCVerse</h1>`;
7274
body += useSnippet("./html/about.snippet.html");
7375
body += `<details>`;
@@ -141,10 +143,7 @@ export const RootBody = ({ rooms, whoIsInTheHub, personalizations }) => {
141143
</script>`;
142144
body += `</details>`;
143145
body += html`
144-
<dl class="room-list">
145-
${WhoIsInTheHub(whoIsInTheHub)} ${rooms.map(Room).join("\n")}
146-
</dl>
147-
146+
${roomListContent}
148147
<hl />
149148
<p>You're logged in! - <a href="/logout">log out</a></p>
150149
`;
@@ -154,6 +153,12 @@ export const RootBody = ({ rooms, whoIsInTheHub, personalizations }) => {
154153
return body;
155154
};
156155

156+
export const RoomList = ({ whoIsInTheHub, rooms }) => html`
157+
<dl class="room-list" id="room-list">
158+
${WhoIsInTheHub(whoIsInTheHub)} ${rooms.map(Room).join("\n")}
159+
</dl>
160+
`;
161+
157162
export const JSInclude = ({ url }) => html`<script src="${url}"></script>`;
158163
export const CSSInclude = ({ url }) =>
159164
html`<link rel="stylesheet" type="text/css" href="${url}" />`;

index.js

+48-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
Login,
2424
Personalization,
2525
escapeHtml,
26+
RoomList,
2627
} from "./html.js";
2728
import expressWebsockets from "express-ws";
2829
import ical from "node-ical";
@@ -634,14 +635,18 @@ app.get(
634635
title: "RCVerse",
635636
body: RootBody(
636637
mungeRootBody({
637-
zoomRooms,
638-
roomNameToParticipantNames,
639-
participantNameToEntity,
640-
roomNameToNote,
641-
myParticipantName: req.locals.rcPersonName,
642-
inTheHubParticipantNames,
643-
sortRooms,
644-
locationToNowAndNextEvents,
638+
roomListContent: RoomList(
639+
mungeRoomList({
640+
zoomRooms,
641+
roomNameToParticipantNames,
642+
participantNameToEntity,
643+
roomNameToNote,
644+
myParticipantName: req.locals.rcPersonName,
645+
inTheHubParticipantNames,
646+
sortRooms,
647+
locationToNowAndNextEvents,
648+
}),
649+
),
645650
personalizations,
646651
}),
647652
),
@@ -683,6 +688,33 @@ app.ws("/websocket", async function (ws, req) {
683688

684689
await getRcUserMiddleware(req, { appendHeader: () => {} }, () => {});
685690

691+
let { sort } = req.query;
692+
693+
// `?sort=none` uses the default ordering instead of sort by count
694+
// TODO: This logic is repeated from above, move into munge
695+
const sortRooms = sort !== "none";
696+
697+
// TODO: Somehow distinguish between the first connection on page load and
698+
// a reconnection, and don't send this on the first connection
699+
// TODO: Somehow distinguish if any messages were actually missed on reconnection
700+
// and only do this if any missed messages
701+
// TODO: Add more complicated solution which replays chunks of missed messages
702+
// instead of refreshing so much of the page
703+
ws.send(
704+
RoomList(
705+
mungeRoomList({
706+
zoomRooms,
707+
roomNameToParticipantNames,
708+
participantNameToEntity,
709+
roomNameToNote,
710+
myParticipantName: req.locals.rcPersonName,
711+
inTheHubParticipantNames,
712+
sortRooms,
713+
locationToNowAndNextEvents,
714+
}),
715+
),
716+
);
717+
686718
// NOTE: Only use async listeners, so that each listener doesn't block.
687719
const roomListener = async (participantName, action, roomName) => {
688720
ws.send(
@@ -1122,7 +1154,14 @@ app.post(
11221154

11231155
// Data mungers take the craziness of the internal data structures
11241156
// and make them peaceful and clean for the HTML generator
1125-
const mungeRootBody = ({
1157+
const mungeRootBody = ({ roomListContent, personalizations }) => {
1158+
return {
1159+
roomListContent,
1160+
personalizations,
1161+
};
1162+
};
1163+
1164+
const mungeRoomList = ({
11261165
zoomRooms,
11271166
roomNameToParticipantNames,
11281167
participantNameToEntity,
@@ -1131,7 +1170,6 @@ const mungeRootBody = ({
11311170
inTheHubParticipantNames,
11321171
sortRooms,
11331172
locationToNowAndNextEvents,
1134-
personalizations,
11351173
}) => {
11361174
const whoIsInTheHub = mungeWhoIsInTheHub({
11371175
inTheHubParticipantNames,
@@ -1179,7 +1217,6 @@ const mungeRootBody = ({
11791217
return {
11801218
whoIsInTheHub,
11811219
rooms,
1182-
personalizations,
11831220
};
11841221
};
11851222

0 commit comments

Comments
 (0)