Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
presence: joined and left are said once per real arrival and departure (#522)
nyblnetea8ab57ff386
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex a1f3c309..0443dce9 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -379,6 +379,13 @@ jobs:# file carrying a script tag.run: node scripts/test-preview.ts+ - name: presence toasts — said once per real arrival and departure+ # A hidden tab's heartbeat arrives once a minute, the room drops the+ # peer and the next beat brings them back: "left" / "joined" every+ # minute for everyone else. The grace and the quiet window are what+ # keep that silent; this rig runs them on a manual clock.+ run: node scripts/test-slides-presence-toasts.ts+- name: Media autoplay rig# An author set a clip's Autoplay to Off and it played anyway, every# show, since media shipped in v0.9.16. render.ts wrotediff --git a/CHANGELOG.md b/CHANGELOG.mdindex 30501f4e..19bdb692 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,14 @@ pre-1.0.## [Unreleased]+- **"Joined" and "left" are said once per real arrival and departure.** A+ collaborator who switched to another tab for a few minutes was announced+ as leaving and joining once a minute, to everyone in the room, while+ doing nothing: browsers slow a hidden tab's timers to once a minute, so+ their presence heartbeat arrived late, the room dropped them, and the next+ beat brought them back. A departure is now announced only once it has+ lasted, and a return within a few minutes is not a new arrival. The avatar+ strip still follows presence exactly; this changes only what gets said.- **Compress the pictures already in a deck.** 1.2.0 shrinks a photo as youinsert it; a deck made before that still carries its photos at full size.About ▸ *Compress pictures in this deck…* runs every picture through thediff --git a/scripts/test-slides-presence-toasts.ts b/scripts/test-slides-presence-toasts.tsnew file mode 100644index 00000000..6813e24f--- /dev/null+++ b/scripts/test-slides-presence-toasts.ts@@ -0,0 +1,77 @@+#!/usr/bin/env node+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors+// "X joined" / "X left" said once per REAL arrival and departure+// (slides/src/editor/presencetoasts.ts):+//+// node scripts/test-slides-presence-toasts.ts+//+// WHAT THIS PROVES, on a manual clock. A peer dropped by the presence TTL+// and back on the next heartbeat — the once-a-minute flap of a tab Chrome+// has throttled — is announced NEITHER as left NOR as joined. A departure+// that lasts past the grace is announced once, and a return after the+// quiet window is a real join again. A genuine first arrival is announced+// at once. A crowded room says nothing. dispose() cancels what is pending.++import { PresenceToasts } from '../slides/src/editor/presencetoasts.ts'++let failures = 0, checks = 0+const ok = (cond: boolean, msg: string) => { checks++; if (!cond) { failures++; console.log(` FAIL ${msg}`) } else console.log(` ok ${msg}`) }++// a manual clock with timers+let now = 1_000_000+const timers: Array<{ at: number; fn: () => void; id: number }> = []+let seq = 0+const setTimer = (fn: () => void, ms: number) => { const id = ++seq; timers.push({ at: now + ms, fn, id }); return id }+const clearTimer = (id: unknown) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1) }+const advance = (ms: number) => { now += ms; for (const t of timers.splice(0).sort((a, b) => a.at - b.at)) { if (t.at <= now) t.fn(); else timers.push(t) } }+const said: string[] = []+const mk = (initial: Array<{ actor: string; name: string }> = []) => new PresenceToasts({ toast: (k, n) => said.push(`${n} ${k}`), leaveGraceMs: 45_000, rejoinQuietMs: 300_000, maxRoom: 8, now: () => now, setTimer, clearTimer }, initial)+const A = { actor: 'a', name: 'Ana' }, B = { actor: 'b', name: 'Ben' }++console.log('\nthe throttled-tab flap')+{+ const p = mk([A, B])+ p.update([A]) // Ben's beat is late: the TTL dropped him+ ok(said.length === 0, 'a departure is not announced at once')+ advance(20_000)+ p.update([A, B]) // his next beat lands (a throttled tab beats once a minute)+ advance(60_000)+ ok(said.length === 0, 'back within the grace: neither "left" nor "joined" — the flap is silent')+ for (let i = 0; i < 5; i++) { p.update([A]); advance(20_000); p.update([A, B]); advance(40_000) }+ ok(said.length === 0, 'five flaps in five minutes: still nothing said')+}++console.log('\na real departure, and a return')+{+ said.length = 0+ const p = mk([A, B])+ p.update([A])+ advance(44_000); ok(said.length === 0, 'not yet at 44 s')+ advance(2_000); ok(said.join() === 'Ben left', 'announced once, after the grace')+ advance(100_000)+ p.update([A, B])+ ok(said.join() === 'Ben left', 'back 2 minutes later: within the quiet window, not announced as a join')+ p.update([A]); advance(46_000)+ ok(said.join() === 'Ben left,Ben left', 'and gone again for good: left, once more')+ advance(400_000)+ p.update([A, B])+ ok(said.join() === 'Ben left,Ben left,Ben joined', 'back after the quiet window: a real join')+}++console.log('\na first arrival, a crowded room, dispose')+{+ said.length = 0+ const p = mk([A])+ p.update([A, B]); ok(said.join() === 'Ben joined', 'someone never seen: joined, at once')+ said.length = 0+ const many = Array.from({ length: 9 }, (_, i) => ({ actor: `m${i}`, name: `M${i}` }))+ p.update([A, B, ...many]); ok(said.length === 0, 'a room past 8 says nothing on the way in')+ p.update([A, B]); advance(60_000); ok(said.length === 0, 'nor on the way out')+ const q = mk([A, B])+ q.update([A]); q.dispose(); advance(60_000)+ ok(said.length === 0, 'dispose cancels a pending "left"')+}++console.log(`\n${checks - failures}/${checks} checks passed`)+process.exit(failures ? 1 : 0)diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.tsindex be9bbf5a..96396c51 100644--- a/slides/src/editor/editor.ts+++ b/slides/src/editor/editor.ts@@ -47,6 +47,7 @@ import { shrinkImageFile, shrinkEnabled, setShrinkEnabled, shrinkNote, fmtBytes,import { dryRun, applyCompress, type DryRun } from './compressdeck'import { createDialog } from '../../../kernel/src/ui/dialog.ts'import '../../../kernel/src/ui/dialog.css'+import { PresenceToasts } from './presencetoasts'const i18nT = t@@ -136,24 +137,18 @@ export class Editor {/** wire the live-collaboration session (avatars, remote selections, relay) */connectSync(session: import('../sync/session').SyncSession) {this.session = session- let known = new Map(session.peers().map((p) => [p.actor, p.name]))+ // presence arrivals/departures get a quiet heads-up — said once per REAL+ // arrival and departure (presencetoasts.ts: a departure must last, a+ // return within minutes is a flap of a throttled tab, not a join), and+ // not at all in a crowded room, where the per-peer toasts would storm+ const toasts = new PresenceToasts({+ toast: (kind, name) => this.toast(kind === 'joined' ? t('{name} joined', { name }) : t('{name} left', { name })),+ }, session.peers())session.onPeers(() => {this.renderAvatars()this.canvas.setRemotePeers(session.peers())if (this.shareWrap.classList.contains('open')) this.renderSharePanel()- // presence arrivals/departures get a quiet heads-up — but in a crowded- // room (or when joining one, where every existing peer looks like a fresh- // arrival), the per-peer toasts would storm. Stay silent past a threshold.- const now = new Map(session.peers().map((p) => [p.actor, p.name]))- if (now.size <= 8) {- for (const [actor, name] of now) {- if (!known.has(actor)) this.toast(t('{name} joined', { name }))- }- for (const [actor, name] of known) {- if (!now.has(actor)) this.toast(t('{name} left', { name }))- }- }- known = now+ toasts.update(session.peers())})// the relay refused something (too big, room full, throttled) — the user// needs to know, because for the permanent codes their change stays indiff --git a/slides/src/editor/presencetoasts.ts b/slides/src/editor/presencetoasts.tsnew file mode 100644index 00000000..69889419--- /dev/null+++ b/slides/src/editor/presencetoasts.ts@@ -0,0 +1,101 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors+//+// "X joined" / "X left" — said once per real arrival and departure, not once+// per flap. Pure (a clock and a toast function in, no DOM), so+// scripts/test-slides-presence-toasts.ts drives it in node.+//+// WHY. Presence is a heartbeat: every peer beats every few seconds and a peer+// that has not beaten for PEER_TTL is dropped (kernel/src/sync/session.ts).+// Browsers throttle timers in a hidden tab — Chrome to once a MINUTE after+// five minutes in the background, Safari sooner — so a collaborator who+// switched tabs beats late, gets dropped, and comes back on the next beat:+// "Ana left" … "Ana joined" … once a minute, for everyone else in the room,+// while Ana has done nothing. The same for a laptop lid, a tunnel, a Wi-Fi+// handover. Those are not arrivals and departures; they are the transport+// breathing. So: a departure is announced only after it has LASTED (the+// grace), and is cancelled by a return; an arrival is announced only for+// someone not seen recently (a return within the quiet window is a flap).+// The avatar strip still follows presence exactly — this is only what gets+// SAID.++export interface PresencePeer { actor: string; name: string }++export interface PresenceToastOpts {+ toast: (kind: 'joined' | 'left', name: string) => void+ /** a departure must last this long before it is announced */+ leaveGraceMs?: number+ /** a return this soon after being seen is a flap, not an arrival */+ rejoinQuietMs?: number+ /** rooms past this size say nothing (a joiner would see a storm) */+ maxRoom?: number+ now?: () => number+ setTimer?: (fn: () => void, ms: number) => unknown+ clearTimer?: (id: unknown) => void+}++export class PresenceToasts {+ private known = new Map<string, string>()+ /** when each actor was last present */+ private lastSeen = new Map<string, number>()+ private pendingLeave = new Map<string, unknown>()+ private readonly grace: number+ private readonly quiet: number+ private readonly maxRoom: number+ private readonly now: () => number+ private readonly setTimer: (fn: () => void, ms: number) => unknown+ private readonly clearTimer: (id: unknown) => void++ private readonly opts: PresenceToastOpts++ constructor(opts: PresenceToastOpts, initial: PresencePeer[] = []) {+ this.opts = opts+ this.grace = opts.leaveGraceMs ?? 45_000+ this.quiet = opts.rejoinQuietMs ?? 5 * 60_000+ this.maxRoom = opts.maxRoom ?? 8+ this.now = opts.now ?? (() => Date.now())+ this.setTimer = opts.setTimer ?? ((fn, ms) => setTimeout(fn, ms))+ this.clearTimer = opts.clearTimer ?? ((id) => clearTimeout(id as ReturnType<typeof setTimeout>))+ for (const p of initial) { this.known.set(p.actor, p.name); this.lastSeen.set(p.actor, this.now()) }+ }++ /** The room as it is now. Call on every presence change. */+ update(peers: PresencePeer[]) {+ const t = this.now()+ const now = new Map(peers.map((p) => [p.actor, p.name]))+ const quietRoom = now.size > this.maxRoom || this.known.size > this.maxRoom+ for (const [actor, name] of now) {+ const pending = this.pendingLeave.get(actor)+ if (pending !== undefined) {+ // back before the grace ran out: a flap, nothing was said, nothing is+ this.clearTimer(pending); this.pendingLeave.delete(actor)+ } else if (!this.known.has(actor)) {+ const seen = this.lastSeen.get(actor)+ const flap = seen !== undefined && t - seen < this.quiet+ if (!flap && !quietRoom) this.opts.toast('joined', name)+ }+ this.lastSeen.set(actor, t)+ }+ for (const [actor, name] of this.known) {+ if (now.has(actor) || this.pendingLeave.has(actor)) continue+ if (quietRoom) continue+ // gone — say so only if still gone after the grace+ const id = this.setTimer(() => {+ this.pendingLeave.delete(actor)+ this.known.delete(actor)+ this.opts.toast('left', name)+ }, this.grace)+ this.pendingLeave.set(actor, id)+ }+ // a peer that is pending-leave stays "known" until the toast fires or it returns+ const next = new Map(now)+ for (const [actor] of this.pendingLeave) if (!next.has(actor)) next.set(actor, this.known.get(actor) ?? '')+ this.known = next+ }++ /** Stop every pending announcement (the session is closing). */+ dispose() {+ for (const id of this.pendingLeave.values()) this.clearTimer(id)+ this.pendingLeave.clear()+ }+}