Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "io" "log" "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, r *http.Request, connected bool) 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, err := live.NewHandler( live.NewCookieStore("session-name", []byte("weak-secret")), WithComponentMount(func(ctx context.Context, h *live.Handler, r *http.Request, s *live.Socket) (Component, error) { return NewGreeter("hello-id", h, s, "World!") }), WithComponentRenderer(), ) if err != nil { log.Fatal(err) } http.Handle("/", h) http.Handle("/live.js", live.Javascript{}) http.ListenAndServe(":8080", nil) }
Output:
Index ¶
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) HandleSelf ¶
func (c *Component) HandleSelf(event string, handler EventHandler)
HandleSelf handles scoped incoming events send by a components Self function.
type ComponentConfig ¶
CompoentConfig configure 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, r *http.Request, 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 iteslef. 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 RenderComponent ¶
func RenderComponent(c Component) RenderFunc
RenderComponent wrap a component and provide a RenderFunc.