Storage
The two tiers an environment keeps state in: the session it holds while it is open, and the interaction store that outlives the visit.
@internote/canvas-sdkOn this page
An environment declares the state it keeps in its definition's storage field (CanvasStorageDeclaration), in one of two tiers:
- Session state lasts while the document is open. It survives the reader scrolling away and back, and is never saved. An environment written with
continue:in a later scene takes it over. - Database state, with
tiers: 'session+db', is saved per reader, for environments that record a reader's answers.
storage: {
tiers: 'session',
initState: () => ({ waves: 0 }),
},Inside the environment, ctx.storage is the CanvasStorage the host assembled: session for the first tier and db for the second. In a surface, read the session with useEnvironmentSession from @internote/canvas-sdk/react.
One slot in the session tier belongs to a whole scene rather than an environment: its parameters, and whether its footer is open. A control may be written inside any environment or in the scene's own detail, and the parameter it declares is read by everything in the scene, so the state cannot sit in one environment's slot without that environment owning the others' sliders. SessionStore's forScene is that view, keyed by SceneIdentity; an environment reads it through ctx.parameters rather than reaching for the store. It is not carried by continue:, which hands one environment's state to another.
Keep in session state anything the reader changed that a picture of the environment should show — a switch thrown, a view panned — as plain data: numbers, strings, booleans, arrays and plain objects. An exported figure is a second host started from SessionStore's snapshot() of the first, and a snapshot takes only plain data: a slot holding a Map, a Set or a class instance is left out, and that environment starts the figure fresh. State held in a React ref or useState is not in the session at all, and never reaches the figure.
For a reading rather than state, such as where the cursor is or a scale bar, use a transient channel: createTransientValue for one reading per environment, or createTransientCollection for one per mark.