component

package
v0.2.18 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package component is lys-ui's component framework, the counterpart of relm4's Component: a model with typed input and output messages, an Init that builds its view, an Update that runs on the loop goroutine for every input, and controllers that own a component's lifetime and forward its outputs.

type Counter struct{ n int }

func (c *Counter) Init(cx *component.Context[Msg, Out]) widget.Widget { ... }
func (c *Counter) Update(cx *component.Context[Msg, Out], msg Msg) { ... }

ctrl := component.Launch(application, &Counter{}).
	Forward(func(o Out) { ... })

A component launched from another component's Context is that component's child: it shuts down with its parent unless detached.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run[C Component[In, Out], In, Out any](cfg app.WindowConfig, setup func(*app.Application) (C, error)) error

Run is relm4's RelmApp::run for one window: it connects to the compositor, calls setup to build the root component (setup gets the Application, to load fonts, register accelerators or open more windows), opens it as the root of a window from cfg, and runs the loop until the last window closes. A normal close returns nil.

Example
package main

import (
	"log"
	"strconv"

	"github.com/stubbedev/lys-ui/app"
	"github.com/stubbedev/lys-ui/component"
	"github.com/stubbedev/lys-ui/ui"
	"github.com/stubbedev/lys-ui/widget"
)

type Msg int

const (
	Increment Msg = iota
	Decrement
)

type Counter struct {
	env ui.Env
	n   int
}

func (c *Counter) Init(cx *component.Context[Msg, int]) widget.Widget {
	return ui.Mount(cx, c.env, ui.Row(
		ui.Button(ui.Label("-"), 8, 6).OnClick(func() { cx.Input(Decrement) }),
		ui.Expand(ui.Label("").WatchText(func() string { return strconv.Itoa(c.n) })),
		ui.Button(ui.Label("+"), 8, 6).OnClick(func() { cx.Input(Increment) }),
	).Spacing(6))
}

func (c *Counter) Update(cx *component.Context[Msg, int], msg Msg) {
	switch msg {
	case Increment:
		c.n++
	case Decrement:
		c.n--
	}
	cx.Output(c.n)
}

func main() {
	err := component.Run(app.WindowConfig{Title: "counter", AppID: "dev.example.counter"},
		func(a *app.Application) (*Counter, error) {
			face, err := app.Font("sans-serif", 15)
			if err != nil {
				return nil, err
			}
			if err := a.AddAccel("Escape", widget.NewAction("quit", a.Quit)); err != nil {
				return nil, err
			}
			return &Counter{env: ui.Env{Face: face, Size: 15}}, nil
		})
	if err != nil {
		log.Fatal(err)
	}
}

func Sort

func Sort[K cmp.Ordered, C Component[In, Out], In, Out any](k *KeyedFactory[K, C, In, Out])

Sort reorders the items in place by ascending key.

Types

type Change

type Change interface {
	Changed() bool
}

Change is anything that reports a change since the last refresh, such as a *Tracked value.

type Component

type Component[In, Out any] interface {
	Init(cx *Context[In, Out]) widget.Widget
	Update(cx *Context[In, Out], msg In)
}

Component is a model with typed input and output messages. Init runs once at launch and returns the root widget. Update runs on the loop goroutine for every input, in send order and never reentrantly.

type Context

type Context[In, Out any] struct {
	// contains filtered or unexported fields
}

Context is a running component's handle to its runtime: sending itself input, emitting output, registering view refreshes and cleanups, and launching children.

func (*Context[In, Out]) Await

func (cx *Context[In, Out]) Await(work func(ctx context.Context) (apply func()))

Await is an asynchronous update step, relm4's async update: work runs on its own goroutine, and the function it returns runs on the loop to apply the result to the model. Until then the component is Busy and later input waits, the rest of the current batch included, so no update ever sees the model halfway. The view refreshes when the step starts and when it lands. work's context is cancelled at shutdown, and a step landing after that never applies.

func (*Context[In, Out]) Busy

func (cx *Context[In, Out]) Busy() bool

Busy reports whether an Await step is in flight; a view can watch it to show progress.

func (*Context[In, Out]) Context

func (cx *Context[In, Out]) Context() context.Context

Context returns a context cancelled when the component shuts down, for work that must not outlive it.

