Lifecycle
hydrate runs when a client socket is newly upgraded into an actor.
Use it to return the connecting client's initial state and notify already-connected clients.
Hydrate initial state
import { Effect } from "effect"
import { TicTacToeActor } from "./TicTacToeActor.ts"
export const hydrate = Effect.gen(function* () {
const { clients } = yield* TicTacToeActor
if (clients.size === 1) {
return {
awaitingPartner: true,
name: "X" as const,
}
}
yield* TicTacToeActor.others.send("GameStarted", {})
return {
awaitingPartner: false,
name: "O" as const,
}
}).pipe(Effect.orDie)hydrate is the snapshot for a reconnectable client. Tic-tac-toe returns awaitingPartner: true to the first socket,
then sends GameStarted to the existing player and returns ready state to the second player.
onDisconnect runs when a socket closes or disconnects and is supplied to WorkerdActorRuntime.make(...).
Read Snapshot and Delta Events for the state pattern and Client State for the hydration and reduction loop.