# pmndrs/valtio architecture

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

- Interactive diagram: https://gitdiagram.com/pmndrs/valtio
- Repository: https://github.com/pmndrs/valtio

## Overview

##### Main Components Structure

a) Core Proxy System
- Proxy creation flow using `proxy()` function
- Two types of proxies:
- Mutation tracking proxy (write operations)
- Usage tracking proxy (read operations)

b) React Integration Layer
- Connection between proxy state and React components
- `useSnapshot` hook as the primary bridge
- Subscription system

c) Utilities Layer
- Utility functions (proxyMap, proxySet, devtools, etc.)
- How they extend the core functionality

##### Data Flow Representation

**State Creation:**
**Raw Object → proxy() → Proxy State Object**

**React Integration:**
**Proxy State → useSnapshot() → Immutable Snapshot → React Component**

**State Updates:**
**Mutation → Proxy State → Subscription System → Component Re-render**

##### Component Details

Specific elements:

**Core:**
- Proxy State Handler
- Snapshot Generator
- Subscription Manager

**React Integration:**
- useSnapshot Hook
- Component Re-render Optimization
- Suspense Integration

**Utils:**
- DevTools Connection
- Map/Set Proxies
- Watch/Subscribe Utilities

##### Important Relationships

- Proxy State ←→ Snapshot (bi-directional relationship)
- Component → Snapshot (read-only access)
- Mutation → Proxy State (write access)
- Subscription System → Components (update notification)

##### Implementation Details to Highlight

Key features:
- Automatic proxy creation for nested objects
- Subscription system architecture
- Snapshot optimization system
- React integration points

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Core["Core Proxy System"]
        ProxyHandler["Proxy State Handler"]:::core
        SnapGen["Snapshot Generator"]:::core
        SubManager["Subscription Manager"]:::core
        
        ProxyHandler --> SnapGen
        SnapGen --> SubManager
    end
    
    subgraph React["React Integration Layer"]
        UseSnap["useSnapshot Hook"]:::react
        CompRender["Component Re-render"]:::react
        SuspenseInt["Suspense Integration"]:::react
        
        UseSnap --> CompRender
        UseSnap --> SuspenseInt
    end
    
    subgraph Utils["Utilities Layer"]
        ProxyMap["ProxyMap"]:::util
        ProxySet["ProxySet"]:::util
        DevTools["DevTools"]:::util
        Watch["Watch"]:::util
        Subscribe["Subscribe"]:::util
        DeepClone["DeepClone"]:::util
    end
    
    %% Main Data Flow
    RawState(("Raw State")):::data --> ProxyHandler
    ProxyHandler --> |"Mutation"| StateProxy[("Proxy State")]:::data
    StateProxy --> |"Read"| SnapGen
    SnapGen --> |"Generate"| Snapshot(("Immutable Snapshot")):::data
    
    %% React Integration Flow
    Snapshot --> UseSnap
    UseSnap --> CompRender
    StateProxy --> |"Subscribe"| SubManager
    SubManager --> |"Notify"| CompRender
    
    %% Utility Connections
    StateProxy --> DevTools
    StateProxy --> ProxyMap
    StateProxy --> ProxySet
    StateProxy --> Watch
    StateProxy --> Subscribe
    StateProxy --> DeepClone
    
    %% Legend
    classDef core fill:#2374ab
    classDef react fill:#57a773
    classDef util fill:#ffc857
    classDef data fill:#ffffff,stroke:#333,stroke-width:2px
    
    %% Click Events
    click ProxyHandler "https://github.com/pmndrs/valtio/blob/main/src/vanilla.ts"
    click UseSnap "https://github.com/pmndrs/valtio/blob/main/src/react.ts"
    click ProxyMap "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/proxyMap.ts"
    click ProxySet "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/proxySet.ts"
    click DevTools "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/devtools.ts"
    click Watch "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/watch.ts"
    click Subscribe "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/subscribeKey.ts"
    click DeepClone "https://github.com/pmndrs/valtio/blob/main/src/vanilla/utils/deepClone.ts"
```
