Skip to content

Commit e48bf75

Browse files
authored
social: the peer-thinking row stops polling while the tab is hidden and reads once when it returns (#3333)
Fixes #3332 Bot: FlyBot Dev-3 Owner: Dev-3 Written-by: Dev-3 Reviewer: Birfy ## What the row did `PeerThinking` polls `social/typing` every 2 s to draw the "…is thinking" row in an open face-to-face room. It did that on a bare interval, whatever the tab was doing: - `plugins/social/client.js:2489` at the base: `const timer = setInterval(load, 2000)`, created unconditionally on mount. - The visibility check lived *inside* `load` (`:2484`, `if (typeof document !== 'undefined' && document.hidden) return`), so a hidden tab kept a timer firing every 2 s that returned before reading anything — one wake-up per 2 s for nothing, until unmount. - The effect registered no `visibilitychange` listener at all, so a tab that came back did not read at once: its first read waited out the rest of the current period (0–2 s). Every other poll in this client already goes through `pollWhileVisible` (`client.js:3059`, #3315, #3327): no timer while hidden, one read as the tab comes back, the cadence restarted from that read, the listener removed on unmount. This row was the next item on `#3327`'s follow-up list. ## What changes - `plugins/social/client.js` — the effect drops `setInterval` and the `document.hidden` early return inside `load` (the helper never calls it while hidden) and keeps `const stop = pollWhileVisible(load, 2000)`; unmount does `live = false; stop?.()` (`+2/−6`). - `plugins/social/test/poll-visibility.test.js` — one case on the shipped component through the file's existing `scene()` / `mount()` harness (`+27`): a hidden mount makes no `social/typing` read and keeps no poll timer, `visibilitychange` back to visible reads exactly once and the 2 s cadence restarts from that read, one period later reads once more, going hidden again stops it, unmount drops the timer. It counts observed calls over time rather than compare a golden. - `docs/subsystems/social.md` — one sentence beside the #3315/#3327 polling rule. - `changelog.d/3332.fixed.md`. Visible cadence is unchanged: the helper's `start()` calls `fn()` once and then `setInterval(fn, 2000)`, which is the same "read at mount, then every 2 s" shape the old `load(); setInterval(load, 2000)` had. No new string, so no dictionary rebuild and no client-bundle re-emit. Not touched: `plugins/social/src/**`, `core/**`, `core/src/client/i18n.js`, `patches/**`. ## No UI face The row's text, when it appears and its layout do not change, only when the reads happen while hidden. Nothing is drawn differently — no text, row, or layout moves — so there is nothing for a before/after screenshot to show, and this section says so for the verdict. ## Red first The new case with the base `plugins/social/client.js` (test file from this branch), the same command twice on the same tree at base `1a276c57874e3c45680d4bec10ae1bd6080777fd`: ``` $ cd plugins/social && node --import ./test/resolve.mjs --test test/poll-visibility.test.js # tests 7 | # pass 6 | # fail 1 (run 1) # tests 7 | # pass 6 | # fail 1 (run 2) not ok 7 - the peer-thinking row stops while hidden and reads once when the tab returns error: 'a hidden row holds no poll timer' actual: [ 2000 ] expected: [] at plugins/social/test/poll-visibility.test.js:275 ``` Both runs are the same shape and fail on the same assertion (`:275`): a hidden mount already keeps a 2 s poll timer, which is the defect and the line the case reads first. The six #3315/#3327 cases above it stay green throughout. The second half of the defect — a tab that comes back does not read at once — was probed on the same base during the READY with a one-off copy of the case that dropped the timer assertion: `# pass 0 | # fail 1`, `coming back made 0 read(s), not one` (`poll-visibility.test.js:278`), i.e. the old effect had no `visibilitychange` listener to read on. The committed case asserts both halves; the first one to fire is the timer. ## Verification at tip `af344f6c06199430eda1fac5a97323837fef2b0c` - tip `af344f6c06199430eda1fac5a97323837fef2b0c`, tree `816a934971ae8b3665adc920aef30285c2b0ff08`, base `1a276c57874e3c45680d4bec10ae1bd6080777fd` - `git diff --name-only 1a276c5..af344f6` → exactly the four files above (`client.js` +2/−6, test +27, docs +1, changelog +1) - `cd plugins/social && node --import ./test/resolve.mjs --test test/poll-visibility.test.js` → 7/7, `# fail 0`, rc 0 - `npm --prefix plugins/social test` → 768/768, 0 fail, rc 0 (unchanged base is 767/767; only this one case is added) - `node scripts/lint-plugins.mjs social` → `Found 42 warnings and 0 errors.`, rc 0 - `node .github/scripts/changelog.mjs check` → `changelog: 9 fragment(s) in changelog.d/ are well formed` - `ARCH_DOCS_BASE=1a276c57874e3c45680d4bec10ae1bd6080777fd node core/scripts/check-arch-docs.mjs` → `arch-docs: OK (4 changed file(s))` ## Checkpoint <!-- flybot:checkpoint --> ```json { "pr": 3333, "claim": "Dev-3", "issue": 3332, "summary": "The peer-thinking row kept a bare 2 s `setInterval` and checked `document.hidden` inside the read, so a hidden tab held a timer ticking for nothing and a tab that came back waited out the rest of the period instead of reading once. It now uses the existing `pollWhileVisible`, so a hidden tab holds no timer, coming back reads once at once and restarts the 2 s cadence from that read, and unmount removes the listener. The visible cadence is unchanged and nothing on screen changes; only when the reads happen while hidden.", "files": [ "plugins/social/client.js", "plugins/social/test/poll-visibility.test.js", "docs/subsystems/social.md", "changelog.d/3332.fixed.md" ], "checks": { "ran": "npm --prefix plugins/social test", "result": "green", "at": "af344f6c06199430eda1fac5a97323837fef2b0c" }, "next": "none — ready for review" } ``` <!-- /flybot:checkpoint -->
1 parent 1a276c5 commit e48bf75

4 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎changelog.d/3332.fixed.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The **peer-thinking** row in a face-to-face room no longer keeps a 2 s `social/typing` timer while the tab is hidden: the read checked `document.hidden` inside the interval, so a hidden tab held a timer ticking for nothing and a tab that came back waited out the rest of the period. It now goes through `pollWhileVisible`, so a hidden tab makes no call, coming back reads once at once and restarts the 2 s cadence from that read, and unmount removes the listener. The visible cadence is unchanged, and nothing on screen changes ([#3332](https://github.com/ThinkFlowLab/FlyBot/issues/3332)).

‎docs/subsystems/social.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Creating a group opens its Session with zero turns, and the host draws no sessio
122122

123123
- Refresh only what the person can see (#3315): the waiting room's 2 s and the room overview's 3 s `social/face-rooms` polls go through `pollWhileVisible`, so a hidden tab makes no call, coming back reads once at once, and the cadence restarts from that read (clear and rebuild) rather than from the hidden phase's remainder; unmount removes the `visibilitychange` listener.
124124
- The paused-chat card's 5 s `social/paused` read and the connecting page's 2 s `social/server` read are the same rule (#3327): both go through `pollWhileVisible`, so a hidden tab makes no call, coming back reads once at once and restarts the cadence from that read, and unmount removes the listener. Nothing on screen changes; only when the reads happen while hidden.
125+
- The peer-thinking row's 2 s `social/typing` read follows the same rule: `pollWhileVisible`, so a hidden tab makes no call, coming back reads once at once and restarts the 2 s cadence from that read, and unmount removes the listener. The visible cadence is unchanged, and nothing on screen changes.
125126
- A plain human line in an open face-to-face room is logged and fanned out once, and starts a single pass of every local Bot (admin first, capped at 6, no @ needed; #287, #295; core `openingSpeakers`, see [core-host](core-host.md)). Since #299 the Bots pick the responder from a plain line (they may `[PASS]`; the first Bot that answers ends the round unless the line is to everyone; the first Bot answers if all pass on the owner's own line), and a line to everyone or `@all` is unchanged. A friend's attested plain line (or `@all`) starts the same pass on this home, routed and single like the owner's own line (#462) (the plugin passes `attested: true` to `postHuman` only for a stamped line); an attested line that names Bots starts only the Bot(s) it names by the label the picker shows (`Friend's Chief`) or by handle; `plugins/social/test` has the two-home case. **Explicit mention list (#293):** the sender's host resolves the picker's labels against the room (`faceMentions().handles`; core `onFaceLine(fn)` passes `{ mentions }` for the owner's typed line) and `faceApi.send` puts the handles in `mentions` next to `typed_by_owner` only when `GET /v1/platform` `features` lists `mentions` (the one feature probe, `serverMentions()`); never on the legacy envelope, Bot lines or an older server. The receiver reads `mentions` (`cleanMentions`, at most 8 handles) only from a line the server attests `typed_by_owner` (`renderFaceEvent`), and `postHuman` resolves the handles against the room first (`mentionedByHandle`): only my own Bots in the list answer; a list naming nobody the room knows, or no list, falls back to the label/name reading above. The composer's `@` picker inserts the label text, so the handles are derived from it at send, not tracked in the composer.
126127
- The encoded human line is bounded at 18 000 **code points**, not UTF-16 units (#2641): `encodeFaceHuman` cuts with the file's own `cutCodePoints`, the same 18 000-code-point budget `coSeatEnvelope` applies to an outgoing room-share line, so a person's line whose bound falls inside an emoji ends on a code-point boundary instead of storing a lone surrogate that the next UTF-8 write turns into `U+FFFD`.
127128
- A peer string that arrives from the far side of Bot Social is capped on **code points** too (#2693): `cleanText` (60, so a friend's Bot or owner display name), `safeName` (120, an attached file's name), `draftText` (800), the person label `grantPersonView` builds in `mcp-client.js` (60), and the error and invite lines the host caps. All of them cut with one `cutCodePoints` in `src/code-points.js`, which `social.js` still re-exports (#2581), so a value whose bound falls inside an emoji keeps the emoji whole instead of ending on a lone high surrogate that a stored setting, a remote session name or a cross-home payload would show as `U+FFFD`; a value already stored that way is left unchanged rather than migrated.

‎plugins/social/client.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,14 +2481,10 @@ window.__ModuleLoader__.load({
24812481
if (!watching) { setItems([]); return undefined }
24822482
let live = true
24832483
const load = () => {
2484-
if (typeof document !== 'undefined' && document.hidden) return
24852484
call('social/typing', {}).then(value => { if (live) setItems(value?.supported === false ? [] : (Array.isArray(value?.typing) ? value.typing : [])) }, () => { if (live) setItems([]) })
24862485
}
2487-
load()
2488-
if (typeof setInterval !== 'function') return () => { live = false }
2489-
const timer = setInterval(load, 2000)
2490-
timer?.unref?.()
2491-
return () => { live = false; clearInterval(timer) }
2486+
const stop = pollWhileVisible(load, 2000)
2487+
return () => { live = false; stop?.() }
24922488
}, [watching, bot?.remote?.handle, room?.id, room?.face?.roomId, room?.face?.status])
24932489
const text = watching ? thinkingLine(thinkingNames(items, { room, bot })) : ''
24942490
return text ? h('div', { className: 'fs-thinking', 'data-flybot': 'peer-thinking', role: 'status', 'aria-live': 'polite' }, h('span', { className: 'bt-shimmer' }, text)) : null

‎plugins/social/test/poll-visibility.test.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,30 @@ test('#3315: the connecting settings page stops while hidden and reads once when
259259
unmount(cleanups)
260260
} finally { s.restore() }
261261
})
262+
263+
// The peer-thinking row (#443) is the same shape again: a bare 2 s `setInterval` with the visibility
264+
// check inside `load`, so a hidden tab keeps the timer ticking for nothing and a tab that comes back
265+
// waits out the rest of the period before its first read.
266+
const peerRoom = () => ({ id: 'r1', origin: 'face-to-face', face: { status: 'open', roomId: 'fr1' }, participants: [] })
267+
const peerThinking = app => mount(app, 'PeerThinking', { room: peerRoom(), bot: { id: 'b1', name: 'Chief', remote: { handle: '@chief.home' } } })
268+
269+
test('the peer-thinking row stops while hidden and reads once when the tab returns', () => {
270+
const s = scene({ hidden: true })
271+
try {
272+
const typingCalls = () => s.calls.filter(([endpoint]) => endpoint === 'social/typing').length
273+
const { cleanups } = peerThinking(s.app)
274+
assert.equal(typingCalls(), 0, 'a hidden row makes no read')
275+
assert.deepEqual(s.timers.periods(), [], 'a hidden row holds no poll timer')
276+
s.doc.hidden = false
277+
s.doc.emit('visibilitychange')
278+
assert.equal(typingCalls(), 1, `coming back made ${typingCalls()} read(s), not one`)
279+
assert.deepEqual(s.timers.periods(), [2000], 'the 2 s cadence restarts from that read')
280+
s.timers.tick()
281+
assert.equal(typingCalls(), 2, 'and one period later reads again, once')
282+
s.doc.hidden = true
283+
s.doc.emit('visibilitychange')
284+
s.timers.tick()
285+
assert.equal(typingCalls(), 2, 'going hidden again stops it')
286+
unmount(cleanups)
287+
} finally { s.restore() }
288+
})

0 commit comments

Comments
 (0)