package livepage
Import Path
github.com/pancsta/livepage (on go.dev)
Dependency Relation
imports 20 packages, and imported by 2 packages
Involved Source Files
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.
Package-Level Type Names (total 5)
Compo h.Compo
App returns the component's app context, if mounted.
JSValue retrieves the JavaScript value associated with the component's root.
If the root element isn't defined, it returns a nil JavaScript value.
Mounted checks if the component is currently mounted within the UI.
OnMount stores the component's context.
(*Compo) PageName() string
Render produces a visual representation of the component's content. This
default implementation ensures the app.Composer interface is satisfied
when app.Compo is embedded. However, developers are encouraged to redefine
this method to customize the component's appearance.
Update flags the component for re-render. No-op until mounted.
ValueTo captures the value of the DOM element (if it exists) that triggered
an event, and assigns it to the provided receiver. The receiver must be a
pointer pointing to either a string, integer, unsigned integer, or a float.
This method panics if the provided value isn't a pointer.
*Compo : github.com/pancsta/go-app/pkg/app.Composer
*Compo : github.com/pancsta/go-app/pkg/app.Mounter
*Compo : github.com/pancsta/go-app/pkg/app.UI
*Compo : github.com/pancsta/go-app/pkg/app.Wrapper
CompoLiveApi is a live component. It has to embed [Compo] (or h.Compo and
implement Update on its own).
JSValue returns the javascript value linked to the element.
Mach is the machine used to serialize DOM handlers and rendering.
Reports whether the element is mounted.
( CompoLiveApi) PageName() string
Render constructs and returns the visual representation of the component
as a node tree.
Update flags the component for re-render.
*github.com/pancsta/asyncmachine-go/tools/visualizer/traces.View
CompoLiveApi : github.com/pancsta/go-app/pkg/app.Composer
CompoLiveApi : github.com/pancsta/go-app/pkg/app.UI
CompoLiveApi : github.com/pancsta/go-app/pkg/app.Wrapper
DomEvent is a DOM event forwarded from the browser.
Button int
ClientX float64
ClientY float64
Code string
data-livepage attr
DeltaX float64
DeltaY float64
internal
Handler is the ID of a go-app event handler bound to the target.
attrs
event payload
Name string
meta
ScrollLeft float64
ScrollTop float64
Type is one of click, change, submit.
Value string
Opts configures a [Page].
Debounce delay of pushing HTML to the clients. Defaults to
[DefaultDebounce].
DiffHtml sends HTML diffs instead of full documents, using go-diff on
the server and a JS equivalent in the browser. Default false.
HtmlPage wraps the rendered component into a whole HTML document. Scripts
are injected into its <head>. Defaults to [DefaultHtmlPage].
Logger defaults to slog.Default().
OriginPatterns are additionally allowed WebSocket origins (host
patterns). Same origin is always allowed.
Prefix of all the paths served by [Page.NewHandler], eg "/live". Optional.
func New[M](ctx context.Context, model M, opts Opts) (*Page[M], error)
Type Parameters:
M: CompoLiveApi
Page binds a [CompoLiveApi] to many browsers.
Disposed atomic.Bool
Clients returns the number of connected browsers.
Compo returns the underlying component.
(*Page[M]) Dispose()
HRefresh re-renders the model and pushes the HTML. It is safe to call from
state handlers and Eval().
HTML returns the full HTML document with the current state, as sent to the
browsers. It returns false when rendering timed out.
NewHandler serves the page, scripts and the WebSocket endpoint.
Push schedules (debounced) pushing of the current HTML to all the clients.
Nothing is sent when the HTML didn't change.
Refresh is threadsafe version of HRefresh and should not be called from state handlers or eval.
func New[M](ctx context.Context, model M, opts Opts) (*Page[M], error)
func github.com/pancsta/asyncmachine-go/tools/visualizer/traces.NewPage(ctx context.Context, mach *am.Machine, prefix string) (*Page[*traces.View], error)
Package-Level Functions (total 3)
DefaultHtmlPage is a minimal HTML document.
Type Parameters:
M: CompoLiveApi
New mounts the model and creates a [Page], which lives as long as ctx.
Package-Level Constants (total 3)
DefaultDebounce is the default delay of pushing HTML to the clients.
PathAssets is the directory with served scripts, relative to
[Opts.Prefix].
PathWs is the WebSocket endpoint, relative to [Opts.Prefix].
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |