Skip to content

useAgent ready resolves on the identity frame, before the initial state frame #2268

Description

@mattzcarey

Problem

The connect sequence sends cf_agent_identity and then a separate cf_agent_state frame. AgentClient and useAgent resolve ready (and flip identified) when the identity frame arrives, so a caller that awaits ready on a host with stored state can observe state === undefined for one message event before the state frame is handled. In React this is one extra render with the default state.

This has always been Agent's behaviour. Since #2257 the WebSockets capability owns the connect sequence for every host, so a plain host has it too. Flagged by Devin on #2257 (packages/agents/src/websockets/websockets.ts, sendIdentity/sendState).

Options

  1. Carry the initial state inside the identity frame, so one frame resolves ready with state already set. Cleanest; changes the identity frame shape for every client.
  2. Add a flag to the identity frame saying a state frame follows, and have the client hold ready until it lands. Needs care so a stateless host, or one whose state is undefined, never waits forever.

Either way this is a wire change that has to land in client.ts, react.tsx, and the capability together, with the React and workers suites updated to the new ordering guarantee.

Where

  • packages/agents/src/websockets/websockets.ts: #connect, sendIdentity, sendState
  • packages/agents/src/client.ts, packages/agents/src/react.tsx: identity handling and _resolveReady

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions