# udecode/zustand-x architecture

> Architecture diagram of the GitHub repository udecode/zustand-x, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/udecode/zustand-x
- Repository: https://github.com/udecode/zustand-x

## Overview

##### Main Components

a) Core Components:
- Store Factory System
- Selector System
- Action System
- Middleware System

b) Integration Components:
- React Components Layer
- Zustand Core Integration
- Global Store Management

**[Application Layer]**
- ↑↓
**[ZustandX Layer]**
- ↑↓
**[Zustand Core Layer]**

##### Specific Elements

a) Store Creation Flow:
- `createStore` factory function
- How initial state flows through the system
- Middleware integration points (immer, devtools, persist)

b) State Management Components:
- Selector System
- Hook Selectors
- Tracked Selectors (react-tracked integration)
- Getter Selectors
- Action System
- State Setters
- Extended Actions

c) Global Store Architecture:
- Root Store Combination
- Global Selectors
- Global Actions

##### Key Relationships to Highlight

- How the store factory creates individual stores
- Relationship between stores and their selectors/actions
- Demonstrate middleware pipeline
- Connection between global store and individual stores

##### Implementation Details

**[Component Name]**

**Key Functions**

**Input/Output**

**Dependencies**

##### Flow Patterns

- Data flow direction
- Component dependencies
- State updates
- Selector subscription patterns

##### Technical Details

- Store creation pipeline
- Middleware integration points
- Selector generation system
- Action extension system
- Global store mapping system

Remember to include notes about:
- TypeScript integration
- React integration points
- Middleware configuration
- Global store patterns

This will provide a comprehensive view of how ZustandX enhances Zustand with additional features while maintaining its core benefits of being small, fast, and scalable.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        RC[React Components]:::react
        click RC "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/useStore.spec.tsx"
    end

    subgraph "ZustandX Layer"
        direction TB
        SF[Store Factory]:::core
        click SF "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/storeFactory.ts"
        
        subgraph "Store Creation"
            CST[Create Store]:::core
            click CST "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/createStore.ts"
            
            subgraph "Configuration"
                CT[Config Types]:::types
                click CT "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/types/CreateStoreOptions.ts"
                IT[Immer Options]:::types
                click IT "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/types/ImmerOptions.ts"
                PT[Persist Options]:::types
                click PT "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/types/PersistOptions.ts"
            end
        end

        subgraph "Core Systems"
            direction TB
            
            subgraph "Selector System"
                ES[Extend Selectors]:::selector
                click ES "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/extendSelectors.ts"
                GS[State Get Selectors]:::selector
                click GS "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/generateStateGetSelectors.ts"
                HS[Hook Selectors]:::selector
                click HS "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/generateStateHookSelectors.ts"
                TS[Tracked Selectors]:::selector
                click TS "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/generateStateTrackedHooksSelectors.ts"
            end

            subgraph "Action System"
                EA[Extend Actions]:::action
                click EA "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/extendActions.ts"
                GA[Generate Actions]:::action
                click GA "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/generateStateActions.ts"
            end

            subgraph "Middleware System"
                IM[Immer Middleware]:::middleware
                click IM "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/middlewares/immer.middleware.ts"
                MW[Middleware Index]:::middleware
                click MW "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/middlewares/index.ts"
            end
        end

        subgraph "Utilities"
            PU[Pipe Utility]:::util
            click PU "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/pipe.ts"
            MV[Map Values]:::util
            click MV "https://github.com/udecode/zustand-x/blob/main/packages/zustand-x/src/utils/mapValuesKey.ts"
        end
    end

    subgraph "Zustand Core Layer"
        ZC[Zustand Core]:::zustand
    end

    %% Relationships
    RC --> SF
    SF --> CST
    CST --> CT & IT & PT
    CST --> ES & EA
    ES --> GS & HS & TS
    EA --> GA
    CST --> IM & MW
    CST --> ZC

    %% Styles
    classDef core fill:#2196F3,color:white
    classDef selector fill:#4CAF50,color:white
    classDef action fill:#FF9800,color:white
    classDef middleware fill:#9C27B0,color:white
    classDef react fill:#03A9F4,color:white
    classDef zustand fill:#607D8B,color:white
    classDef types fill:#9E9E9E,color:white
    classDef util fill:#795548,color:white

    %% Legend
    subgraph Legend
        L1[Core Components]:::core
        L2[Selector System]:::selector
        L3[Action System]:::action
        L4[Middleware]:::middleware
        L5[React Integration]:::react
        L6[Zustand Core]:::zustand
        L7[Types]:::types
        L8[Utilities]:::util
    end
```
