# d-led/mermaidlive architecture

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

- Interactive diagram: https://gitdiagram.com/d-led/mermaidlive
- Repository: https://github.com/d-led/mermaidlive

## Overview

Key Components to Include:

##### Client Layer
- Long-polling connection management
- Connection status display
- Event subscription handling

##### Server Layer
- HTTP Server (Go-based)
- Resource Management (embedded UI resources)
- State Machine Controller
- Pub/Sub Event System
- File Watcher (for development)

##### Distributed System Layer
- G-Counter CRDT Implementation
- ZeroMQ Mesh Network
- Peer Discovery System
- Persistent Storage

- Rectangles: Core services
- Hexagons: External systems
- Diamonds: Decision points

##### Component Relationships
**→ Synchronous calls**
**⇢ Asynchronous events**
**⇄ Bi-directional communication**

- Blue: Client-side components
- Green: Server-side components
- Orange: Distributed system components
- Gray: External services/dependencies

##### Event Flow
- How commands flow from UI → Server → State Machine
- Event publication path: State Machine → PubSub → Connected Clients
- Demonstrate the long-polling mechanism for client updates

##### Distributed System
- Fully-connected ZeroMQ mesh between replicas
- Peer discovery through fly.io DNS
- CRDT state replication flow
- Persistent storage interaction

##### Development Features
- Live reload mechanism
- Resource compilation/embedding flow
- Test infrastructure connections

Additional Recommendations:

- CRDT replication process
- Client connection lifecycle
- State machine transitions

- Client connection establishment
- State updates and propagation
- Replica synchronization

Remember to include testing infrastructure in a separate view, as the project has comprehensive testing at unit, API, and browser levels using shared Gherkin features.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Style Definitions
    classDef client fill:#a8d1f0,stroke:#333
    classDef server fill:#90EE90,stroke:#333
    classDef distributed fill:#FFB347,stroke:#333
    classDef storage fill:#D3D3D3,stroke:#333
    classDef test fill:#E6E6FA,stroke:#333

    subgraph ClientLayer["Client Layer"]
        UI["Browser UI"]:::client
        MermaidRenderer["Mermaid Renderer"]:::client
        ConnManager["Connection Manager"]:::client
        StatusDisplay["Status Display"]:::client
        EventHandler["Event Handler"]:::client
    end

    subgraph ServerLayer["Server Layer"]
        WebServer["HTTP Server"]:::server
        StateMachine["State Machine"]:::server
        PubSub["Event System"]:::server
        ResourceMgr["Resource Manager"]:::server
        FileWatcher["File Watcher"]:::server
    end

    subgraph DistributedLayer["Distributed Layer"]
        ClusterMgr["Cluster Manager"]:::distributed
        Counter["Counter CRDT"]:::distributed
        PeerDiscovery["Peer Discovery"]:::distributed
        ClusterMonitor["Cluster Monitor"]:::distributed
        Storage[(Persistent Storage)]:::storage
    end

    subgraph TestLayer["Test Infrastructure"]
        BehaviorTests["Behavior Specs"]:::test
        UITests["UI Tests"]:::test
        APITests["API Tests"]:::test
    end

    %% Relationships
    UI --> ConnManager
    UI --> MermaidRenderer
    UI --> StatusDisplay
    ConnManager --> EventHandler
    
    EventHandler --> WebServer
    WebServer --> StateMachine
    StateMachine --> PubSub
    PubSub --> EventHandler
    FileWatcher --> ResourceMgr
    ResourceMgr --> WebServer
    
    StateMachine --> ClusterMgr
    ClusterMgr --> Counter
    ClusterMgr --> PeerDiscovery
    ClusterMonitor --> ClusterMgr
    Counter --> Storage
    
    BehaviorTests --> UITests
    BehaviorTests --> APITests
    
    %% Click Events
    click UI "https://github.com/d-led/mermaidlive/blob/main/ui-src/index.html"
    click ConnManager "https://github.com/d-led/mermaidlive/blob/main/ui-src/cluster.ts"
    click WebServer "https://github.com/d-led/mermaidlive/blob/main/server.go"
    click StateMachine "https://github.com/d-led/mermaidlive/blob/main/async_fsm.go"
    click FileWatcher "https://github.com/d-led/mermaidlive/blob/main/watch.go"
    click ResourceMgr "https://github.com/d-led/mermaidlive/blob/main/resources.go"
    click ClusterMgr "https://github.com/d-led/mermaidlive/blob/main/cluster.go"
    click PeerDiscovery "https://github.com/d-led/mermaidlive/blob/main/fly_peer_locator.go"
    click PubSub "https://github.com/d-led/mermaidlive/blob/main/event.go"
    click Counter "https://github.com/d-led/mermaidlive/blob/main/counter_listener.go"
    click ClusterMonitor "https://github.com/d-led/mermaidlive/blob/main/cluster_observer.go"
    click BehaviorTests "https://github.com/d-led/mermaidlive/tree/main/features"
    click UITests "https://github.com/d-led/mermaidlive/blob/main/src/steps/ui.steps.ts"
    click APITests "https://github.com/d-led/mermaidlive/blob/main/api_steps_test.go"

    %% Legend
    subgraph Legend
        ClientComp["Client Component"]:::client
        ServerComp["Server Component"]:::server
        DistributedComp["Distributed Component"]:::distributed
        StorageComp["Storage"]:::storage
        TestComp["Test Component"]:::test
    end
```
