flow

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package flow provides reactive state management for Bellui applications.

Value holds an observable value that notifies listeners when it changes. Derived computes a value from dependencies and auto-updates when they change.

This package has no Bellui dependencies — it is pure Go with generics. Widget binding helpers are in the widget package (widget.BindEntry, etc.).

Example:

name := flow.New("World")
count := flow.New(0)

greeting := flow.Derive(func() string {
    return fmt.Sprintf("Hello %s (%d)", name.Get(), count.Get())
}, name, count)

name.Listen(func(old, new string) {
    fmt.Printf("name changed: %q → %q\n", old, new)
})

name.Set("Bellui")   // triggers listener + recomputes greeting
count.Set(1)        // recomputes greeting
Example
name := New("World")
count := New(0)

greeting := Derive(func() string {
	return fmt.Sprintf("Hello %s (%d)", name.Get(), count.Get())
}, name, count)

fmt.Println(greeting.Get())

name.Set("Bellui")
fmt.Println(greeting.Get())

count.Set(42)
fmt.Println(greeting.Get())
Output:
Hello World (0)
Hello Bellui (0)
Hello Bellui (42)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Derived

type Derived[T comparable] struct {
	// contains filtered or unexported fields
}

Derived is a computed value that automatically recomputes when any of its source dependencies change. It implements Source so other Derived values or listeners can depend on it.

firstName := flow.New("John")
lastName := flow.New("Doe")

fullName := flow.Derive(func() string {
    return firstName.Get() + " " + lastName.Get()
}, firstName, lastName)

fmt.Println(fullName.Get()) // "John Doe"
firstName.Set("Jane")
fmt.Println(fullName.Get()) // "Jane Doe"

func Derive

func Derive[T comparable](compute func() T, deps ...Source) *Derived[T]

Derive creates a computed value from a function and its dependencies. The function is called immediately to compute the initial value, and again whenever any dependency changes.

func (*Derived[T]) Close

func (d *Derived[T]) Close()

Close unsubscribes from all source dependencies. After Close, the derived value will no longer update.

func (*Derived[T]) Get

func (d *Derived[T]) Get() T

Get returns the current computed value.

func (*Derived[T]) Listen

func (d *Derived[T]) Listen(fn func(old, new T)) func()

Listen registers a callback called when the computed value changes. Returns an unsubscribe function.

func (*Derived[T]) OnChange

func (d *Derived[T]) OnChange(fn func()) func()

OnChange implements Source.

type Source

type Source interface {
	// OnChange registers a callback called when the value changes.
	// Returns an unsubscribe function.
	OnChange(fn func()) func()
}

Source is the interface shared by Value and Derived — anything that can be listened to for changes.

type Value

type Value[T comparable] struct {
	// contains filtered or unexported fields
}

Value is an observable value of type T. When the value changes via Set(), all registered listeners are notified. Safe for concurrent use.

func New

func New[T comparable](initial T) *Value[T]

New creates a Value with the given initial value.

func (*Value[T]) Get

func (v *Value[T]) Get() T

Get returns the current value.

func (*Value[T]) Listen

func (v *Value[T]) Listen(fn func(old, new T)) func()

Listen registers a typed callback that is called whenever the value changes. The callback receives the old and new values. Returns an unsubscribe function.

func (*Value[T]) Modify

func (v *Value[T]) Modify(fn func(T) T)

Modify atomically reads, modifies, and sets the value using the given function. Useful for read-modify-write operations:

count.Modify(func(v int) int { return v + 1 })

func (*Value[T]) OnChange

func (v *Value[T]) OnChange(fn func()) func()

OnChange implements Source.

func (*Value[T]) Set

func (v *Value[T]) Set(new T)

Set updates the value. If the new value differs from the current one, all listeners are notified. Safe to call from any goroutine.

Jump to

Keyboard shortcuts

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