# edrlab/thorium-reader architecture

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

- Interactive diagram: https://gitdiagram.com/edrlab/thorium-reader
- Repository: https://github.com/edrlab/thorium-reader

## Overview

Based on both the file tree and the README, Thorium Reader is a desktop application built with Electron, using a multi-process architecture (one Node.js main process and multiple renderer processes) and a clear Model–View–Controller (MVC) pattern implemented via Redux (for state management) and Redux-saga (for side effects). The application also uses React for rendering the views, and the integration between the main and renderer processes is achieved using Electron’s IPC/RPC mechanisms.

##### Main Components
- Main Process (Electron back-end):
– This is the central node.js process (“src/main”) containing the core business logic.
– It includes modules for handling CLI commands, database access, PDF handling, network operations, and redux state/sagas.
- Renderer Processes:
– Library Window: A renderer (in “src/renderer/library”) that provides the library interface where users see the catalog, settings, and dialogs.
– Reader Window(s): One or more renderer processes (in “src/renderer/reader”) responsible for displaying the reading interface.
- Shared/Utility Components:
– Common Code Layer: Located in “src/common”, includes shared API definitions, models, IPC handlers, and Redux actions/reducers.
– External modules: These include proxy handling, various helper utilities (e.g., for HTTP requests, file system operations, etc.), and integration with third-party libraries like i18next.

##### Architectural Layers and Their Relationships
- At the top level, show the Electron “Host” environment with the Main Process.
- Under the Main Process, include subcomponents such as:
– Router/DI and Application services (files in src/main/di.ts, src/main/event.ts, etc.)
– Database access module (src/main/db)
– API endpoint implementations (inside src/main/redux/sagas/api)
– Tools and utilities (located in src/main/tools, src/main/network, etc.)
– Library Window (with its own Redux store, React components, and IPC integration)
– Reader Window(s) (again with Redux, React view components, and distinct renderers for reading content)
– Model: Redux state and reducers
– Controller: Redux-saga middleware handling asynchronous processes
– View: React components (built as class components, as noted in the README)

##### API Concept and IPC Communication
- Separate Redux actions and reducers for the various domains (catalog, reader, publication, etc.) that illustrate the flow from action initiation to state updates.

##### Design Patterns & Technologies
- MVC pattern in each renderer:
– Models (Redux state), Controllers (Redux-saga), and Views (React UI).
- Labels for significant technologies:
– Electron (Main Process)
– React + Redux + Saga (Renderer Processes)
– TypeScript (the entire codebase)
– IPC mechanisms and API endpoints linking processes

##### Detail Over Breadth
- While the file tree is extensive, focus on capturing the major architectural elements. It is best not to include every single folder/file, but rather to abstract them into logical components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Electron Main Process and its Modules
    subgraph "Electron Main Process"
        mainProc["Main Process"]:::backend
        cli["CLI Commands Module"]:::module
        db["Database Access Module"]:::module
        pdf["PDF Handling Module"]:::module
        network["Network Operations Module"]:::module
        reduxMain["Redux State/Sagas"]:::module
        apiConcept["API Endpoints(e.g.,opds/getFeed,publication/importFromFs)"]:::api
    end

    %% Renderer Processes with MVC structure
    subgraph "Renderer Processes"
        subgraph "Library Window Renderer (MVC)"
            libModel["Model: Redux State"]:::model
            libController["Controller: Redux-Saga"]:::controller
            libView["View: React Components"]:::view
        end

        subgraph "Reader Window Renderer (MVC)"
            readerModel["Model: Redux State"]:::model
            readerController["Controller: Redux-Saga"]:::controller
            readerView["View: React Components"]:::view
        end
    end

    %% Shared/Common Components
    common["Shared/Common Code Layer"]:::common

    %% IPC Communication between Renderer and Main Process
    libView -->|"IPC/RPC"| mainProc
    readerView -->|"IPC/RPC"| mainProc

    %% MVC internal interactions for Library Window
    libView -->|"dispatch"| libController
    libController -->|"update"| libModel
    libModel -->|"render"| libView

    %% MVC internal interactions for Reader Window
    readerView -->|"dispatch"| readerController
    readerController -->|"update"| readerModel
    readerModel -->|"render"| readerView

    %% Main Process internal dependencies
    cli --> mainProc
    db --> mainProc
    pdf --> mainProc
    network --> mainProc
    reduxMain --> mainProc
    apiConcept --> reduxMain

    %% Shared Code impacts Main and Renderer Layers
    common --- mainProc
    common --- libView
    common --- readerView

    %% API Flow: Renderer controllers call API endpoints via Redux-Saga in Main Process
    libController -->|"API_Call"| reduxMain
    readerController -->|"API_Call"| reduxMain

    %% State updates from Main Process to Renderers via IPC
    reduxMain -->|"State_Update"| libView
    reduxMain -->|"State_Update"| readerView

    %% Click Events for Component Mappings
    click mainProc "https://github.com/edrlab/thorium-reader/tree/develop/src/main"
    click cli "https://github.com/edrlab/thorium-reader/tree/develop/src/main/cli"
    click db "https://github.com/edrlab/thorium-reader/tree/develop/src/main/db"
    click pdf "https://github.com/edrlab/thorium-reader/tree/develop/src/main/pdf"
    click network "https://github.com/edrlab/thorium-reader/tree/develop/src/main/network"
    click reduxMain "https://github.com/edrlab/thorium-reader/tree/develop/src/main/redux"
    click libView "https://github.com/edrlab/thorium-reader/tree/develop/src/renderer/library"
    click readerView "https://github.com/edrlab/thorium-reader/tree/develop/src/renderer/reader"
    click common "https://github.com/edrlab/thorium-reader/tree/develop/src/common"

    %% Styles Classes
    classDef backend fill:#FFDDC1,stroke:#C94C4C,stroke-width:2px;
    classDef module fill:#F7C5CC,stroke:#B9184B,stroke-width:2px;
    classDef api fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef model fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef controller fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef view fill:#D1C4E9,stroke:#7E57C2,stroke-width:2px;
    classDef common fill:#FFECB3,stroke:#FF9800,stroke-width:2px;
```
