# mrousavy/react-native-mmkv architecture

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

- Interactive diagram: https://gitdiagram.com/mrousavy/react-native-mmkv
- Repository: https://github.com/mrousavy/react-native-mmkv

## Overview

##### Main Components

a. Core Components:
- MMKV Native Module (C++ TurboModule)
- JavaScript/TypeScript API Layer
- Platform-specific implementations (iOS/Android/Web)
- React Hooks Layer

b. Storage Components:
- MMKV Storage Engine
- Encryption Layer
- File System Interface

**┌─ Application Layer ─────────────────────┐**
- React Components / Hooks                │
- ┤
- JavaScript/TypeScript API               │
- ┤
- JSI Bridge (C++ TurboModule)           │
- ┤
- Platform Implementations       │
- ┬───────────────┬────────┤
- iOS (Native)  │ Android       │ Web    │
- (Obj-C/C++)   │ (Java/C++)    │ (JS)   │
- ┴───────────────┴────────┘

##### Key Elements to Highlight

a. Data Flow:
- Synchronous nature of operations
- Encryption/decryption flow

b. Platform-specific Components:
- iOS implementation using MMKV native library
- Android implementation using MMKV native library
- Web implementation using LocalStorage/In-Memory fallback

c. Integration Points:
- React Hooks integration
- State management library connectors (Redux, MobX, etc.)
- Debugging tools (Flipper, Reactotron)

##### Special Features to Highlight

- Multi-instance support
- Encryption capabilities
- App Groups support
- Performance optimizations
- Cross-platform compatibility

##### Technical Details

Annotations for:
- JSI/TurboModule implementation
- Storage location customization
- Encryption key handling
- Performance metrics
- Memory management

##### Implementation Notes

- Direct native access through JSI
- Synchronous operation nature
- Security features
- Multiple storage instance support
- Platform-specific optimizations

##### Integration Points

Connections to:
- React Native New Architecture
- State management libraries
- Development tools
- Testing framework

Remember to highlight the key differentiating features:
- ~30x faster than AsyncStorage
- Synchronous operations
- Native C++ implementation
- New Architecture compatibility
- Multi-process support
- Encryption capabilities

This will provide a clear understanding of how the library works internally while maintaining the right level of abstraction for a principal software engineer to understand the system architecture.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Application Layer"
        RC[React Components]
        RH[React Hooks]:::hooks
    end

    subgraph "JavaScript API Layer"
        API[JavaScript/TypeScript API]:::api
        Types[Type Definitions]:::types
        Entry[Main Entry Point]:::entry
        Creator[MMKV Instance Creator]:::creator
        MemMgmt[Memory Management]:::memory
    end

    subgraph "JSI Bridge Layer"
        TurboMod[C++ TurboModule]:::turbo
        HostObj[Native Host Object]:::host
        NativeInt[Native Module Interface]:::nativeint
        PlatCtx[Platform Context]:::platctx
    end

    subgraph "Platform Implementation Layer"
        subgraph "iOS Native"
            IOSImpl[iOS Implementation]:::ios
            IOSLog[iOS Logger]:::ioslog
        end

        subgraph "Android Native"
            AndroidImpl[Android Implementation]:::android
            AndroidLog[Android Logger]:::androidlog
        end

        subgraph "Web"
            WebImpl[Web Implementation]:::web
        end
    end

    %% Data Flow Connections
    RC --> RH
    RH --> API
    API --> TurboMod
    API --> Types
    API --> Entry
    API --> Creator
    API --> MemMgmt
    TurboMod --> HostObj
    TurboMod --> NativeInt
    TurboMod --> PlatCtx
    HostObj --> IOSImpl
    HostObj --> AndroidImpl
    HostObj --> WebImpl
    IOSImpl --> IOSLog
    AndroidImpl --> AndroidLog

    %% Styling
    classDef hooks fill:#4a9eff,stroke:#333,stroke-width:2px
    classDef api fill:#3498db,stroke:#333,stroke-width:2px
    classDef types fill:#3498db,stroke:#333,stroke-width:2px
    classDef entry fill:#3498db,stroke:#333,stroke-width:2px
    classDef creator fill:#3498db,stroke:#333,stroke-width:2px
    classDef memory fill:#3498db,stroke:#333,stroke-width:2px
    classDef turbo fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef host fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef nativeint fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef platctx fill:#e74c3c,stroke:#333,stroke-width:2px
    classDef ios fill:#2ecc71,stroke:#333,stroke-width:2px
    classDef android fill:#2ecc71,stroke:#333,stroke-width:2px
    classDef web fill:#2ecc71,stroke:#333,stroke-width:2px
    classDef ioslog fill:#f1c40f,stroke:#333,stroke-width:2px
    classDef androidlog fill:#f1c40f,stroke:#333,stroke-width:2px

    %% Click Events
    click RH "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/hooks.ts"
    click API "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/MMKV.ts"
    click Types "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/Types.ts"
    click Entry "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/index.ts"
    click Creator "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/createMMKV.ts"
    click MemMgmt "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/MemoryWarningListener.ts"
    click TurboMod "https://github.com/mrousavy/react-native-mmkv/blob/main/package/cpp/NativeMmkvModule.cpp"
    click HostObj "https://github.com/mrousavy/react-native-mmkv/blob/main/package/cpp/MmkvHostObject.cpp"
    click NativeInt "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/NativeMmkv.ts"
    click PlatCtx "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/NativeMmkvPlatformContext.ts"
    click IOSImpl "https://github.com/mrousavy/react-native-mmkv/blob/main/package/ios/MmkvPlatformContextModule.mm"
    click AndroidImpl "https://github.com/mrousavy/react-native-mmkv/blob/main/package/android/src/main/java/com/mrousavy/mmkv/MmkvPackage.java"
    click WebImpl "https://github.com/mrousavy/react-native-mmkv/blob/main/package/src/createMMKV.web.ts"
    click IOSLog "https://github.com/mrousavy/react-native-mmkv/blob/main/package/ios/AppleLogger.mm"
    click AndroidLog "https://github.com/mrousavy/react-native-mmkv/blob/main/package/android/src/main/cpp/AndroidLogger.cpp"
```
