# lacti/typescript-webworker-rpc architecture

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

- Interactive diagram: https://gitdiagram.com/lacti/typescript-webworker-rpc
- Repository: https://github.com/lacti/typescript-webworker-rpc

## Overview

##### Main Components

a) Client-Side Components:
- RPCClient Module
- Methods: call(), post(), onError()
- Window Context

b) Server-Side Components:
- RPCServer Module
- Methods: on(), onError()
- Web Worker Context

c) Communication Layer:
- Message Channel
- Transfer Objects

##### Relationship Flows

- Client to Server: postMessage flow
- Server to Client: response flow
- Error handling paths (in red)
- Transfer objects flow (for ArrayBuffer transfers)

##### Specific Implementation Details

Technical elements:
- TypeScript interfaces (show as small interface definitions)
- Promise handling layer
- Message queue system
- Transfer object handling

- Window      |                    |  Web Worker   |
- postMessage     |  +----------+ |
- RPCClient |<-|------------------>|->|RPCServer | |

##### Additional Elements

Supporting components:
- Error handling flow paths
- Transfer object specifications

##### Implementation Notes

Annotations for:
- TypeScript type safety
- Promise-based communication
- Transferable object handling
- Fire-and-forget operations (post method)

Key Considerations:

##### Type System
- How TypeScript interfaces ensure type safety
- Generic type parameters in the design

##### Communication Patterns
- Both synchronous (call) and asynchronous (post) patterns
- Error propagation paths

##### Special Features
- ArrayBuffer transfer capabilities
- Duplex communication support

- The clean separation between client and server components
- Type-safe communication channels
- Bidirectional messaging capabilities
- Error handling paths
- Support for transferable objects

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Window
        Client["RPCClient Module"]
        ClientMethods["Methods:
        - call()
        - post()
        - onError()"]
        Client --- ClientMethods
    end

    subgraph WebWorker
        Server["RPCServer Module"]
        ServerMethods["Methods:
        - on()
        - onError()"]
        Server --- ServerMethods
    end

    subgraph CommunicationLayer
        Channel["Message Channel"]
        TransferObj["Transfer Objects
        (ArrayBuffer)"]
    end

    subgraph TypeSystem
        Interfaces["TypeScript Interfaces"]
        TypeUtils["Type Utilities"]
        PromiseHandler["Promise Handling"]
    end

    Client --> Channel
    Channel --> Server
    Server --> Channel
    Channel --> Client
    TransferObj --> Channel

    Interfaces --> Client
    Interfaces --> Server
    TypeUtils --> Client
    TypeUtils --> Server
    PromiseHandler --> Client
    PromiseHandler --> Server

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px
    classDef error fill:#ffecec,stroke:#ff0000
    classDef transfer fill:#fff3cd,stroke:#856404
    classDef typescript fill:#e8f4ff,stroke:#0066cc
    classDef communication fill:#e8ffe8,stroke:#28a745

    class Channel communication
    class TransferObj transfer
    class Interfaces,TypeUtils typescript
    class PromiseHandler typescript

    click Client "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/client.ts"
    click Server "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/server.ts"
    click Interfaces "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/types.ts"
    click TypeUtils "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/utils/type.ts"
    click PromiseHandler "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/utils/promise.ts"
    click Channel "https://github.com/lacti/typescript-webworker-rpc/blob/master/src/abstract.ts"

    subgraph Legend
        L1[Client/Server Components]:::default
        L2[Error Handling]:::error
        L3[Transfer Objects]:::transfer
        L4[TypeScript Elements]:::typescript
        L5[Communication]:::communication
    end
```
