Documentation
¶
Overview ¶
Package livepage serves a go-app component as a live HTML page. The browser connects over a WebSocket (github.com/coder/websocket), forwards DOM events (click, change, submit) to the model, and receives re-rendered HTML, which is morphed into the document using Idiomorph. jQuery and Idiomorph are served by the package and injected into the <head> of every page.
Usage:
type Model struct {
livepage.ModelBase
mach *am.Machine
}
// implement Render and Mach; use go-app event handlers like OnClick
page, err := livepage.New(ctx, model, livepage.Opts{})
http.ListenAndServe(":8080", page.Handler())
State changes outside of DOM events should call Page.HRefresh.
Enable Opts.DiffHtml to push compact HTML diffs instead of full documents.
Index ¶
Constants ¶
const ( // 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
}
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
// 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
}
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.