Pages, layouts and state
Screens are a Layout and a Model, hosted by a Shell that switches them by route.
A
Screen
draws a
Model
through a
Layout
and can load its data before it draws. A
Shell
is the root of the app: it holds every screen under one shared layout and model, and draws the screen whose route matches the URL. A
Model
holds the typed state screens read and write.
Since
v1.0.0
, all of them live in the
app
package, next to
Compo
, and the engine routes to them. Up to
v0.1.0
they lived in the
page
and
data
packages, which are deleted.
ยง1 Screen
type
Screen
[
T
any
]
struct
{
app
.
Compo
Path
string
// unexported fields
}
func
NewScreen
[
T
any
]
(
path
string
)
*
Screen
[
T
]
A
Screen
is a component served at a route.
NewScreen
creates one for the given path. It has no layout and no model yet, so it is not usable until both are set.
Building a screen
A screen is built by chaining calls. Each call returns the same
*Screen[T]
:
package
main
import
"github.com/Toms223/WebGo/app"
type
Settings
struct
{
User
string
}
type
settingsLayout
struct
{
}
func
(
settingsLayout
)
Loading
(
)
app
.
UI
{
return
app
.
Text
(
"loading"
)
}
func
(
settingsLayout
)
Error
(
err
error
)
app
.
UI
{
return
app
.
Text
(
err
.
Error
(
)
)
}
func
(
settingsLayout
)
Draw
(
m
*
app
.
Model
[
Settings
]
)
app
.
HTMLDiv
{
return
app
.
Div
(
)
.
Text
(
m
.
State
(
)
.
User
)
}
func
(
l
settingsLayout
)
Body
(
...
app
.
UI
)
app
.
Layout
[
Settings
]
{
return
l
}
func
main
(
)
{
model
,
err
:=
app
.
NewModel
(
"settings"
,
&
Settings
{
}
)
if
err
!=
nil
{
panic
(
err
)
}
s
:=
app
.
NewScreen
[
Settings
]
(
"/settings"
)
.
WithLayout
(
settingsLayout
{
}
)
.
WithModel
(
model
)
if
s
.
Err
(
)
!=
nil
{
panic
(
s
.
Err
(
)
)
}
}
-
At(path string) *Screen[T]sets the screen's path. -
WithLayout(layout Layout[T]) *Screen[T]sets the layout. PassingnillatchesErrNoLayout. -
WithModel(m *Model[T]) *Screen[T]sets the model. PassingnillatchesErrNoModel. -
WithLoader(loader func(ctx Context, state *T) error) *Screen[T]sets the functionOnLoadruns. See OnLoad below.
Error latching
A
Screen
keeps a single builder error. The first call that produces one (
WithLayout(nil)
or
WithModel(nil)
) stores it. From then on every builder call (
At
,
WithLayout
,
WithModel
,
WithLoader
) returns at once without changing anything. Only the first failure in a chain is kept.
Err
func
(
s
*
Screen
[
T
]
)
Err
(
)
error
Err
returns the latched error, if there is one. Otherwise it returns
ErrNoLayout
when the layout is
nil
, then
ErrNoModel
when the model is
nil
, and
nil
when both are set.
Route and CompoID
func
(
s
*
Screen
[
T
]
)
Route
(
)
string
func
(
s
*
Screen
[
T
]
)
CompoID
(
)
string
Route
returns the path with exactly one leading slash:
""
becomes
/
,
"settings"
becomes
/settings
, and
"/settings"
stays
/settings
.
CompoID
returns the route. When a parent renders a screen of the same Go type on another route, the mounted one is dismounted and the new one mounted, instead of copying fields across. That is how a
Shell
switches between two screens of the same type cleanly. See
Lifecycle
.
OnMount
func
(
s
*
Screen
[
T
]
)
OnMount
(
ctx
app
.
Context
)
If
Err()
reports an error,
OnMount
stores it and returns, so
Render
shows it. Otherwise it reads the model's state from the store with
Get
and observes the model while the screen is mounted, so every
Set
on the model renders the screen again.
OnLoad
func
(
s
*
Screen
[
T
]
)
OnLoad
(
ctx
app
.
Context
)
error
A
Screen
is an
app.Loader
: the engine runs
OnLoad
in the browser, on its own goroutine, after
OnMount
and again after each
OnUpdate
, and holds the screen's render until it returns. The load phase is described under
Lifecycle
.
-
Without a loader, or with a builder error,
OnLoadreturnsnilat once. - Otherwise it makes a shallow copy of the model's state and calls the loader with a pointer to the copy.
-
When the loader returns
nil, the copy is written back on the UI goroutine throughLoad(ctx).Apply(...).Set(), so observers re-render and the model's action fires. The screen then draws with the new state. -
When the loader returns an error, the copy is dropped, the model is unchanged, and the screen draws
Error(err). The next load, after a remount or anOnUpdate, tries again. -
OnLoadnever runs on the server.
The copy is shallow. Slices and maps inside the state are shared with the model. A loader that assigns a new slice or map is safe; one that appends to or writes into the existing one races with the UI goroutine.
A screen always implements
Loader
, so even without a loader it shows
Loading()
for the moment its empty load takes to come back through the UI goroutine.
Loading, Error and Render
func
(
s
*
Screen
[
T
]
)
Loading
(
)
app
.
UI
func
(
s
*
Screen
[
T
]
)
Error
(
err
error
)
app
.
UI
func
(
s
*
Screen
[
T
]
)
Render
(
)
app
.
UI
-
While
OnLoadruns, the engine drawsLoading(), which is the layout'sLoading(). -
After a failed load, it draws
Error(err), which is the layout'sError(err). -
Otherwise it draws
Render().Renderreturns the layout'sError(err)whenErr()reports an error, andlayout.Draw(model)otherwise. -
All three panic with
ErrNoLayoutwhen the screen has no layout.
Only
*Screen[T]
(a pointer) is a component. A screen does not need a
Shell
: it can be rendered as a child of any component, as the
example
below does.