Cash App Sep 7, 2022

Molecule: Build a StateFlow stream using Jetpack Compose

Article Summary

Cash App open sourced Molecule, a library that lets you write reactive code as simple if/else statements. No more complex RxJava chains.

Benoît Quenaudon from Cash App explains how Molecule bridges Jetpack Compose's reactive runtime with traditional Kotlin flows and StateFlows. The library hit version 0.4.0 and is now running in production at Cash App.

Key Takeaways

Critical Insight

Molecule lets Android developers write reactive state management code imperatively using Compose's runtime, eliminating complex operator chains while maintaining full reactiveness.

The article reveals how Compose's reactive abstraction works under the hood and why it changes everything about state management.

About This Article

Problem

Android developers struggled to use Jetpack Compose's reactive runtime outside of composable functions. They had to write complex workarounds to connect composable methods with traditional Kotlin flows and StateFlows.

Solution

Cash App built the Molecule library to convert @Composable functions into Flow or StateFlow. The moleculeFlow() and launchMolecule() APIs handle this conversion. It supports two recomposition clocks: ContextClock for frame-based updates and Immediate for eager emission.

Impact

Molecule reached version 0.4.0 and went into production at Cash App. Presenters written as composable methods are now simpler to write, review, and test compared to traditional reactive patterns.

Recent from Cash App

Related Articles