How cashapp/molecule works
Main Components
• Molecule Core Library (molecule-runtime): This module holds the primary implementation including the API package definitions (for Android, JVM, and common Kotlin code) as well as the composable functions, clock implementations (like AndroidUiFrameClock, WindowAnimationFrameClock, DisplayLinkClock, etc.), and reactive state generation logic.
• Platform-specific Implementations: Under molecule-runtime, you have distinct source sets for different platforms (androidMain, browserMain, jvmMain, linuxMain, macosMain, mingwMain, darwinMain, etc.). Each of these implements platform-oriented features (e.g., unique Clock implementations and underlying time sources).
• Samples / Usage Examples: The “sample-viewmodel” and “sample” directories serve as example applications that show how a client integrates Molecule’s API to create ViewModels and wire up UI (via Android Manifest, layouts, etc.).
• Build & CI Components: The Gradle build files (build.gradle, gradle-wrapper, gradle.properties, etc.) and GitHub workflows indicate automated build, testing, and release processes.
Interactions and Relationships
• That the molecules (composable functions) produce state streams (StateFlow/Flow) that are consumed in UI components (even if only indirectly referenced in the README). This separation is key to the design.
• That the build and CI configuration (Gradle scripts, GitHub workflows) supports building, testing (using Turbine for flows and other tests in the commonTest directories), and publishing the library.
Architectural Patterns and Design Principles
• Separation of concerns: The logic that produces the state model is isolated from UI rendering. The Presenter-like objects (composable functions) are responsible solely for state management, while examples focus on consuming these state streams.
• Multiplatform/Modular design: The use of commonMain and platform-specific source sets displays a clean architecture that targets different platforms with a unified API.
• Reactive and Declarative Programming: The system is designed around Kotlin’s Flow and StateFlow along with Jetpack Compose’s composable paradigm.
• Clock Abstraction: The clear separation between recomposition modes (ContextClock vs. Immediate) is essential – it’s an important design aspect that ensures the library can work both on the Android UI thread and in unit tests.
Relevant Technologies and Dependencies
• Kotlin and Jetpack Compose (compiler and Compose runtime).
• Coroutines (for building and managing asynchronous code).
• Gradle (for multi-module build management).
• GitHub Actions (for CI/CD workflows).
• Testing frameworks (e.g., Turbine, as shown in the README for unit tests).
Detailed Elements
• How Samples consume the Molecule API.
• How the composable logic interacts with platform clocks.
• How the core module combines reactive flows.
• Annotating blocks with names of technologies (e.g., Kotlin Coroutines, Jetpack Compose, Gradle) where appropriate.
• Architectural pattern (separation of UI and business logic, and multiplatform support).