Skip to main content

Views

A view is what a screen reads: a block that computes an answer from stored data when asked, with a scope that says whose data it reads. A view writes nothing, calls no model and stores nothing, so a screen can watch one freely. Its answer is typed by the block's output schema. The server reports which changes the run depended on, so the screen asks again when one of them happens and never names what to listen for.

The examples use the roster view of the team flow, a list of the workers a user has hired. rosterBlock is a handler that lists them and declares an output schema. It lives in src/flows/team/blocks/roster-block.ts.

src/flows/team/views/roster.ts
import { defineView } from "@flow-state-dev/core";
import { rosterBlock } from "../blocks/roster-block";

export const roster = defineView({
scope: "user",
key: (row) => row.id,
block: rosterBlock,
});
src/flows/team/flow.ts
import { defineFlow } from "@flow-state-dev/core";
import { roster } from "./views/roster";

export default defineFlow({
kind: "team",
actions: {},
views: { roster },
})();

The map key under views is the view's name. For the concept behind views, see Views.

defineView fields​

defineView(config) returns a frozen ViewDefinition with kind: "view" and the fields you set. ViewOutput<TView> is its answer type and ViewInput<TView> its input type, both exported from @flow-state-dev/core.

FieldRequiredMeaning
scopeyes"session", "user" or "org". Where the view runs.
blockyesThe block that computes the answer. It must declare an outputSchema. A sequencer works when its last step is typed.
inputSchemanoReplaces block.inputSchema for the view's input, as on an action. It is not checked when the view is declared.
keynoA function of one row that returns a string, for a view whose answer is a list. See Row keys.
descriptionnoText for tooling. Defaults to block.description.

A flow installs views in the views map of defineFlow. An entry that defineView did not return fails when the flow is defined: Flow "<kind>" view "<name>" is not a view. Declare it with `defineView({ scope, block })`. A views value that is not a map fails with Flow "<kind>" declares "views" that is not a map of name to a view.

Which scopes may a view have?​

Three: "session", "user" and "org". The caller's user and org are the principal the flow's resolver returns, as for an action. Set a resolver, the flow's own authentication.resolvePrincipal or resolvePrincipal on createFlowState for every flow, and they come from the caller's verified credentials. With neither, the default resolver reads userId from the request body, so any caller can name any user. User and org views keep users apart only once one of them is set.

ScopeRunsNeeds a sessionCalled with
sessionOn one session, which must already exist. A view cannot create one.YessessionId
userAt the caller's user scope. Each user is answered from their own rows.NoNo session id
orgAt the caller's org scope.NoNo session id
  • A user or org view answers a user who has never had a session, and does not create one.
  • Another user's change does not refresh a user view. A second organization reads none of the first organization's user or org rows.
  • A session view on another user's session answers exactly as a session id that was never used: 404.
  • A block in a user or org view that reaches for ctx.session is refused with view_no_session.
  • A user view may read the caller's sessions. An org view that does is refused.
  • A view called at the wrong address is 400: view_needs_session for a session view called without a session id, view_takes_no_session for a user or org view called with one.

Row keys​

RuleDetail
key is optionalWithout it the answer is any value the block's schema allows.
With key, the answer is a listOtherwise the run fails with view_answer_not_keyed: View "<name>" declares a `key`, so its answer must be a list.
Keys are distinct stringsA repeated key, or a key that is not a string, fails with view_answer_not_keyed: View "<name>"'s rows must have distinct string keys; "<id>" repeats for a repeat, or ... distinct string keys; a key is not a string for a non-string key. Over the route that is 422, not retryable.
The answer carries its keysIn row order, as keys on the wire answer. An empty list has keys: [].
A screen keeps rows by keyA row that did not change is the same object across answers. Rows are matched by key and compared by their JSON.
Only a keyed view answers partiallyA view with no key is never handed a change. See ctx.view.

The distinctness of keys is checked on each answer, not when the view is declared.

What is refused when a view is declared?​

defineView throws a plain Error at the call, before any screen asks. It checks in this order, so a bad scope with a bad block names the scope.

RefusedThe message names
A config that is not an objectdefineView takes `{ scope, block }`.
A scope that is not "session", "user" or "org"defineView: scope "<value>" is not "session", "user" or "org".
A block that is not a block, or is missingdefineView: `block` is not a block. A view computes its answer with one block.
A block with no outputSchema, or one that is z.any() or z.unknown()defineView: block "<name>" declares no output schema. A view's answer is typed by its block's `outputSchema`; declare one.
A key that is not a functiondefineView: block "<name>"'s `key` is not a function of a row.