func (*Context[In, Out]) Index

func (cx *Context[In, Out]) Index() *Index

Index returns the component's position in its Factory, or nil for a component that is not a factory item.

func (*Context[In, Out]) Input

func (cx *Context[In, Out]) Input(msg In)

Input sends msg to the component's own Update. Safe from any goroutine; from inside Update it lands on the next loop pass.

func (*Context[In, Out]) Launch

func (cx *Context[In, Out]) Launch[CIn, COut any](c Component[CIn, COut]) *Controller[CIn, COut]

Launch starts c as a child of this component: it shuts down when this component does, unless its controller is detached.

func (*Context[In, Out]) LaunchWorker

func (cx *Context[In, Out]) LaunchWorker[WIn, WOut any](w Worker[WIn, WOut]) *WorkerController[WIn, WOut]

LaunchWorker starts w as a worker owned by this component: it stops when the component shuts down, unless detached.

func (*Context[In, Out]) Layer

func (cx *Context[In, Out]) Layer() *app.LayerWindow

Layer returns the layer surface the component lives in, like Window.

func (*Context[In, Out]) Loop

func (cx *Context[In, Out]) Loop() Loop

Loop returns the loop the component runs on.

func (*Context[In, Out]) NewFactory

func (cx *Context[In, Out]) NewFactory[C Component[CIn, COut], CIn, COut any](view FactoryView[C]) *Factory[C, CIn, COut]

NewFactory returns a factory owned by this component, rendering into view: its items shut down with the component.

func (*Context[In, Out]) NewKeyedFactory

func (cx *Context[In, Out]) NewKeyedFactory[K comparable, C Component[CIn, COut], CIn, COut any](view FactoryView[C]) *KeyedFactory[K, C, CIn, COut]

NewKeyedFactory returns a keyed factory owned by this component.

func (*Context[In, Out]) OnShutdown

func (cx *Context[In, Out]) OnShutdown(fn func())

OnShutdown registers fn to run when the component shuts down, in reverse registration order.

func (*Context[In, Out]) Oneshot

func (cx *Context[In, Out]) Oneshot(fn func(ctx context.Context) In)

Oneshot runs fn on its own goroutine and delivers its result to Update: relm4's oneshot_command. fn's context is cancelled when the component shuts down, and a result arriving after that is dropped.

func (*Context[In, Out]) Output

func (cx *Context[In, Out]) Output(msg Out)

Output emits msg to whatever the controller forwards to. Safe from any goroutine; delivery happens on the loop.

func (*Context[In, Out]) Sender

func (cx *Context[In, Out]) Sender() Sender[In]

Sender returns a Sender for the component's input, for goroutines and other components.

func (*Context[In, Out]) Shutdown

func (cx *Context[In, Out]) Shutdown()

Shutdown stops the component from inside, as its controller's Shutdown does: a dialog or window component closing itself.

func (*Context[In, Out]) Spawn

func (cx *Context[In, Out]) Spawn(fn func(ctx context.Context, emit func(In)))

Spawn runs fn on its own goroutine; every emit delivers a message to Update: relm4's spawn_command. fn's context is cancelled when the component shuts down, and emits after that are dropped.

func (*Context[In, Out]) Track

func (cx *Context[In, Out]) Track(fn func(), deps ...Change)

Track runs fn now, and after a batch of updates only when one of deps changed: relm4's #[track]. Without deps it never runs again.

func (*Context[In, Out]) Tracked

func (cx *Context[In, Out]) Tracked[T any](initial T) *Tracked[T]

Tracked returns a tracked value owned by this component, starting unchanged at initial.

func (*Context[In, Out]) Watch

func (cx *Context[In, Out]) Watch(fn func())

Watch runs fn now and again after every batch of updates, relm4's #[watch]. It is how a view keeps a widget in step with the model.

func (*Context[In, Out]) WatchWindow

func (cx *Context[In, Out]) WatchWindow(fn func(*app.Window))

WatchWindow runs fn with the component's window as soon as it exists and again after every batch of updates: #[watch] on the window's own properties.

func (*Context[In, Out]) Window

func (cx *Context[In, Out]) Window() *app.Window

Window returns the toplevel the component lives in: the one component.Window or Run opened for it or for an ancestor, nil otherwise and nil during Init (the window is created around the root Init returns).

func (*Context[In, Out]) WindowFullscreen

func (cx *Context[In, Out]) WindowFullscreen(get func() bool)

WindowFullscreen keeps the window fullscreen while get() is true, requesting a change only when the value moved.

func (*Context[In, Out]) WindowMaximized

func (cx *Context[In, Out]) WindowMaximized(get func() bool)

WindowMaximized keeps the window maximized while get() is true, requesting a change only when the value moved.

func (*Context[In, Out]) WindowMinSize

func (cx *Context[In, Out]) WindowMinSize(get func() (width, height uint32))

WindowMinSize keeps the window's minimum size equal to get(), requesting a change only when the value moved.

func (*Context[In, Out]) WindowTitle

func (cx *Context[In, Out]) WindowTitle(get func() string)

WindowTitle keeps the window's title equal to get(), requesting a change only when the value moved.

type Controller

type Controller[In, Out any] struct {
	// contains filtered or unexported fields
}

Controller owns a launched component: its root widget, its input, where its outputs go, and its lifetime.

func Dialog

func Dialog[In, Out any](a *app.Application, parent app.DialogParent, cfg app.DialogConfig, c Component[In, Out]) (*Controller[In, Out], *app.Dialog, error)

Dialog runs c as the content of a dialog over parent. The component shuts down when the dialog closes, and shutting the component down closes the dialog.

func Launch

func Launch[In, Out any](loop Loop, c Component[In, Out]) *Controller[In, Out]

Launch starts c on loop as a top-level component, shut down when the loop stops.

func Layer

func Layer[In, Out any](a *app.Application, cfg app.LayerConfig, c Component[In, Out]) (*Controller[In, Out], *app.LayerWindow, error)

Layer launches c as the root of a new layer surface, like Window.

func Popover

func Popover[In, Out any](a *app.Application, host app.Host, cfg app.PopoverConfig, c Component[In, Out]) (*Controller[In, Out], *app.Popover, error)

Popover runs c as the content of a popover over host. The component shuts down when the popover closes, and shutting the component down dismisses the popover.

func Window

func Window[In, Out any](a *app.Application, cfg app.WindowConfig, c Component[In, Out]) (*Controller[In, Out], *app.Window, error)

Window launches c as the root of a new toplevel window: the counterpart of relm4's RelmApp::run. The component shuts down when the window closes; a window that fails to open shuts it down at once.

Example
package main

import (
	"errors"
	"log"
	"strconv"

	"github.com/stubbedev/lys-ui/app"
	"github.com/stubbedev/lys-ui/component"
	"github.com/stubbedev/lys-ui/ui"
	"github.com/stubbedev/lys-ui/widget"
)

type Msg int

const (
	Increment Msg = iota
	Decrement
)

type Counter struct {
	env ui.Env
	n   int
}

func (c *Counter) Init(cx *component.Context[Msg, int]) widget.Widget {
	return ui.Mount(cx, c.env, ui.Row(
		ui.Button(ui.Label("-"), 8, 6).OnClick(func() { cx.Input(Decrement) }),
		ui.Expand(ui.Label("").WatchText(func() string { return strconv.Itoa(c.n) })),
		ui.Button(ui.Label("+"), 8, 6).OnClick(func() { cx.Input(Increment) }),
	).Spacing(6))
}

func (c *Counter) Update(cx *component.Context[Msg, int], msg Msg) {
	switch msg {
	case Increment:
		c.n++
	case Decrement:
		c.n--
	}
	cx.Output(c.n)
}

func main() {
	sess, err := app.Connect()
	if err != nil {
		log.Fatal(err)
	}
	defer sess.Close()
	face, err := app.Font("sans", 15)
	if err != nil {
		log.Fatal(err)
	}
	application := app.NewApplication(sess)
	if _, _, err := component.Window(application, app.WindowConfig{
		Title: "counter", AppID: "dev.example.counter",
	}, &Counter{env: ui.Env{Face: face, Size: 15}}); err != nil {
		log.Fatal(err)
	}
	if err := application.Run(); err != nil && !errors.Is(err, app.ErrClosed) {
		log.Fatal(err)
	}
}

func (*Controller[In, Out]) Alive

func (c *Controller[In, Out]) Alive() bool

Alive reports whether the component has not shut down.

func (*Controller[In, Out]) Detach

func (c *Controller[In, Out]) Detach() *Controller[In, Out]

Detach releases the component from its owner, so the owner's shutdown no longer reaches it. It then runs until Shutdown or until the loop stops.

func (*Controller[In, Out]) Forward

func (c *Controller[In, Out]) Forward(fn func(Out)) *Controller[In, Out]

Forward routes every output to fn on the loop goroutine, replacing any earlier route. Outputs with no route are dropped.

func (*Controller[In, Out]) ForwardTo

func (c *Controller[In, Out]) ForwardTo[T any](s Sender[T], f func(Out) T) *Controller[In, Out]

ForwardTo routes every output, mapped by f, to s: relm4's forward(sender, map).

func (*Controller[In, Out]) Send

func (c *Controller[In, Out]) Send(msg In)

Send delivers msg to the component's Update. Safe from any goroutine.

func (*Controller[In, Out]) Sender

func (c *Controller[In, Out]) Sender() Sender[In]

Sender returns a Sender for the component's input.

func (*Controller[In, Out]) Shutdown

func (c *Controller[In, Out]) Shutdown()

Shutdown stops the component on the loop goroutine: its children shut down first, then the component's own Shutdown and OnShutdown hooks run, and later input is dropped. Calling it again does nothing.

func (*Controller[In, Out]) State

func (c *Controller[In, Out]) State() *StateWatcher[In, Out]

State returns the component's state watcher, through which its owner observes the model as it changes: relm4's Controller::state.

func (*Controller[In, Out]) Widget

func (c *Controller[In, Out]) Widget() widget.Widget

Widget returns the component's root widget.

type DialogResponder

type DialogResponder[In, Out any] interface {
	OnResponse(cx *Context[In, Out], response string)
}

DialogResponder is implemented by components run as a dialog that react to its response (a button, Esc, Enter): OnResponse runs on the loop before the dialog closes and the component shuts down, so it can still emit outputs.

type Factory

type Factory[C Component[In, Out], In, Out any] struct {
	// contains filtered or unexported fields
}

Factory is a collection of item components rendered into a container, relm4's FactoryVecDeque. Every item is a full component (Init, Update, outputs, children, Loader for async items) with a stable *Index. Edits apply to the container in place as they are made: items that stay keep their widgets, moved items move, and nothing is rebuilt. Like every widget mutation, a Factory belongs to the loop goroutine.

func NewFactory

func NewFactory[C Component[In, Out], In, Out any](loop Loop, view FactoryView[C]) *Factory[C, In, Out]

NewFactory returns a top-level factory rendering into view, shut down when the loop stops.

func (*Factory[C, In, Out]) All

func (f *Factory[C, In, Out]) All() iter.Seq2[int, C]

All yields every item with its position, in order.

func (*Factory[C, In, Out]) Broadcast

func (f *Factory[C, In, Out]) Broadcast(msg In)

Broadcast delivers msg to every item.

func (*Factory[C, In, Out]) Clear

func (f *Factory[C, In, Out]) Clear()

Clear removes every item, last first.

func (*Factory[C, In, Out]) Detach

func (f *Factory[C, In, Out]) Detach() *Factory[C, In, Out]

Detach releases the factory from its owner; it then lives until Shutdown or until the loop stops.

func (*Factory[C, In, Out]) Forward

func (f *Factory[C, In, Out]) Forward(fn func(*Index, Out)) *Factory[C, In, Out]

Forward routes every item output, with the emitting item's index, to fn on the loop goroutine, replacing any earlier route.

func (*Factory[C, In, Out]) ForwardTo

func (f *Factory[C, In, Out]) ForwardTo[T any](s Sender[T], m func(*Index, Out) T) *Factory[C, In, Out]

ForwardTo routes every item output, mapped by m, to s.

func (*Factory[C, In, Out]) Get

func (f *Factory[C, In, Out]) Get(i int) C

Get returns the item at i.

func (*Factory[C, In, Out]) Insert

func (f *Factory[C, In, Out]) Insert(i int, c C) *Index

Insert launches c as the item at position i (0 to Len) and returns its index.

func (*Factory[C, In, Out]) Len

func (f *Factory[C, In, Out]) Len() int

Len returns the number of items.

func (*Factory[C, In, Out]) Move

func (f *Factory[C, In, Out]) Move(from, to int)

Move moves the item at from to position to, in place.

func (*Factory[C, In, Out]) PushBack

func (f *Factory[C, In, Out]) PushBack(c C) *Index

PushBack appends c and returns its index.

func (*Factory[C, In, Out]) PushFront

func (f *Factory[C, In, Out]) PushFront(c C) *Index

PushFront prepends c and returns its index.

func (*Factory[C, In, Out]) Remove

func (f *Factory[C, In, Out]) Remove(i int) C

Remove shuts down the item at i, removes its widget, and returns its model.

func (*Factory[C, In, Out]) Send

func (f *Factory[C, In, Out]) Send(i int, msg In)

Send delivers msg to the item at i.

func (*Factory[C, In, Out]) Shutdown

func (f *Factory[C, In, Out]) Shutdown()

Shutdown shuts every item down, last first, and leaves their widgets in place. Calling it again does nothing.

func (*Factory[C, In, Out]) Swap

func (f *Factory[C, In, Out]) Swap(i, j int)

Swap exchanges the items at i and j.

type FactoryView

type FactoryView[C any] interface {
	Insert(i int, item C, w widget.Widget)
	Remove(i int, item C, w widget.Widget)
	Move(from, to int, item C, w widget.Widget)
}

FactoryView places a Factory's item widgets in a container, relm4's FactoryView. Its container holds the factory's items and nothing else, so positions line up. Implement it to render into any container.

func BoxView

func BoxView[C any](b *widget.Box, expand bool) FactoryView[C]

BoxView renders items as the children of a Box (or a list of rows: a Column inside a Scroll), each expanding on the box's axis when expand is set.

func FixedView

func FixedView[C any](f *widget.Fixed, place func(i int) (x, y int)) FactoryView[C]

FixedView renders items into a Fixed, item i at the coordinates place(i) returns, relm4's FixedPosition; items re-place in place as positions shift.

func FlowBoxView

func FlowBoxView[C any](f *widget.FlowBox) FactoryView[C]

FlowBoxView renders items as the children of a FlowBox; selection follows moved items.

func GridView

func GridView[C any](g *widget.Grid, place func(i int) (col, row, colSpan, rowSpan int)) FactoryView[C]

GridView renders items into a Grid, item i at the cell place(i) returns; items re-place in place as positions shift.

func ListBoxView

func ListBoxView[C any](b *widget.ListBox) FactoryView[C]

ListBoxView renders items as the rows of a ListBox.

func NotebookView

func NotebookView[C any](n *widget.Notebook, title func(C) string) FactoryView[C]

NotebookView renders items as the tabs of a Notebook, each titled by title when inserted.

func StackView

func StackView[C any](s *widget.Stack, name func(C) string) FactoryView[C]

StackView renders items as the pages of a Stack, each under the name name returns for it when inserted. Names must be unique.

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index is a factory item's position, kept current as items are inserted, removed and moved: relm4's DynamicIndex.

func (*Index) Current

func (x *Index) Current() int

Current returns the item's position, or -1 once it was removed.

type KeyedFactory

type KeyedFactory[K comparable, C Component[In, Out], In, Out any] struct {
	// contains filtered or unexported fields
}

KeyedFactory is a Factory whose items are addressed by an application key instead of a position: relm4's FactoryHashMap. Items keep their insertion order (or the order SortFunc set), render through the same FactoryViews, and forward outputs with their key.

func NewKeyedFactory

func NewKeyedFactory[K comparable, C Component[In, Out], In, Out any](loop Loop, view FactoryView[C]) *KeyedFactory[K, C, In, Out]

NewKeyedFactory returns a top-level keyed factory rendering into view, shut down when the loop stops.

func (*KeyedFactory[K, C, In, Out]) All

func (k *KeyedFactory[K, C, In, Out]) All() iter.Seq2[K, C]

All yields every key and item in order.

func (*KeyedFactory[K, C, In, Out]) Broadcast

func (k *KeyedFactory[K, C, In, Out]) Broadcast(msg In)

Broadcast delivers msg to every item.

func (*KeyedFactory[K, C, In, Out]) Clear

