// base44/actors/BoardRoom/entry.ts (plain JS)
import { Actor } from 'base44:runtime/actors'; // the ONLY import that resolves the Actor base
const MAX_USERS = 32; // a room is single-threaded — admit only what one instance can serve
export default class BoardRoom extends Actor {
// Keyed by conn.id (the SDK reuses it on reconnect, so entries are reclaimable) —
// but conn.id is CLIENT-HELD and must NEVER appear in broadcasts: broadcast state
// is keyed by the server-assigned seat instead.
users = new Map(); // conn.id -> { seat, cursor }
nextSeat = 1;
items = new Map(); // the shared, persisted room state
async handleStart() {
// Runs on ANY wake (deploy restart, idle, hibernation) — rehydrate, then
// reconcile: a hibernation wake keeps sockets ATTACHED without re-running
// handleConnect, so a still-live id must keep its seat, not lose it.
const saved = await this.storage.get('items');
if (saved) this.items = new Map(saved);
const seats = await this.storage.get('seats');
if (seats) this.users = new Map(seats);
const live = new Set(this.getConnections().map((c) => c.id));
for (const id of this.users.keys()) if (!live.has(id)) this.users.delete(id);
this.nextSeat = Math.max(0, ...[...this.users.values()].map((u) => u.seat)) + 1;
}
async handleConnect(conn) {
if (!this.users.has(conn.id) && this.users.size >= MAX_USERS) {
conn.reject(4001, 'room full'); // reject closes the socket but NOT this handler —
return; // return immediately
}
// Reconnects are ROUTINE (blips, reloads, redeploys): a returning id reclaims
// its entry — never demote it or mint a new seat.
if (!this.users.has(conn.id)) {
this.users.set(conn.id, { seat: this.nextSeat++, cursor: null });
await this.saveSeats(); // at seat milestones, never per cursor move
}
conn.send({ type: 'you', seat: this.users.get(conn.id).seat });
conn.send({ type: 'state', items: [...this.items.values()] }); // late joiners get full state
this.broadcastPresence();
}
async handleMessage(conn, msg) {
// Runtime-validate EVERYTHING: the wire payload is attacker-controlled and
// msg can even be null. Accept operations, never authoritative state.
if (typeof msg !== 'object' || msg === null) return;
const u = this.users.get(conn.id);
if (!u) return;
if (msg.type === 'cursor') {
u.cursor = [Number(msg.x) || 0, Number(msg.y) || 0];
this.broadcastPresence();
} else if (msg.type === 'upsert_item' && typeof msg.id === 'string' && msg.id.length <= 64) {
const item = { id: msg.id, text: String(msg.text ?? '').slice(0, 2000) };
this.items.set(item.id, item);
await this.storage.put('items', [...this.items.entries()]); // persist on change — never on every pointer move
this.broadcast({ type: 'item', item });
}
}
async handleClose(conn) {
this.users.delete(conn.id);
await this.saveSeats();
this.broadcastPresence();
}
saveSeats() { return this.storage.put('seats', [...this.users.entries()]); }
broadcastPresence() {
// PROJECT an explicit public shape — never spread whole server objects into a
// broadcast (they grow per-user secrets later).
this.broadcast({
type: 'presence',
users: [...this.users.values()].map((u) => ({ seat: u.seat, cursor: u.cursor })),
});
}
}