Skip to content

refactor(ws): extract entryStillCurrent + tryWsClose helpers#143

Merged
almogdepaz merged 1 commit into
mainfrom
refactor-websocket-helpers
May 14, 2026
Merged

refactor(ws): extract entryStillCurrent + tryWsClose helpers#143
almogdepaz merged 1 commit into
mainfrom
refactor-websocket-helpers

Conversation

@almogdepaz
Copy link
Copy Markdown
Owner

Dedup two patterns repeated throughout src/server/websocket.ts:

  • 7 inline if (!entry.alive || activePtySessions.get(session) !== entry || entry.viewer !== ws) return; checks → entryStillCurrent(entry, session, ws)
  • 12 inline try { x.close(code, reason); } catch (e) { log.debug(..., { session, error: errMsg(e) }) } blocks → tryWsClose(target, code, reason, logMsg, session)

Log message strings preserved verbatim. No behavior change.

verification

  • bun test tests/unit/ — 1150 pass
  • bun test tests/integration/pty-takeover.test.ts tests/integration/concurrent-pty-viewer.test.ts tests/integration/take-control.test.ts — 48 pass (these are the integration tests that exercise every close path)

notes

  • net LOC delta is +23 (849 → 872), not the -40 to -60 originally estimated. the inline sites were already single-line, so the win is DRY + max-line-width drop (~190 → ~120 chars), not line count.
  • tryWsClose covers only WebSocket close(code, reason) sites; terminal.close() and proc.kill() stay inline since their signatures and error contexts differ.

Phase 1 of plans/PLAN-lean-and-mean.md (v2). Remaining phases (push tracker dedup, ralph log helper) were evaluated and skipped — shallow dedupe + sticky test API on the first, cosmetic rename on the second.

dedup 7 inline alive/current/viewer checks and 12 inline try/close/catch
blocks in websocket.ts into two module-local helpers. behavior unchanged;
log message strings preserved verbatim.

phase 1 of plans/PLAN-lean-and-mean.md (v2). LOC delta is +23, not the
predicted -40 to -60 \u2014 the original sites were already single-line, so
the win is DRY + max-line-width drop (\~190 \u2192 \~120), not line count.
@almogdepaz almogdepaz merged commit e55ba46 into main May 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant