BloomReducer<S, A> class

A signal-backed state container managing complex state transitions via a pure reducer function.

BloomReducer provides Redux/React useReducer-style state management integrated directly into the Bloom JS Native signals reactivity system. When an action is dispatched via dispatch, reducerFn calculates the next state and updates the underlying Signal. Any reactive consumer—such as a Live widget descriptor, computed, or effect—will automatically re-render or re-evaluate in response.

Backend Behavior

  • Browser (mount): Fully reactive. State updates propagate to all subscribed DOM nodes and effect scopes. Dispatched actions are recorded in history.
  • SSR (renderToHtml): Safe to initialize and read on the server. During SSR, state holds BloomReducer's initial state (or any state reached before rendering), which is read synchronously during HTML tree generation.

Example

// 1. Define State & Actions
class TodoItem {
  final String id;
  final String title;
  final bool done;
  const TodoItem({required this.id, required this.title, this.done = false});
}

sealed class TodoAction {}
class AddTodo extends TodoAction { final String title; AddTodo(this.title); }
class ToggleTodo extends TodoAction { final String id; ToggleTodo(this.id); }

// 2. Define Pure Reducer
List<TodoItem> todoReducer(List<TodoItem> state, TodoAction action) => switch (action) {
  AddTodo(:final title) => [
      ...state,
      TodoItem(id: '${state.length + 1}', title: title),
    ],
  ToggleTodo(:final id) => [
      for (final item in state)
        if (item.id == id)
          TodoItem(id: item.id, title: item.title, done: !item.done)
        else
          item,
    ],
};

// 3. Instantiate & Use in Component Tree
final todos = useReducer(todoReducer, const <TodoItem>[]);

BloomNode buildTodoList() {
  return Div(
    children: [
      Button(
        text: 'Add Task',
        on: {'click': (e) => todos.dispatch(AddTodo('New Task'))},
      ),
      Live(() => Ul(
        children: [
          for (final item in todos.state.value)
            Li(
              text: item.title,
              style: item.done ? 'text-decoration: line-through' : null,
              on: {'click': (e) => todos.dispatch(ToggleTodo(item.id))},
            ),
        ],
      )),
    ],
  );
}

See also:

Constructors

BloomReducer(BloomReducerFn<S, A> reducerFn, S initialState)
Creates a BloomReducer instance initialized with reducerFn and initialState.

Properties

hashCode int
The hash code for this object.
no setterinherited
history List<A>
An unmodifiable, chronological history of all actions dispatched to this reducer.
no setter
reducerFn BloomReducerFn<S, A>
The pure state-transition function that derives the next state on each dispatch.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state ReadonlySignal<S>
Reactive signal holding the current state.
no setter

Methods

dispatch(A action) → void
Dispatches action, synchronously computing and applying the next state via reducerFn.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited