vdom

package
v0.10.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackendUpdate_InitialChunkSize = 50  // Size for initial chunks that contain both TransferElems and StateSync
	BackendUpdate_ChunkSize        = 100 // Size for subsequent chunks
)
View Source
const (
	WorkType_Render = "render"
	WorkType_Effect = "effect"
)
View Source
const BindTag = "#bind"
View Source
const ChildrenPropKey = "children"
View Source
const FragmentTag = "#fragment"
View Source
const Html_BindParamTagName = "bindparam"
View Source
const Html_BindPrefix = "#bind:"
View Source
const Html_BindTagName = "bind"
View Source
const Html_GlobalEventPrefix = "#globalevent"
View Source
const Html_ParamPrefix = "#param:"
View Source
const KeyPropKey = "key"
View Source
const ObjectType_Binding = "binding"
View Source
const ObjectType_Func = "func"
View Source
const ObjectType_Ref = "ref"
View Source
const TextTag = "#text"
View Source
const WaveNullTag = "wave:null"
View Source
const WaveTextTag = "wave:text"

Variables

This section is empty.

Functions

func Class added in v0.9.2

func Class(name string) classAttrWrapper

func ClassIf added in v0.9.2

func ClassIf(cond bool, name string) classAttrWrapper

func ClassIfElse added in v0.9.2

func ClassIfElse(cond bool, name string, elseName string) classAttrWrapper

func Classes added in v0.9.3

func Classes(classes ...any) string

func Filter added in v0.9.3

func Filter[T any](items []T, fn func(T) bool) []T

func FilterIdx added in v0.9.3

func FilterIdx[T any](items []T, fn func(T, int) bool) []T

func ForEach added in v0.9.2

func ForEach[T any](items []T, fn func(T) any) []any

func ForEachIdx added in v0.9.2

func ForEachIdx[T any](items []T, fn func(T, int) any) []any

func Fragment added in v0.9.2

func Fragment(parts ...any) any

func If added in v0.9.2

func If(cond bool, part any) any

func IfElse added in v0.9.2

func IfElse(cond bool, part any, elsePart any) any

func P

func P(propName string, propVal any) any

func PStyle added in v0.9.2

func PStyle(styleAttr string, propVal any) any

func Props added in v0.9.2

func Props(props any) map[string]any

func QueueRefOp added in v0.9.2

func QueueRefOp(ctx context.Context, ref *VDomRef, op VDomRefOperation)

func UseAtom added in v0.9.0

func UseAtom[T any](ctx context.Context, atomName string) (T, func(T))

func UseEffect

func UseEffect(ctx context.Context, fn func() func(), deps []any)

func UseId

func UseId(ctx context.Context) string

func UseRenderTs added in v0.9.2

func UseRenderTs(ctx context.Context) int64

func UseState

func UseState[T any](ctx context.Context, initialVal T) (T, func(T))

func UseStateWithFn added in v0.9.2

func UseStateWithFn[T any](ctx context.Context, initialVal T) (T, func(T), func(func(T) T))

Types

type Atom added in v0.9.0

type Atom struct {
	Val    any
	Dirty  bool
	UsedBy map[string]bool // component waveid -> true
}

type ChildKey

type ChildKey struct {
	Tag string
	Idx int
	Key string
}

type Component

type Component[P any] func(props P) *VDomElem

type ComponentImpl added in v0.9.2

type ComponentImpl struct {
	WaveId  string
	Tag     string
	Key     string
	Elem    *VDomElem
	Mounted bool

	// hooks
	Hooks []*Hook

	// #text component
	Text string

	// base component -- vdom, wave elem, or #fragment
	Children []*ComponentImpl

	// component -> component
	Comp *ComponentImpl
}

type DomRect added in v0.9.0