A write, a model call or a missing session is not refused here. Those are refused when the view runs.

What a view refuses when it runs​

A view's context refuses every write and every model call, so watching a view never changes what it reads and never pays for a model. A view's input is parsed against the block's schema, as an action's is, and invalid input is 400.

The blockHTTPerror
Writes a resource, writes session or user state, or dispatches an action409read_only
Runs a generator, resolves a model, or runs an evaluator422view_model_call
Reaches for ctx.session in a user or org view422view_no_session
Reads liveness (livenessOf), which no topic covers422view_unrecorded_read
Answers a keyed view with repeated or non-string keys, or a non-list422view_answer_not_keyed
Answers partially with no change, or from a view with no key422view_partial_refused

A view's block may be a sequencer. The reads its steps make count as the view's reads.

Calling a view over HTTP​

ViewRoute
User or orgPOST /api/flows/<flow>/views/<view>
SessionPOST /api/flows/<flow>/<sessionId>/views/<view>

A view the flow does not declare is 404, even if an action has that name. Success is 200 with:

FieldMeaning
outputThe block's output, null when absent.
topicsThe change topics what the run read falls under, deduplicated and sorted. A view that read nothing has topics: [].
hearsPer topic, which notices may change what the run read. See What a view hears. A topic names a part of the stored data, such as a session or a user's resources. A notice is the message the server sends a watching screen when something under a topic changes: { topic, kind, ids? }.
keysA keyed view's row keys, in order.
partial{ removed }, set when the answer covers only the rows a change touched.

Failures:

StatuserrorWhen
400view_needs_session, view_takes_no_sessionThe call is at the wrong address for the scope.
400validation_errorThe input fails the block's input schema.
404The unknown-flow messageThe flow is not registered, or the flow instance is pinned to an owner (its ownerPin, see Flows) and the caller is outside it. Such a caller gets the same answer as for an unregistered address, whatever the view or session.
404The unknown-view messageThe flow declares no view of that name.
404The unknown-session answerA session view on a session that does not exist or that the caller may not run in. Same body as an unused id.
409read_onlyA write or a dispatch from a view.
409wrong-instance-sessionThe session belongs to a different instance of this flow than the one addressed.
409migration-requiredThe stored session has no recorded owner and the flow has several instances. See Authentication.
422view_model_call, view_no_session, view_unrecorded_readSee What a view refuses when it runs.
422The error's own codeAny other non-retryable FlowError, such as view_answer_not_keyed or view_partial_refused.
503The error's own codeA FlowError marked retryable.
500view_failedAny other error. message is the error's message.

A request may carry changed: { topic, kind, ids } in its body. It is honoured when topic is a well-formed topic (scope session with a non-empty string id, or user or org, each with an optional string resource), kind is one of items, request, run, state, resource, board, suspension or resync, and ids holds 1 to 100 strings. Anything else is no change and the view runs whole. The hint decides which rows the view reads again, never who may read them.

When a view is read again​

Each answer carries the topics of what the view read. A watching screen listens to them, reads again when one changes, and listens to new topics when the answer's topics change.

EventResult
A write to anything the last run readThe view is read again, unless the answer's hears rules the notice out.
A write from another request, session, flow or clientReaches the view the same way. A browser's write through the resource routes reaches it too.
A connect, a reconnect, a timer tick or a resync noticeAlways read again, never filtered by hears.
The poll timerReads every 3 s by default, as cover where push can miss a change. pollIntervalMs: null turns it off.
A failed readThe watch is stale and tries again on its own at 1 s, doubling to 30 s. A read that lands clears stale.

These events cause no read, or a narrower one:

EventResult
A notice the answer's hears rules outNot read.
A notice with ids, on a keyed view whose answer the screen holdsThe view is asked for only the rows it touches, and a partial answer is placed by key.
A partial answer with a key the screen does not holdThe screen reads the view whole.
More notices while a read is in flightOne more read, not one each: one in flight and at most one waiting.
An answer equal to the lastReports no new value. Equality is by value.
Nothing changes, push is up and the poll is offNo view calls.

A new input, session, client, or an enabled that turned off and on again starts a new watch, from no value and not stale.

What a view hears​

