Expand description
Durable Object adapter for shards on Cloudflare Workers.
The idea: one DO class per shard type. Each match/zone/document gets its
own DO instance (addressed by name). The DO owns the shard’s state, the
tick loop (via setAlarm), and all WebSocket connections.
Why DOs specifically: they give you exactly the isolation + persistence semantics that Workers lack otherwise — single-threaded execution per instance, on-instance storage, WebSocket hibernation (clients stay connected even while the DO is idle and not billed).
§Status
This is a scaffold, not a working DO. The real worker crate has
a different macro story and tight coupling to wasm-bindgen. A full impl
requires:
- A concrete
#[durable_object]-annotated struct (in the user’s Workers bundle, not here). - A bridge from incoming WS messages → shard input queue.
- A bridge from shard snapshots → DO WebSocket send.
state.setAlarm()for scheduled ticks in event-driven mode.- Storage.get()/put() for persistence across hibernation.
The abstractions below give users a head-start by providing the right trait shapes for their DO implementation to hook into.
Structs§
Constants§
- DURABLE_
OBJECT_ TEMPLATE_ JS - The boilerplate a user adds to their Workers bundle’s JS entry file.
Traits§
- DoStorage
- Abstraction over DO storage.put()/get() so the scaffold can express the
persistence pattern without depending on the
workercrate.
Functions§
- do_
websocket_ sink - Build a
SnapshotSinkthat forwards snapshots to a DO’s WebSocket send. - persist_
to_ do_ storage - Save a shard’s serialized state to DO storage.
- register_
do_ subscriber - Registers a WebSocket-connected player with a shard running inside a DO.
- restore_
from_ do_ storage - Restore a shard’s serialized state from DO storage.