type DomRect struct {
	Top    float64 `json:"top"`
	Left   float64 `json:"left"`
	Right  float64 `json:"right"`
	Bottom float64 `json:"bottom"`
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

type EffectWorkElem

type EffectWorkElem struct {
	Id          string
	EffectIndex int
}

type Hook

type Hook struct {
	Init      bool          // is initialized
	Idx       int           // index in the hook array
	Fn        func() func() // for useEffect
	UnmountFn func()        // for useEffect
	Val       any           // for useState, useMemo, useRef
	Deps      []any
}

generic hook structure

type RootElem

type RootElem struct {
	OuterCtx        context.Context
	Root            *ComponentImpl
	RenderTs        int64
	CFuncs          map[string]any
	CompMap         map[string]*ComponentImpl // component waveid -> component
	EffectWorkQueue []*EffectWorkElem
	NeedsRenderMap  map[string]bool
	Atoms           map[string]*Atom
	RefOperations   []VDomRefOperation
}

func MakeRoot

func MakeRoot() *RootElem

func (*RootElem) AddEffectWork

func (r *RootElem) AddEffectWork(id string, effectIndex int)

func (*RootElem) AddRenderWork

func (r *RootElem) AddRenderWork(id string)

func (*RootElem) Event

func (r *RootElem) Event(id string, propName string, event VDomEvent)

func (*RootElem) GetAtom added in v0.9.0

func (r *RootElem) GetAtom(name string) *Atom

func (*RootElem) GetAtomVal added in v0.9.0

func (r *RootElem) GetAtomVal(name string) any

func (*RootElem) GetRefOperations added in v0.9.2

func (r *RootElem) GetRefOperations() []VDomRefOperation

func (*RootElem) GetStateSync added in v0.9.0

func (r *RootElem) GetStateSync(full bool) []VDomStateSync

func (*RootElem) MakeVDom

func (r *RootElem) MakeVDom() *VDomElem

func (*RootElem) QueueRefOp added in v0.9.2

func (r *RootElem) QueueRefOp(op VDomRefOperation)

func (*RootElem) RegisterComponent

func (r *RootElem) RegisterComponent(name string, cfunc any) error

func (*RootElem) Render

func (r *RootElem) Render(elem *VDomElem)

func (*RootElem) RunWork added in v0.9.0

func (r *RootElem) RunWork()

this will be called by the frontend to say the DOM has been mounted it will eventually send any updated "refs" to the backend as well

func (*RootElem) SetAtomVal added in v0.9.0

func (r *RootElem) SetAtomVal(name string, val any, markDirty bool)

func (*RootElem) SetOuterCtx

func (r *RootElem) SetOuterCtx(ctx context.Context)

func (*RootElem) UpdateRef added in v0.9.2

func (r *RootElem) UpdateRef(updateRef VDomRefUpdate)

type VDomAsyncInitiationRequest added in v0.9.0

type VDomAsyncInitiationRequest struct {
	Type    string `json:"type" tstype:"\"asyncinitiationrequest\""`
	Ts      int64  `json:"ts"`
	BlockId string `json:"blockid,omitempty"`
}

func MakeAsyncInitiationRequest added in v0.9.0

func MakeAsyncInitiationRequest(blockId string) VDomAsyncInitiationRequest

type VDomBackendOpts added in v0.9.0

type VDomBackendOpts struct {
	CloseOnCtrlC         bool `json:"closeonctrlc,omitempty"`
	GlobalKeyboardEvents bool `json:"globalkeyboardevents,omitempty"`
	GlobalStyles         bool `json:"globalstyles,omitempty"`
}

type VDomBackendUpdate added in v0.9.0

type VDomBackendUpdate struct {
	Type          string             `json:"type" tstype:"\"backendupdate\""`
	Ts            int64              `json:"ts"`
	BlockId       string             `json:"blockid"`
	Opts          *VDomBackendOpts   `json:"opts,omitempty"`
	HasWork       bool               `json:"haswork,omitempty"`
	RenderUpdates []VDomRenderUpdate `json:"renderupdates,omitempty"`
	TransferElems []VDomTransferElem `json:"transferelems,omitempty"`
	StateSync     []VDomStateSync    `json:"statesync,omitempty"`
	RefOperations []VDomRefOperation `json:"refoperations,omitempty"`
	Messages      []VDomMessage      `json:"messages,omitempty"`
}

func SplitBackendUpdate added in v0.9.2

func SplitBackendUpdate(update *VDomBackendUpdate) []*VDomBackendUpdate

SplitBackendUpdate splits a large VDomBackendUpdate into multiple smaller updates The first update contains all the core fields, while subsequent updates only contain array elements that need to be appended

func (*VDomBackendUpdate) CreateTransferElems added in v0.9.2

func (beUpdate *VDomBackendUpdate) CreateTransferElems()

type VDomBinding added in v0.9.0

type VDomBinding struct {
	Type string `json:"type" tstype:"\"binding\""`
	Bind string `json:"bind"`
}

used in props

type VDomContextVal

type VDomContextVal struct {
	Root    *RootElem
	Comp    *ComponentImpl
	HookIdx int
}

type VDomCreateContext added in v0.9.0

type VDomCreateContext struct {
	Type    string              `json:"type" tstype:"\"createcontext\""`
	Ts      int64               `json:"ts"`
	Meta    waveobj.MetaMapType `json:"meta,omitempty"`
	Target  *VDomTarget         `json:"target,omitempty"`
	Persist bool                `json:"persist,omitempty"`
}

type VDomElem added in v0.9.0

type VDomElem struct {
	WaveId   string         `json:"waveid,omitempty"` // required, except for #text nodes
	Tag      string         `json:"tag"`
	Props    map[string]any `json:"props,omitempty"`
	Children []VDomElem     `json:"children,omitempty"`
	Text     string         `json:"text,omitempty"`
}

vdom element

func Bind

func Bind(htmlStr string, params map[string]any) *VDomElem

func E

func E(tag string, parts ...any) *VDomElem

func H added in v0.9.3

func H(tag string, props map[string]any, children ...any) *VDomElem

func TextElem

func TextElem(text string) VDomElem

func (*VDomElem) Key added in v0.9.0

func (e *VDomElem) Key() string

func (*VDomElem) WithKey added in v0.9.3

func (e *VDomElem) WithKey(key string) *VDomElem

type VDomEvent added in v0.9.0

type VDomEvent struct {
	WaveId          string             `json:"waveid"`
	EventType       string             `json:"eventtype"` // usually the prop name (e.g. onClick, onKeyDown)
	GlobalEventType string             `json:"globaleventtype,omitempty"`
	TargetValue     string             `json:"targetvalue,omitempty"`
	TargetChecked   bool               `json:"targetchecked,omitempty"`
	TargetName      string             `json:"targetname,omitempty"`
	TargetId        string             `json:"targetid,omitempty"`
	KeyData         *WaveKeyboardEvent `json:"keydata,omitempty"`
	MouseData       *WavePointerData   `json:"mousedata,omitempty"`
}

type VDomFrontendUpdate added in v0.9.0

type VDomFrontendUpdate struct {
	Type          string            `json:"type" tstype:"\"frontendupdate\""`
	Ts            int64             `json:"ts"`
	BlockId       string            `json:"blockid"`
	CorrelationId string            `json:"correlationid,omitempty"`
	Dispose       bool              `json:"dispose,omitempty"` // the vdom context was closed
	Resync        bool              `json:"resync,omitempty"`  // resync (send all backend data).  useful when the FE reloads
	RenderContext VDomRenderContext `json:"rendercontext,omitempty"`
	Events        []VDomEvent       `json:"events,omitempty"`
	StateSync     []VDomStateSync   `json:"statesync,omitempty"`
	RefUpdates    []VDomRefUpdate   `json:"refupdates,omitempty"`
	Messages      []VDomMessage     `json:"messages,omitempty"`
}

type VDomFunc added in v0.9.0

type VDomFunc struct {
	Fn              any      `json:"-"` // server side function (called with reflection)
	Type            string   `json:"type" tstype:"\"func\""`
	StopPropagation bool     `json:"stoppropagation,omitempty"`
	PreventDefault  bool     `json:"preventdefault,omitempty"`
	GlobalEvent     string   `json:"globalevent,omitempty"`
	Keys            []string `json:"#keys,omitempty"` // special for keyDown events a list of keys to "capture"
}

used in props

func (*VDomFunc) CallFn added in v0.9.0

func (vdf *VDomFunc) CallFn(event VDomEvent)

type VDomKeyboardEvent added in v0.9.0

type VDomKeyboardEvent struct {
	Type     string `json:"type"`
	Key      string `json:"key"`
	Code     string `json:"code"`
	Shift    bool   `json:"shift,omitempty"`
	Control  bool   `json:"ctrl,omitempty"`
	Alt      bool   `json:"alt,omitempty"`
	Meta     bool   `json:"meta,omitempty"`
	Cmd      bool   `json:"cmd,omitempty"`
	Option   bool   `json:"option,omitempty"`
	Repeat   bool   `json:"repeat,omitempty"`
	Location int    `json:"location,omitempty"`
}

matches WaveKeyboardEvent

type VDomMessage added in v0.9.0

type VDomMessage struct {
	MessageType string `json:"messagetype"`
	Message     string `json:"message"`
	StackTrace  string `json:"stacktrace,omitempty"`
	Params      []any  `json:"params,omitempty"`
}

type VDomRef added in v0.9.0

type VDomRef struct {
	Type          string           `json:"type" tstype:"\"ref\""`
	RefId         string           `json:"refid"`
	TrackPosition bool             `json:"trackposition,omitempty"`
	Position      *VDomRefPosition `json:"position,omitempty"`
	HasCurrent    bool             `json:"hascurrent,omitempty"`
}

used in props

func UseVDomRef added in v0.9.0

func UseVDomRef(ctx context.Context) *VDomRef

type VDomRefOperation added in v0.9.0

type VDomRefOperation struct {
	RefId     string `json:"refid"`
	Op        string `json:"op"`
	Params    []any  `json:"params,omitempty"`
	OutputRef string `json:"outputref,omitempty"`
}

type VDomRefPosition added in v0.9.0

type VDomRefPosition struct {
	OffsetHeight       int     `json:"offsetheight"`
	OffsetWidth        int     `json:"offsetwidth"`
	ScrollHeight       int     `json:"scrollheight"`
	ScrollWidth        int     `json:"scrollwidth"`
	ScrollTop          int     `json:"scrolltop"`
	BoundingClientRect DomRect `json:"boundingclientrect"`
}

type VDomRefUpdate added in v0.9.0

type VDomRefUpdate struct {
	RefId      string           `json:"refid"`
	HasCurrent bool             `json:"hascurrent"`
	Position   *VDomRefPosition `json:"position,omitempty"`
}

type VDomRenderContext added in v0.9.0

type VDomRenderContext struct {
	BlockId    string `json:"blockid"`
	Focused    bool   `json:"focused"`
	Width      int    `json:"width"`
	Height     int    `json:"height"`
	RootRefId  string `json:"rootrefid"`
	Background bool   `json:"background,omitempty"`
}

type VDomRenderUpdate added in v0.9.0

type VDomRenderUpdate struct {
	UpdateType string    `json:"updatetype" tstype:"\"root\"|\"append\"|\"replace\"|\"remove\"|\"insert\""`
	WaveId     string    `json:"waveid,omitempty"`
	VDomWaveId string    `json:"vdomwaveid,omitempty"`
	VDom       *VDomElem `json:"vdom,omitempty"` // these get removed for transfer (encoded to transferelems)
	Index      *int      `json:"index,omitempty"`
}

type VDomSimpleRef added in v0.9.2

type VDomSimpleRef[T any] struct {
	Current T `json:"current"`
}

func UseRef

func UseRef[T any](ctx context.Context, val T) *VDomSimpleRef[T]

type VDomStateSync added in v0.9.0

type VDomStateSync struct {
	Atom  string `json:"atom"`
	Value any    `json:"value"`
}

type VDomTarget added in v0.9.0

type VDomTarget struct {
	NewBlock  bool               `json:"newblock,omitempty"`
	Magnified bool               `json:"magnified,omitempty"`
	Toolbar   *VDomTargetToolbar `json:"toolbar,omitempty"`
}

target -- to support new targets in the future, like toolbars, partial blocks, splits, etc. default is vdom context inside of a terminal block

type VDomTargetToolbar added in v0.9.2

type VDomTargetToolbar struct {
	Toolbar bool   `json:"toolbar"`
	Height  string `json:"height,omitempty"`
}

type VDomTransferElem added in v0.9.2

type VDomTransferElem struct {
	WaveId   string         `json:"waveid,omitempty"` // required, except for #text nodes
	Tag      string         `json:"tag"`
	Props    map[string]any `json:"props,omitempty"`
	Children []string       `json:"children,omitempty"`
	Text     string         `json:"text,omitempty"`
}

the over the wire format for a vdom element

func ConvertElemsToTransferElems added in v0.9.2

func ConvertElemsToTransferElems(elems []VDomElem) []VDomTransferElem

func DedupTransferElems added in v0.9.2

func DedupTransferElems(elems []VDomTransferElem) []VDomTransferElem

type WaveKeyboardEvent added in v0.9.2

type WaveKeyboardEvent struct {
	Type     string `json:"type" tstype:"\"keydown\"|\"keyup\"|\"keypress\"|\"unknown\""`
	Key      string `json:"key"`  // KeyboardEvent.key
	Code     string `json:"code"` // KeyboardEvent.code
	Repeat   bool   `json:"repeat,omitempty"`
	Location int    `json:"location,omitempty"` // KeyboardEvent.location

	// modifiers
	Shift   bool `json:"shift,omitempty"`
	Control bool `json:"control,omitempty"`
	Alt     bool `json:"alt,omitempty"`
	Meta    bool `json:"meta,omitempty"`
	Cmd     bool `json:"cmd,omitempty"`    // special (on mac it is meta, on windows/linux it is alt)
	Option  bool `json:"option,omitempty"` // special (on mac it is alt, on windows/linux it is meta)
}

type WavePointerData added in v0.9.2

type WavePointerData struct {
	Button  int `json:"button"`
	Buttons int `json:"buttons"`

	ClientX   int `json:"clientx,omitempty"`
	ClientY   int `json:"clienty,omitempty"`
	PageX     int `json:"pagex,omitempty"`
	PageY     int `json:"pagey,omitempty"`
	ScreenX   int `json:"screenx,omitempty"`
	ScreenY   int `json:"screeny,omitempty"`
	MovementX int `json:"movementx,omitempty"`
	MovementY int `json:"movementy,omitempty"`

	// Modifiers
	Shift   bool `json:"shift,omitempty"`
	Control bool `json:"control,omitempty"`
	Alt     bool `json:"alt,omitempty"`
	Meta    bool `json:"meta,omitempty"`
	Cmd     bool `json:"cmd,omitempty"`    // special (on mac it is meta, on windows/linux it is alt)
	Option  bool `json:"option,omitempty"` // special (on mac it is alt, on windows/linux it is meta)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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