Save and restore a scene
Snapshot a scene to JSON, with its objects and readings, and rebuild it elsewhere.
snapshotScene turns a scene into a plain, versioned JSON value: its origin, its objects, the
history of their readings and the staleness window. restoreScene builds a new scene from it.
import { createScene, restoreScene, snapshotScene, type SceneSnapshot } from "@klorad/api/world";
const scene = createScene({ name: "Waterfront", coordinateSystem: { origin: { lat: 40.62637, lon: 22.94838 } } });
scene.add({ id: "hall", name: "Exhibition hall", position: { east: 0, north: 0 }, geometry: { kind: "box", width: 24, depth: 14, height: 7 } });
// Plain JSON: objects, their observations, the origin and the staleness window.
const json = JSON.stringify(snapshotScene(scene));
// Later, or on another machine: the same scene, with a fresh event stream.
const restored = restoreScene(JSON.parse(json) as SceneSnapshot);
restored.get("hall")?.name; // "Exhibition hall"
export { restored };What is and is not in a snapshot
| In | Not in |
|---|---|
Id, name, origin, staleAfterMs | Subscribers and the event stream (a restored scene starts a new one) |
| Every object, with its correspondence and binding | Connections to sources (connect them again) |
| Every observation kept in history | Interaction definitions and Access policies (they are code) |
The snapshot carries format: "klorad.scene" and a version, so a future format can be read
alongside this one. Pass a clock or other options as the second argument of restoreScene
when the restored scene needs them.
When to use it
Fixtures for tests and demos, a "start from here" state for a new environment, and exporting a twin to another tool. It is not a live replication mechanism: for that, persist operator decisions in your database and keep live state at its source.
Something wrong or unclear? Tell your contact at Prieston Technologies.