Cocoa Beans 0.0.54-beta0 Help

πŸ“‹ Mutable Observable

Available Since 0.0.39

Introduction

Observable#mutable returns a reactive container that allows both reading and updating its internal value. It implements the Observable<T> interface and provides a set(T) method to modify the current state.

Mutable observers are the foundation for building dynamic, reactive state in your application.

Any change to the state triggers notifications to dependent observers and re-computes them as needed.

Usage

@Test void mutable() { MutableObservable<Integer> observable = Observable.mutable(123); assertEquals(123, observable.get()); observable.set(456); // flag his observers dirty assertEquals(456, observable.get()); }

You can use it directly, or combine it with mapping, watching, or composing multiple observables.

How it works

A MutableObservable keeps an internal value and a list of dependent observers. When set() is called: 1. The internal value is updated.

2. All observers are notified and may trigger their effects via Observer#flagAsDirty

If the new value is equal to the old one (by Object#equals), dependents will not be notified, thereby improving performance.

Advantages

  • πŸ“ Mutable state: Ideal for reactive inputs, counters, toggles, and form fields.

  • πŸ”” Notifies observers: Automatically propagates changes through the system.

  • πŸ”— Composable: Works with mapping, compound logics and other observables.

  • ⚑ Efficient: No re-computation or dirty state.

Last modified: 07 July 2025