# camflan/use-event-source architecture

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

- Interactive diagram: https://gitdiagram.com/camflan/use-event-source
- Repository: https://github.com/camflan/use-event-source

## Overview

##### Main Components

a) Client-Side Components:
- React Application
- useEventSource Hook
- EventSource Interface
- State Management (external - user provided)

b) Server-Side Components:
- SSE Server Endpoint (external)

**┌─ Client Application Layer ─────────────────────┐**

- ┌─ React Component ─────────────────────┐    │

- ┌─ useEventSource Hook ──────────┐  │    │

- Configuration              │  │    │
- Connection Management      │  │    │
- Error Handling            │  │    │
- ┬─────────────┘  │    │

- ┼─────────────────┘    │

- ┌─ EventSource ─────┼───────────────────┐  │
- Native Browser API│                   │  │
- ┬─┘                   │  │

- ┼─────────────────────┘  │

**┌───────────────────▼──────────────────────┐**
- SSE Server Endpoint (External)     │
- ┘

##### Important Flows to Highlight

a) Connection Flow:
- Component initialization → Hook initialization → EventSource connection
- Manual connection through openStream()
- Disconnection through closeStream()

b) Data Flow:
- SSE Server → EventSource → Hook → onMessage callback → User's state management

c) Error Flow:
- Error occurrence → Hook error state → Component error handling

##### States

- Connection States:
- "connected"
- "connecting"
- "disconnected"

##### Implementation Details

- Hook return values:
- {
- error: Error
- connectionStatus: string
- openStream: () => void
- closeStream: () => void
- }

##### Notes

- Keep the focus on the hook's interaction with EventSource
- Clear boundaries between library code and user code
- Event-driven nature of the system
- Configuration options
- Error propagation paths

Remember to indicate that the SSE server is external to the system, as this library only handles the client-side implementation of SSE connections.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client ["Client Application Layer"]
        subgraph React ["React Application"]
            App["Example Application"]:::app
            Button["Connection Control"]:::component
            Badge["Status Display"]:::component
            RevisionEntry["Event Data Display"]:::component
            
            subgraph Hook ["useEventSource Hook"]
                Config["Configuration"]:::config
                ConnMgmt["Connection Management"]:::core
                ErrHandle["Error Handling"]:::core
                ReturnVals["Return Values:<br/>- error<br/>- connectionStatus<br/>- openStream<br/>- closeStream"]:::interface
            end
            
            App --> Button
            App --> Badge
            App --> RevisionEntry
            App --> Hook
        end
        
        EventSource["Browser EventSource API"]:::browser
        
        Hook --> EventSource
    end
    
    SSEServer["SSE Server Endpoint<br/>(External)"]:::external
    
    EventSource --> SSEServer
    
    %% Data Flow
    SSEServer -->|Events| EventSource
    EventSource -->|Data| Hook
    Hook -->|Status/Data| App
    
    %% Click Events
    click App "https://github.com/camflan/use-event-source/blob/main/example/App.tsx"
    click Button "https://github.com/camflan/use-event-source/blob/main/example/Button.tsx"
    click Badge "https://github.com/camflan/use-event-source/blob/main/example/Badge.tsx"
    click RevisionEntry "https://github.com/camflan/use-event-source/blob/main/example/RevisionEntry.tsx"
    click Hook "https://github.com/camflan/use-event-source/blob/main/src/index.tsx"
    
    %% Styles
    classDef app fill:#e1f5fe,stroke:#01579b
    classDef component fill:#e8f5e9,stroke:#2e7d32
    classDef core fill:#bbdefb,stroke:#1976d2
    classDef config fill:#fff3e0,stroke:#f57c00
    classDef interface fill:#f3e5f5,stroke:#7b1fa2
    classDef browser fill:#e8eaf6,stroke:#3f51b5
    classDef external fill:#eeeeee,stroke:#424242
    
    %% Legend
    subgraph Legend
        L1["Application Component"]:::app
        L2["UI Component"]:::component
        L3["Core Logic"]:::core
        L4["Configuration"]:::config
        L5["Interface"]:::interface
        L6["Browser API"]:::browser
        L7["External System"]:::external
    end
```
