Documentation
¶
Overview ¶
Package livepage is a streaming HTML UI layer for go-app.dev components synchronized by asyncmachine-go.
Index ¶
Constants ¶
const ( // DataId is meant to be used for [h.Composer.DataSet] and returned in [DomEvent.Data]. DataId = "livepage" // DefaultDebounce is the default delay of pushing HTML to the clients. DefaultDebounce = 200 * time.Millisecond // PathWs is the WebSocket endpoint, relative to [Opts.Prefix]. PathWs = "/ws" // PathAssets is the directory with served scripts, relative to // [Opts.Prefix]. PathAssets = "/assets/" )
Variables ¶
This section is empty.
Functions ¶
func DefaultHtmlPage ¶
DefaultHtmlPage is a minimal HTML document.
Types ¶
type Compo ¶
type CompoLiveApi ¶
type CompoLiveApi interface {
h.Composer
// Mach is the machine used to serialize DOM handlers and rendering.
Mach() *am.Machine
// Update flags the component for re-render.
Update()
PageName() string
App() *h.Context
}
CompoLiveApi is a live component. It has to embed Compo (or h.Compo and implement Update on its own).
type DomEvent ¶
type DomEvent struct {
// Type is one of click, change, submit.
Type string
// attrs
Id string
Name string
Value string
// data-livepage attr
Data string
// Handler is the ID of a go-app event handler bound to the target.
Handler string
// event payload
Key string `json:",omitempty"`
Code string `json:",omitempty"`
Button int `json:",omitempty"`
ClientX float64 `json:",omitempty"`
ClientY float64 `json:",omitempty"`
DeltaX float64 `json:",omitempty"`
DeltaY float64 `json:",omitempty"`
ScrollTop float64 `json:",omitempty"`
ScrollLeft float64 `json:",omitempty"`
// meta
Path string
// Client is the id of the browser connection, set by the server. See
// [Opts.OnClient].
Client string `json:"-"`
// internal
Event *am.Event `json:"-"`
}
DomEvent is a DOM event forwarded from the browser.
type Opts ¶
type Opts struct {
// HtmlPage wraps the rendered component into a whole HTML document. Scripts
// are injected into its <head>. Defaults to [DefaultHtmlPage].
HtmlPage func(body string) string
// Debounce delay of pushing HTML to the clients. Defaults to
// [DefaultDebounce].
Debounce time.Duration
// DiffHtml sends HTML diffs instead of full documents, using go-diff on
// the server and a JS equivalent in the browser. Default false.
DiffHtml bool
// Prefix of all the paths served by [Page.NewHandler], eg "/live". Optional.
Prefix string
// OriginPatterns are additionally allowed WebSocket origins (host
// patterns). Same origin is always allowed.
OriginPatterns []string
// Logger defaults to slog.Default().
Logger *slog.Logger
// OnEvent is a delegated global event handler.
OnEvent func(*DomEvent)
// OnClient is called when a browser connection is established (connected
// true) or closed (connected false). The id is unique per page and matches
// [DomEvent.Client].
OnClient func(id string, connected bool)
// WsMux multiplexes binary WebSocket frames into numbered streams for each
// browser connection. Text frames stay with livepage. It's meant for
// protocols like aRPC. Binary frames are discarded when nil.
WsMux func(ctx context.Context, mux *WsMux)
}
Opts configures a Page.
type Page ¶
type Page[M CompoLiveApi] struct { Disposed atomic.Bool // contains filtered or unexported fields }
Page binds a CompoLiveApi to many browsers.
func (*Page[M]) HRefresh ¶
func (p *Page[M]) HRefresh()
HRefresh re-renders the model and pushes the HTML. It is safe to call from state handlers and Eval().
func (*Page[M]) HTML ¶
HTML returns the full HTML document with the current state, as sent to the browsers. It returns false when rendering timed out.
func (*Page[M]) NewHandler ¶
NewHandler serves the page, scripts and the WebSocket endpoint.
type WsMux ¶ added in v0.1.2
type WsMux struct {
// contains filtered or unexported fields
}
WsMux multiplexes the binary frames of a page WebSocket into numbered streams. Text frames keep being handled by Page. Streams are created on demand with WsMux.Open, also when a frame for an unknown stream arrives.