Cocoa Beans 0.0.54-beta0 Help

πŸ“Ί Observable api

Available Since 0.0.39

Table of content:

Introduction

The Observable<T> interface represents a reactive, stateful object that can be watched for changes. It provides a consistent and flexible API for building reactive UIs, data flows, and dynamic logic in applications.

Observables are designed to be either immutable or mutable, with support for compound states that automatically derive values from other observables. It includes built-in support for list-based observables, mapped observables, and reactive recomposition through dependency tracking.

This system is designed to be lightweight, composable, and extensible.

Usage

@Test void example() { MutableObservable<Integer> num = Observable.mutable(0); Observable<Boolean> isEven = num.map(n -> n % 2 == 0); Observable<String> parity = Observable.compound(num, isEven) .map(args -> args.arg0() + ": " + args.arg1()); assertEquals("0: true", parity.get()); num.set(9); // Flag isEven and parity as dirty but don't recompute yet assertEquals("9: false", parity.get()); num.set(42); // Flag isEven and parity as dirty but don't recompute yet assertEquals("42: true", parity.get()); num.set(21); // Flag isEven and parity as dirty but don't recompute yet num.set(42); // Flag isEven and parity as dirty but don't recompute yet assertEquals("42: true", parity.get()); // output "42: true" but didn't need to recompute // the final piece of the puzzle: if num reaches 69, we want to shut down the JVM WatcherManager watcherManager = new WatcherManager(); num.lazyWatch(watcherManager, value -> { if (value == 69) { System.exit(69); } }); // In order to trigger the watching function, we need to call the heartbeat method // If the value of num is 69, the JVM will shut down // Right now it's not, we don't want to destroy our CI watcherManager.heartbeat(); }
@Test void listExample() { ListObservable<String> names = Observable.list(); // Create an ArrayList<String> assertEquals(List.of(), names.get()); names.add("Kfir"); assertEquals(List.of("Kfir"), names.get()); names.addAll(List.of("Lior", "Tom")); assertEquals(List.of("Kfir", "Lior", "Tom"), names.get()); names.remove("Tom"); assertEquals(List.of("Kfir", "Lior"),names.get()); Observable<List<String>> namesLength = names.map(list -> list.stream() .map(name -> name + ": " + name.length()) .toList() ); assertEquals(List.of("Kfir: 4", "Lior: 4"), namesLength.get()); names.add("Elion"); assertEquals(List.of("Kfir: 4", "Lior: 4", "Elion: 5"), namesLength.get()); }

How it works

Observable is a reactive object that can be watched for changes,And can be used to build reactive UIs, data flows, and dynamic logic in applications Also when we say something is dirty it means that it probably needs to be re-computedBut we talk about when it will re-compute it and when it will not re-compute it later,Also isDirty mean that the parent had changed and we don't just update the child immediately we wait for Observable#get to be calledAfter Observable#get is being called it will make it clean againAnd if the Observable is not dirty it will not re-compute it at allObservervoid flagAsDirty(Observable<?> observable)Flag listener as dirtyObservableTT get()void observe(Observer observer)<M> Observable<M> map(Function<T, M> mapper)AttachedWatcher<T> watch(WatcherManager watcherManager, Consumer<T> consumer)Will return the value of the state if it is dirty it will recompute itAdds an observer to the stateMaps the value of the observable using the given function when it is dirtyCreate a watcher for the observable that will be attached to the given watcher manager and run the given actionMappedObservableF, TObservable<F> dependsF prevboolean isDirtyboolean firstT currentFunction<F, T> mapperImmutableObservableTT valueMutableObservableTT value--void set(T value)Set the value of the observable and notify listenersCollectionObservableE, C extends Collection<E>void add(E value)void remove(E value)boolean addAll(Collection<? extends E> collection)boolean removeAll(Collection<? extends E> collection)boolean removeIf(Predicate<? super E> filter)boolean retainAll(Collection<? extends E> collection)void clear()ListObservableEvoid add(int index, E element)E remove(int index)void sort(Comparator<? super E> comparator);SetObservableESet interface is for easier use and code readabilityWatcherTObservable<T> dependsConsumer<T> consumerboolean firstboolean isDirtyT prevValuevoid heartbeat()WatcherOperatorvoid detach(AttachedWatcher<?> watcher);AttachedWatcherTWatcherOperator managervoid attach(WatcherOperator manager)boolean isAttached()WatcherOperator getManager()WatcherManagerSet<Watcher<?>> watchersvoid heartbeat()<T> AttachedWatcher<T> watch(Observable<T> depends, Consumer<T> consumer)ObservableCompoundTboolean isDirtyboolean firstMap<Observable<?>, Object> dependsOnFunction<List<?>, T> singularMapper;T current

Advantages

βœ… Type-safe reactive model with strong generics and records.

♻️ Immutable and Mutable models support both predictable state and dynamic changes.

πŸ”— Composable—easy to derive, map, and observe values.

🧩 Extensible—can be combined into complex reactive graphs with minimal boilerplate.

πŸš€ Designed for performance and clarity with lazy re-computation and controlled observers.

Include in your project

<dependencies> <dependency> <groupId>dev.apartium.cocoa-beans</groupId> <artifactId>cocoa-beans-state</artifactId> <version>0.0.54-beta0</version> </dependency> </dependencies>
repositories { mavenCentral() } dependencies { compileOnly("dev.apartium.cocoa-beans:cocoa-beans-state:0.0.54-beta0") }
repositories { mavenCentral() } dependencies { compileOnly("dev.apartium.cocoa-beans:cocoa-beans-state:0.0.54-beta0") }

What next

Last modified: 05 March 2026