Store

io.worxbend.tui.runtime.Store
See theStore companion object
final class Store[S]

One application state value with pure transitions, cached projections and an optional undo/redo history — sugar over a Signal and Computeds, not a second reactive system.

final case class Todo(items: Vector[String], filter: String)

private val store  = Store(Todo(Vector.empty, ""), history = 50)
private val shown  = store.select(s => s.items.filter(_.contains(s.filter)))

def view(using ReactiveScope, Theme): Element =
 column(list(shown.get, state), button("undo")(store.undo()))

store.dispatch(s => s.copy(items = s.items :+ "write docs"))

dispatch applies action to the current state and publishes the result; an action that produces a state the signal's SignalEquality calls unchanged notifies nobody and records no history entry. select is a long-lived Computed: create it once beside the store, not inside view, and dispose it with the store's owner — the contract is the one on Computed. With history > 0 the store keeps up to that many previous states; undo steps back through them and redo forward again until the next dispatch, which discards the redo branch.

Ownership and threads: the render-thread rules of Signal and Computed apply unchanged. dispatch, undo, redo, select and get are render-thread calls (or runner-less unit-test calls); peek may be read from any thread. The history is plain mutable state touched only on that same thread.

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def canRedo: Boolean

Whether redo has a state to go forward to.

Whether redo has a state to go forward to.

Attributes

def canUndo: Boolean

Whether undo has a state to go back to.

Whether undo has a state to go back to.

Attributes

def dispatch(action: S => S): Unit

Applies a pure transition and publishes the result. A transition that changes nothing is a no-op in every sense: no notification, no history entry, no redo branch discarded.

Applies a pure transition and publishes the result. A transition that changes nothing is a no-op in every sense: no notification, no history entry, no redo branch discarded.

Attributes

def get(using scope: ReactiveScope): S

Reads the state and subscribes the tracking scope, as Signal.get does.

Reads the state and subscribes the tracking scope, as Signal.get does.

Attributes

def peek: S

Reads the state without subscribing; safe from any thread.

Reads the state without subscribing; safe from any thread.

Attributes

def redo(): Boolean

Steps forward one state undone by undo; false when there is none.

Steps forward one state undone by undo; false when there is none.

Attributes

def select[A](f: S => A): Computed[A]

A cached projection of the state. Long-lived: create it once and dispose it where the store's owner goes away.

A cached projection of the state. Long-lived: create it once and dispose it where the store's owner goes away.

Attributes

def undo(): Boolean

Steps back one state; false when there is none (no history configured, or all of it already undone).

Steps back one state; false when there is none (no history configured, or all of it already undone).

Attributes

Concrete fields

val state: Reactive[S]

The state as a read-only reactive value, for a Computed or a view that wants to read it directly.

The state as a read-only reactive value, for a Computed or a view that wants to read it directly.

Attributes