hears is a list of { topic, kinds, paths?, partitions? }. A watcher reads again on a notice its topic covers whose kind is listed and, for a resource notice that names a path, whose path is at or under one of paths. A resource notice that names no path always reads. A change at an ancestor path may touch anything beneath it, so a write through a broader collection reaches a view that read a narrower one.

What the run readWhat it hears
A collection such as workers/*resource notices under the collection's root, workers, and nothing else on its scope.
A single resourceIts own key. The state kind is added when the run also read the scope's state.
A session's items, metadata or historyitems, request, run and state on the session topic.
A session's state alonestate on the session topic. A reply streaming in does not wake it.
A user task board, through its task collection, where tasks name sessionsboard notices on each session the board names and on the user's board. Writes to the board's own resource do not wake it.
An org task boardWrites to the board's own resource at the org, and board notices on the org's board.

A user board read in which no task names a session hears writes to the board's own resource, as a read of any resource does. A view that read nothing has hears: [] and nothing wakes it.

A view hears a change through every read path, at every scope. A session view over a resource declared with sharedToLineage (see Resources) is woken by another session's write to it.

Watching from a screen​

viewRef<TView>(flow, view) from @flow-state-dev/client names a view for the client. Both strings are required, or it throws viewRef: a flow address is required or viewRef: a view name is required. It returns a frozen { flow, view } that holds no block, so it is safe in a browser bundle. ViewValueOf<TRef> and ViewInputOf<TRef> give a handle's answer and input types.

src/app/sidebar.tsx
import { viewRef } from "@flow-state-dev/client";
import { useView } from "@flow-state-dev/react";
import type { roster } from "../flows/team/views/roster";

const rosterView = viewRef<typeof roster>("team", "roster");

export function Sidebar() {
const { value, stale, failure, refresh } = useView(rosterView);
if (value === undefined) return <p>Loading</p>;
return (
<ul data-stale={stale} title={failure?.message}>
{value.map((worker) => (
<li key={worker.id} onClick={refresh}>
{worker.name}
</li>
))}
</ul>
);
}

useView(ref, input?, options?) returns:

FieldMeaning
valueThe answer, typed by the view. undefined until the first answer lands.
staletrue while the last read failed.
failureWhile stale, { message, httpStatus? }: the server's own error or message words and its HTTP status when it answered one. Absent otherwise.
refresh()Reads the view again on demand, for every screen sharing the read. Does nothing while disabled.
OptionMeaning
sessionIdRequired for a session view.
changedAccepted by the type and ignored. A watched view's hint comes only from the notice that woke the read. To pass a hint, use client.view(ref, input, { changed }).
enabledfalse watches nothing and answers { value: undefined, stale: false }. Default true.
pollIntervalMsPoll period in ms. Default 3000, null for none.
clientA client the host holds. Pass a stable reference.
  • Screens in one client that watch the same view with the same input share one read, and the read stops with the last watcher. Input is compared as sent, with object keys sorted.
  • An omitted input is sent as {}. Any other value, null included, is sent as given.
  • A keyed view's unchanged rows keep their identity across answers.

client.view(ref, input?, options?) runs a view once as the client's user and resolves { value, topics, keys?, partial?, hears? }. options is { sessionId?, changed? }. It rejects with ClientHttpError: 404 for an unknown view or a session the user cannot run in, 400 for input the schema refuses or a view called at the wrong scope, 422 for a view that called a model or reached for a session it does not have.

ctx.view inside a view's block​

ctx.view is set on every block of a view's run, child blocks included, and is absent anywhere else.

MemberMeaning
changedThe change this run may answer for, { topic, kind, ids }. The ids name what changed, such as a task on a board notice or an entry's storage key on a resource notice, never row keys. undefined on a whole run: a first read, a connect, a notice with no ids, or a view with no key.
partial(entries, options?)Answers for only some rows. entries are placed by key over the rows the screen holds, and options.removed keys are dropped. Return what it returns.

A view that never calls partial answers whole. Answer whole whenever a change could move a row, because a partial answer never reorders rows. partial throws view_partial_refused when the run has no changed or the view declares no key. A key the screen does not hold makes the screen read whole.

In development and tests the client checks each partial answer against a whole run. A difference logs [flow-state-dev] "<flow>" view "<view>" answered partially for [...] and differs from a whole run, and the screen shows the whole run.

What a view stores​

Nothing. The answer is computed on each read and kept nowhere, and every store is unchanged after any number of calls. The data a view reads belongs to the flow's resources.