Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "io" "net/http" "github.com/jfyne/live" ) // NewGreeter creates a new greeter component. func NewGreeter(ID string, h live.Handler, s live.Socket, name string) (*Component, error) { return NewComponent( ID, h, s, WithMount(func(ctx context.Context, c *Component) error { c.State = name return nil }), WithRender(func(w io.Writer, c *Component) error { // Render the greeter, here we are including the script just to make this toy example work. return HTML(` <div class="greeter">Hello {{.}}</div> <script src="/live.js"></script> `, c).Render(w) }), ) } func main() { h := live.NewHandler( WithComponentMount(func(ctx context.Context, h live.Handler, s live.Socket) (*Component, error) { return NewGreeter("hello-id", h, s, "World!") }), WithComponentRenderer(), ) http.Handle("/", live.NewHttpHandler(live.NewCookieStore("session-name", []byte("weak-secret")), h)) http.Handle("/live.js", live.Javascript{}) http.ListenAndServe(":8080", nil) }
Output:
Index ¶
- func WithComponentMount(construct ComponentConstructor) live.HandlerConfig
- func WithComponentRenderer() live.HandlerConfig
- type Component
- func (c *Component) Event(event string) string
- func (c *Component) HandleEvent(event string, handler EventHandler)
- func (c *Component) HandleParams(handler EventHandler)
- func (c *Component) HandleSelf(event string, handler SelfHandler)
- func (c *Component) Self(ctx context.Context, s live.Socket, event string, data interface{}) error
- func (c *Component) String() string
- type ComponentConfig
- type ComponentConstructor
- type EventHandler
- type MountHandler
- type RegisterHandler
- type RenderFunc
- type RenderHandler
- type SelfHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithComponentMount ¶
func WithComponentMount(construct ComponentConstructor) live.HandlerConfig
WithComponentMount set the live.Handler to mount the root component.
func WithComponentRenderer ¶
func WithComponentRenderer() live.HandlerConfig
WithComponentRenderer set the live.Handler to use a root component to render.
Types ¶
type Component ¶
type Component struct { // ID identifies the component on the page. This should be something stable, so that during the mount // it can be found again by the socket. ID string // Handler a reference to the host handler. Handler live.Handler // Socket a reference to the socket that this component // is scoped too. Socket live.Socket // Register the component. This should be used to setup event handling. Register RegisterHandler // Mount the component, this should be used to setup the components initial state. Mount MountHandler // Render the component, this should be used to describe how to render the component. Render RenderHandler // State the components state. State interface{} }
Component a self contained component on the page.
func NewComponent ¶
func NewComponent(ID string, h live.Handler, s live.Socket, configurations ...ComponentConfig) (*Component, error)
NewComponent creates a new component and returns it. It does not register it or mount it.
func (*Component) Event ¶
Event scopes an event string so that it applies to this instance of this component only.
func (*Component) HandleEvent ¶
func (c *Component) HandleEvent(event string, handler EventHandler)
HandleEvent handles a component event sent from a connected socket.
func (*Component) HandleParams ¶ added in v0.8.0
func (c *Component) HandleParams(handler EventHandler)
HandleParams handles parameter changes. Caution these handlers are not scoped to a specific component.
func (*Component) HandleSelf ¶
func (c *Component) HandleSelf(event string, handler SelfHandler)
HandleSelf handles scoped incoming events send by a components Self function.
type ComponentConfig ¶
ComponentConfig configures a component.
func WithMount ¶
func WithMount(fn MountHandler) ComponentConfig
WithMount set a mounnt handler on the component.
func WithRegister ¶
func WithRegister(fn RegisterHandler) ComponentConfig
WithRegister set a register handler on the component.
func WithRender ¶
func WithRender(fn RenderHandler) ComponentConfig
WithRender set a render handler on the component.
type ComponentConstructor ¶
type ComponentConstructor func(ctx context.Context, h live.Handler, s live.Socket) (*Component, error)
ComponentConstructor a func for creating a new component.
type EventHandler ¶
EventHandler for a component, only needs the params as the event is scoped to both the socket and then component itself. Returns any component state that needs updating.
type MountHandler ¶
MountHandler the components mount function called on first GET request and again when the socket connects.
type RegisterHandler ¶
RegisterHandler the first part of the component lifecycle, this is called during component creation and is used to register any events that the component handles.
type RenderFunc ¶
RenderFunc a helper function to ease the rendering of nodes.
func HTML ¶
func HTML(layout string, c *Component) RenderFunc
HTML render some html with added template functions to support components. This passes the component state to be rendered.
Template functions - "Event" takes an event string and scopes it for the component.
func Render ¶ added in v0.9.1
func Render(c *Component) RenderFunc
Render wrap a component and provide a RenderFunc.
type RenderHandler ¶
RenderHandler ths component.
type SelfHandler ¶ added in v0.14.0
SelfHandler for a component, only needs the data as the event is scoped to both the socket and then component itself. Returns any component state that needs updating.