func (k *KeyedFactory[K, C, In, Out]) Clear()

Clear removes every item.

func (*KeyedFactory[K, C, In, Out]) Detach

func (k *KeyedFactory[K, C, In, Out]) Detach() *KeyedFactory[K, C, In, Out]

Detach releases the factory from its owner.

func (*KeyedFactory[K, C, In, Out]) Forward

func (k *KeyedFactory[K, C, In, Out]) Forward(fn func(K, Out)) *KeyedFactory[K, C, In, Out]

Forward routes every item output, with its key, to fn on the loop.

func (*KeyedFactory[K, C, In, Out]) ForwardTo

func (k *KeyedFactory[K, C, In, Out]) ForwardTo[T any](s Sender[T], m func(K, Out) T) *KeyedFactory[K, C, In, Out]

ForwardTo routes every item output, mapped by m, to s.

func (*KeyedFactory[K, C, In, Out]) Get

func (k *KeyedFactory[K, C, In, Out]) Get(key K) C

Get returns the item under key.

func (*KeyedFactory[K, C, In, Out]) Has

func (k *KeyedFactory[K, C, In, Out]) Has(key K) bool

Has reports whether an item is stored under key.

func (*KeyedFactory[K, C, In, Out]) Index

func (k *KeyedFactory[K, C, In, Out]) Index(key K) *Index

Index returns the current position of the item under key.

func (*KeyedFactory[K, C, In, Out]) Insert

func (k *KeyedFactory[K, C, In, Out]) Insert(key K, c C)

Insert stores c under key at the end. An existing item under key is replaced in place: it shuts down and c takes its position.

func (*KeyedFactory[K, C, In, Out]) Len

func (k *KeyedFactory[K, C, In, Out]) Len() int

Len returns the number of items.

func (*KeyedFactory[K, C, In, Out]) Remove

func (k *KeyedFactory[K, C, In, Out]) Remove(key K) C

Remove shuts down the item under key, removes its widget, and returns its model.

func (*KeyedFactory[K, C, In, Out]) Send

func (k *KeyedFactory[K, C, In, Out]) Send(key K, msg In)

Send delivers msg to the item under key.

func (*KeyedFactory[K, C, In, Out]) Shutdown

func (k *KeyedFactory[K, C, In, Out]) Shutdown()

Shutdown shuts every item down.

func (*KeyedFactory[K, C, In, Out]) SortFunc

func (k *KeyedFactory[K, C, In, Out]) SortFunc(compare func(a, b K) int)

SortFunc reorders the items in place by their keys.

type Loader

type Loader[In, Out any] interface {
	Component[In, Out]
	Loading() widget.Widget
	Load(ctx context.Context)
}

Loader is a component whose initialization needs work off the loop: relm4's AsyncComponent. Loading returns the widget shown meanwhile. Load then runs on its own goroutine and may fill in the model. Init runs on the loop once Load returns, and its view replaces the loading one. Input sent before that is held and delivered after Init. Load's context is cancelled when the component shuts down, and Init never runs for a component that shut down while loading.

type Loop

type Loop interface {
	Invoke(fn func())
	OnStop(fn func()) (cancel func())
}

Loop is the event loop components run on. *app.Application is the production loop; componenttest.Loop drives components in tests.

type Sender

type Sender[M any] struct {
	// contains filtered or unexported fields
}

Sender sends messages of one type to a component, from any goroutine.

func (Sender[M]) Send

func (s Sender[M]) Send(msg M)

Send delivers msg.

type Shutdowner

type Shutdowner[In, Out any] interface {
	Shutdown(cx *Context[In, Out])
}

Shutdowner is implemented by components that release resources when they shut down. Outputs sent from Shutdown are still delivered.

type StateWatcher

type StateWatcher[In, Out any] struct {
	// contains filtered or unexported fields
}

StateWatcher observes a launched component's model, relm4's Controller::state: a controller's owner subscribes to the model as it changes after each batch of updates, and reads it between changes. Reach it through Controller.State.

func (*StateWatcher[In, Out]) Model

func (w *StateWatcher[In, Out]) Model[C Component[In, Out]]() C

Model returns the component's model as it is now, typed as C: the same value Launch or Context.Launch was given. Read it on the loop - from a Subscribe callback, or a parent's own Update - so it does not race a batch in flight.

