# cashapp/molecule architecture

> Architecture diagram of the GitHub repository cashapp/molecule, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cashapp/molecule
- Repository: https://github.com/cashapp/molecule

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build & CI Components
    subgraph "Build & CI"
        BuildScripts["Build Scripts & Gradle"]:::build
        CICD["CI/CD Workflows"]:::build
    end

    %% Molecule Core Library
    subgraph "Molecule Core Library (molecule-runtime)"
        Molecule_Core["Molecule Core Library (molecule-runtime)"]:::core
        subgraph "Core Implementation"
            API_Def["API Definitions (molecule-runtime/api)"]:::core
            CommonMain["Common Composable Logic (molecule-runtime/src/commonMain)"]:::core
            CommonTest["Shared Testing Logic (molecule-runtime/src/commonTest)"]:::core
        end
        subgraph "Platform-specific Implementations"
            Android["Android (molecule-runtime/src/androidMain)"]:::platform
            Browser["Browser (molecule-runtime/src/browserMain)"]:::platform
            JVM["JVM (molecule-runtime/src/jvmMain)"]:::platform
            Darwin["Darwin/macOS (molecule-runtime/src/darwinMain/macosMain)"]:::platform
            Linux["Linux (molecule-runtime/src/linuxMain)"]:::platform
            Mingw["Mingw (molecule-runtime/src/mingwMain)"]:::platform
            DisplayLink["DisplayLink (molecule-runtime/src/displayLinkMain)"]:::platform
            DisplayLinkTest["DisplayLink Tests (molecule-runtime/src/displayLinkTest)"]:::platform
            QuartzCore["QuartzCore (molecule-runtime/src/quartzCoreMain)"]:::platform
        end
        Reactive["Reactive Streams (StateFlow/Flow)"]:::flow
    end

    %% Sample Projects
    subgraph "Sample Projects"
        SampleVM["Sample ViewModel (sample-viewmodel)"]:::sample
        GeneralSample["General Sample (sample)"]:::sample
    end

    %% Build & CI interactions
    BuildScripts -- "builds" --> Molecule_Core
    CICD -- "deploys" --> Molecule_Core

    %% Core interactions
    CommonMain -- "produces" --> Reactive
    Molecule_Core -- "provides" --> API_Def

    %% Delegation to platform-specific implementations
    API_Def -- "delegates" --> Android
    API_Def -- "delegates" --> Browser
    API_Def -- "delegates" --> JVM
    API_Def -- "delegates" --> Darwin
    API_Def -- "delegates" --> Linux
    API_Def -- "delegates" --> Mingw
    API_Def -- "delegates" --> DisplayLink
    API_Def -- "delegates" --> QuartzCore
    DisplayLink -- "testedBy" --> DisplayLinkTest

    %% Sample Projects Consumption of Reactive Streams & Core API
    Reactive -- "consumedBy" --> SampleVM
    Reactive -- "consumedBy" --> GeneralSample
    SampleVM -- "uses" --> API_Def
    GeneralSample -- "uses" --> API_Def

    %% Class Definitions
    classDef core fill:#ffe6f7,stroke:#cc0099,stroke-width:2px;
    classDef platform fill:#e6f7ff,stroke:#0066cc,stroke-width:2px;
    classDef sample fill:#e6ffe6,stroke:#339933,stroke-width:2px;
    classDef build fill:#fff2cc,stroke:#cc7a00,stroke-width:2px;
    classDef flow fill:#ffe6cc,stroke:#cc6600,stroke-width:2px;

    %% Click Events
    click Molecule_Core "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime"
    click API_Def "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/api"
    click CommonMain "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/commonMain"
    click CommonTest "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/commonTest"
    click Android "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/androidMain"
    click Browser "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/browserMain"
    click JVM "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/jvmMain"
    click Darwin "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/darwinMain"
    click Linux "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/linuxMain"
    click Mingw "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/mingwMain"
    click DisplayLink "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/displayLinkMain"
    click DisplayLinkTest "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/displayLinkTest"
    click QuartzCore "https://github.com/cashapp/molecule/tree/trunk/molecule-runtime/src/quartzCoreMain"
    click SampleVM "https://github.com/cashapp/molecule/tree/trunk/sample-viewmodel"
    click GeneralSample "https://github.com/cashapp/molecule/tree/trunk/sample"
    click BuildScripts "https://github.com/cashapp/molecule/blob/trunk/build.gradle"
    click CICD "https://github.com/cashapp/molecule/tree/trunk/.github/workflows"
```
