A generic websocket connection with Zod schema validation and on message execution.

fix: correct reconnect attempts, add exhausted event, merge param methods #1

Open eric.wien
targeting main
Open opened by eric.wien
targeting main

Fixes 4 issues surfaced in a design review of the codebase.

Fixes #

  • Reconnect off-by-one — scheduleReconnect checked >= maxAttempts after incrementing, so maxAttempts: N produced only N-1 real reconnects. Now checks before incrementing; maxAttempts: 3 yields 3 attempts per service, matching the documented contract.
  • New exhausted event — emitted when reconnection gives up after all attempts/cycles ({ services, attempts, cycles }); previously the give-up branch only logged, so consumers had no signal. Wired through WebSocketClient, documented in README + JSDoc.
  • Router throughput (demo) — test-jetstream.ts rewritten to a single z.discriminatedUnion + one handler (O(1) dispatch on kind) instead of registering 3 overlapping schemas that ran a full safeParse per schema per message. The router code is unchanged; the canonical example now is also the fastest pattern.
  • Param method merge — setParams folded into updateParams(params, { immediate }). setParams kept as a deprecated alias for updateParams(params, { immediate: false }).

Tests #

Added regression guards in tests/client.test.ts: off-by-one attempt count (3 reconnecting + 1 exhausted), deferred params, setParams alias. Full suite: 55 passing. Typecheck, lint, build all green.

Behavior changes (review carefully) #

  • maxAttempts now does one more attempt per service than before — anyone relying on the old N-1 behavior gets N.
  • exhausted is a new public event on both the connection and client.

This pull request has no versions submitted yet.

Sign up or Sign in to add to the discussion.