Documentation
¶
Overview ¶
Package mounting is the compiled source for the two things docs/quickstart.md §2 explains beside its router: where the live handler is mounted, and where the first paint's state comes from.
Both were undocumented when QA-1 built the quickstart from the docs alone (docs/qa/phase-4-docs-alone.md, findings F-1, F-2 and F-4), and both failures are silent — a page that renders correctly and is dead, or a page that serves the wrong number to every visitor. This package exists so the fix is compiled and its behaviour is asserted, rather than described in prose that nothing checks.
Index ¶
- func Page(s State) templ.Component
- func Routes(mountPath string, app, page http.Handler) http.Handler
- type State
- type Store
- func (st *Store) App() *live.App[State, live.AnonymousIdentity]
- func (st *Store) FirstPaint() http.Handler
- func (st *Store) Init(ctx context.Context, session live.Session[live.AnonymousIdentity]) (State, []live.Effect[live.AnonymousIdentity], error)
- func (st *Store) Load(ctx context.Context) (State, error)
- func (st *Store) Set(n int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Page ¶
Page stands in for the templ component docs/quickstart.md §3 writes. It is written as Go here so this sample needs no generated file; in an application it is `templ Page(s State)` and the signature is the one below.
func Routes ¶
Routes mounts one live application and one page handler on a net/http mux.
Two patterns for the live handler, and the pair is not redundant: the first is the WebSocket endpoint itself, at exactly that path, and the second is the subtree the client runtime is served from. Delete either and the failure is silent — an upgrade that ServeMux redirects instead of upgrading, or a runtime URL that the catch-all answers with the HTML page.
No http.StripPrefix anywhere, and none is needed: the handler routes by path SUFFIX, so it works at any prefix and never has to be told what its own prefix is. Stripping is worse than unnecessary on the exact pattern — it leaves the empty path, and the handler's own mux answers that with a redirect the WebSocket client cannot follow.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is whatever the initial state actually comes from — a database, a cache, the row a session is a view of. It is here so that Load has something to read that can change between two requests, which is the whole of what this sample demonstrates.
func (*Store) App ¶
App is the live application this store's state belongs to.
Init is the only field this sample is about. The other seven are what live.New requires of any application — a reducer, a region, the event names it accepts, and the four security hooks, opted out of here because a sample with one number in it has no identities — and they are written out so this compiles rather than to be copied.
func (*Store) FirstPaint ¶
FirstPaint is the handler that serves the page, and it is the library's own: (*live.App).PageHandler calls Config.Init — st.Init, above — on every request and renders Page from the state it returns. So the bytes a visitor is served and the first snapshot their session receives are not merely written from one function, they ARE one function.
This method used to be that handler, written by hand: load, check the error, hand the state to templ.Handler. The library provides it now, and a sample that kept the hand-rolled version would be teaching a reader to write code they have been given — so what is left here is the wiring, which is the part that is theirs.
The alternative it exists to make unwritable is templ.Handler(Page(State{})) registered once: it builds the component at start-up and serves those bytes forever, which is correct only while Init also returns the zero value and fails silently the moment Init reads anything. PageHandler cannot be given a state value, only the function that renders one. The last spec in mounting_test.go pins that failure so it stays recognisable.
func (*Store) Init ¶
func (st *Store) Init(ctx context.Context, session live.Session[live.AnonymousIdentity]) (State, []live.Effect[live.AnonymousIdentity], error)
Init is Config.Init. It returns exactly what Load returns.