func (*StateWatcher[In, Out]) Subscribe

func (w *StateWatcher[In, Out]) Subscribe[C Component[In, Out]](fn func(C)) (unsubscribe func())

Subscribe registers fn to run on the component's loop after every batch of its updates, receiving the component itself as the live model: C is the component's type, inferred from fn. There is no initial call and no copy of the model, so fn must only read it: it must not change the model, and must not send the component input - a Send from inside the notification re-enters the component it observes. lys-ui cannot run it reentrantly (input sent from inside a batch lands on the next loop pass, like a self-send from Update), but an observer has no reason to rely on that. Subscribe is safe from any goroutine, and after shutdown fn never runs. The returned unsubscribe is idempotent, safe from any goroutine and safe after shutdown.

type Tracked

type Tracked[T any] struct {
	// contains filtered or unexported fields
}

Tracked is a model value that knows whether it changed since the component's last view refresh, relm4's tracker. Create it with Context.Tracked; Set and Update mark it changed, and the component clears the mark after every refresh.

func (*Tracked[T]) Changed

func (t *Tracked[T]) Changed() bool

Changed reports whether the value was set since the last refresh.

func (*Tracked[T]) Get

func (t *Tracked[T]) Get() T

Get returns the value.

func (*Tracked[T]) Set

func (t *Tracked[T]) Set(v T)

Set replaces the value and marks it changed.

func (*Tracked[T]) Update

func (t *Tracked[T]) Update(fn func(*T))

Update applies fn to the value in place and marks it changed.

type ViewUpdater

type ViewUpdater[In, Out any] interface {
	UpdateView(cx *Context[In, Out])
}

ViewUpdater is implemented by components that refresh their view after each batch of updates, relm4's update_view.

type Worker

type Worker[In, Out any] interface {
	Update(msg In, emit func(Out))
}

Worker is a typed background actor, relm4's Worker. Update runs on the worker's own goroutine for every input, in send order; emit sends an output, delivered on the loop.

type WorkerController

type WorkerController[In, Out any] struct {
	// contains filtered or unexported fields
}

WorkerController owns a launched worker: its input, where its outputs go, and its lifetime.

func LaunchWorker

func LaunchWorker[In, Out any](loop Loop, w Worker[In, Out]) *WorkerController[In, Out]

LaunchWorker starts w on its own goroutine as a top-level worker, stopped when the loop stops.

func (*WorkerController[In, Out]) Detach

func (c *WorkerController[In, Out]) Detach() *WorkerController[In, Out]

Detach releases the worker from its owner; it then runs until Shutdown or until the loop stops.

func (*WorkerController[In, Out]) Forward

func (c *WorkerController[In, Out]) Forward(fn func(Out)) *WorkerController[In, Out]

Forward routes every output to fn on the loop goroutine, replacing any earlier route. Outputs with no route are dropped.

func (*WorkerController[In, Out]) ForwardTo

func (c *WorkerController[In, Out]) ForwardTo[T any](s Sender[T], f func(Out) T) *WorkerController[In, Out]

ForwardTo routes every output, mapped by f, to s.

func (*WorkerController[In, Out]) Send

func (c *WorkerController[In, Out]) Send(msg In)

Send delivers msg to the worker's Update. Safe from any goroutine.

func (*WorkerController[In, Out]) Sender

func (c *WorkerController[In, Out]) Sender() Sender[In]

Sender returns a Sender for the worker's input.

func (*WorkerController[In, Out]) Shutdown

func (c *WorkerController[In, Out]) Shutdown()

Shutdown stops the worker: queued input is dropped, the Update in progress finishes, and its WorkerShutdowner hook runs. Outputs not yet delivered when Shutdown runs on the loop are delivered then; later ones are dropped. Calling it again does nothing.

type WorkerShutdowner

type WorkerShutdowner interface {
	Shutdown()
}

WorkerShutdowner is implemented by workers that release resources when they stop; Shutdown runs on the worker's goroutine after its last Update.

Directories

Path Synopsis
Package componenttest runs components without a compositor: a Loop that queues invoked work until the test drives it.
Package componenttest runs components without a compositor: a Loop that queues invoked work until the test drives it.

Jump to

Keyboard shortcuts

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