page

package
v0.10.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"io"
	"log"
	"net/http"

	"github.com/jmcarbo/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) 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 Init

func Init(ctx context.Context, construct func() (*Component, error)) (*Component, error)

Init takes a constructor and then registers and mounts the component.

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

func (c *Component) Event(event string) string

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

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 EventHandler)

HandleSelf handles scoped incoming events send by a components Self function.

func (*Component) Self

func (c *Component) Self(ctx context.Context, s *live.Socket, event string, data interface{}) error

Self sends an event scoped not only to this socket, but to this specific component instance. Or any components sharing the same ID.

func (*Component) String

func (c *Component) String() string

String renders the component to a string.

type ComponentConfig

type ComponentConfig func(c *Component) error

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, r *http.Request, s *live.Socket) (*Component, error)

ComponentConstructor a func for creating a new component.

type EventHandler

type EventHandler func(ctx context.Context, p live.Params) (interface{}, error)

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

type MountHandler func(ctx context.Context, c *Component, r *http.Request) error

MountHandler the components mount function called on first GET request and again when the socket connects.

type RegisterHandler

type RegisterHandler func(c *Component) error

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

type RenderFunc func(io.Writer) error

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

func Render(c *Component) RenderFunc

Render wrap a component and provide a RenderFunc.

func (RenderFunc) Render

func (r RenderFunc) Render(w io.Writer) error

Render take a writer and render the func.

type RenderHandler

type RenderHandler func(w io.Writer, c *Component) error

RenderHandler ths component.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL