Handling dynamic data
Where a layout's changing values live, the four ways to reach them, and how to pick one.
A layout file describes what to draw. Everything that changes while Tauler runs (the time, the volume, whether a drawer is open) is a value the layout reads from somewhere else.
The model
A layout is a function. Tauler runs it again whenever a value it reads changes, draws what it returns, and keeps nothing in between. A variable you set during one run is gone in the next. Other pages call one run a tick.
So state lives in one of two places:
- In another program. Tauler runs the program for you and passes lines of JSON between
it and the layout, the way a web page and a worker pass
postMessagedata. The program can be a shell loop, a script in any language, or one that ships with Tauler. - In the layout. You declare an object that Tauler keeps for you between runs.
Where state lives
| State lives in | The layout | Name | You call |
|---|---|---|---|
| another program | reads it | Stream | useStringStream, useJSONStream |
| another program | sends to it | no noun: you dispatch an event | useDispatch |
| another program | reads it and sends to it | Service | useService |
| the layout | reads and writes it | Store | store |
Each name, once:
- A subprocess is a program Tauler runs for a layout.
- A stream is a subprocess you read. Its value is the latest line it printed. Tauler also provides streams of its own.
- An event is an object with a
type, such as{ type: "open", url }. To dispatch it is to send it to a subprocess, which receives it as one line of JSON. - A service is a subprocess you read and dispatch to. It holds state for you, outside
Tauler.
const [state, dispatch] = useService(bin)has the shape of React'sconst [state, dispatch] = useReducer(...), with the reducer in another program. - A store is state the layout owns. A shared store is declared at the top level of a file. A local store is declared inside a component, and each place the component is written gets its own.
Choose one
- The value exists outside Tauler (the time, the CPU load, a file) and you only show it. Read it as a stream.
- A click must cause something outside Tauler, and there is nothing to show afterwards. Dispatch an event.
- You show a value and also change it, and other programs can change it too (the volume, the focused workspace). Use a service.
- The value matters only to the layout (an open drawer, a selected tab). Keep it in a shared store, or in a local store when each copy of a component needs its own.
- The value must survive a restart of Tauler. A store does not, so write a program that saves it, and use it as a service.
The one rule
A layout keeps nothing between runs. The rest follows from it:
- The
usefunctions are not React's hooks. There are no per-component state slots, no effects and no dependency arrays. Each call says "this run reads that program" and returns its latest value. - A program is started when a run asks for it and stopped when a run no longer does, so the calls must not come and go. See the rule on the streams page.
- Every value is read at the top of the render function, so it is in scope everywhere below. Two parts of the tree share a value by reading the same variable.
- What must be remembered goes in a store or in a program.
Related
- Do something on a click: handlers are where events are dispatched and stores are written.
- The layout file:
ctx, the screen size and theme Tauler passes to every run. - Screenshots: streams do not run in a screenshot.