# testpress/react-native-tpstreams architecture

> Architecture diagram of the GitHub repository testpress/react-native-tpstreams, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/testpress/react-native-tpstreams
- Repository: https://github.com/testpress/react-native-tpstreams

## Overview

##### Project type and purpose
- This is an open-source React Native native-module library (“react-native-tpstreams”) that exposes TPStreams video-playback and download functionality to a React Native app.
- It contains:
- A JavaScript/TypeScript layer (in `src/`) that defines the public API (player component, download functions).
- Native modules for Android (Kotlin/Java under `android/src/main/java/com/tpstreams/*`) and iOS (Swift/Obj-C under `ios/`).
- An `example/` React Native application demonstrating integration.
- CI/CD and packaging config (`.github/`, `package.json`, `TPStreamsRNPlayerView.podspec`, Gradle files).

##### File structure
- Top-level directories:
- `src/` – JS/TS implementation of the React Native bridge API and UI component.
- `android/` – Android native module and view manager implementations.
- `ios/` – iOS native module and view manager implementations.
- `example/` – Demo React Native app to test the library on both platforms.
- Config files and build scripts:
- `babel.config.js`, `tsconfig.json`, `package.json` – define build targets and dependencies.
- `TPStreamsRNPlayerView.podspec` – iOS CocoaPods spec.
- `.github/workflows/*.yml` – CI for lint/test and npm publishing.

##### README for additional insights
- Describes initialization (`TPStreams.initialize(orgId)`), UI component (`<TPStreamsPlayerView>`), player & download methods, events, and a complete example of real-time download progress.
- Reveals two primary subsystems: video playback and download management, each with JS methods calling into native.

##### Main components of the system (
a. React Native JavaScript layer
- `TPStreams.initialize(...)`
- `TPStreamsPlayerView` component (JS wrapper)
- Download API functions (`pauseDownload`, `resumeDownload`, etc.)
b. React Native Bridge Layer
- EventEmitter channels
- Method invoker
c. Android native module
- `TPStreamsRNModule` (initialization, playback commands)
- `TPStreamsRNPlayerViewManager` (native view for video)
- `TPStreamsDownloadModule` (download commands & progress)
- Underlying ExoPlayer or TPStreams Android SDK
d. iOS native module
- `TPStreamsModule` (init & playback commands)
- `TPStreamsRNPlayerViewManager` (native view using AVPlayer)
- `TPStreamsDownloadModule` (download commands & events)
- Underlying TPStreams iOS SDK
e. External TPStreams backend service
- Video streaming CDN/API
- Token refresh endpoint
f. Device subsystems
- Media playback engine (ExoPlayer/AVPlayer)
- Local file storage / SQLite or file system for offline downloads
g. Example App (consumer)
- Embeds JS layer + native modules via `react-native-tpstreams` dependency

##### Relationships and interactions
- JS calls (init, play/pause, download control) → React Native Bridge → invokes native module methods.
- Native module → interacts with TPStreams SDK (network calls, media engine) → returns results/events via EventEmitter over the bridge → JS listeners.
- Download progress: native download manager reports real-time progress events → JS state updates in `onDownloadProgressChanged`.
- Token renewal: native fires `onAccessTokenExpired` event → JS callback fetches new token → supplies it back to native.

##### Architectural patterns and design principles
- Bridge Pattern: clear separation between JS and native modules.
- Modularization: separate modules for playback vs. downloads vs. view management.
- Event-driven communication: using React Native’s EventEmitter for real-time updates.
- Single-responsibility modules: each native component handles one domain.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% JS Layer
    subgraph "JS/TS Layer"
        direction TB
        Index["TPStreams.initialize()"]:::js
        PlayerComp["TPStreamsPlayerView"]:::js
        DownloadComp["Download API"]:::js
        NativeComp["PlayerViewNativeComponent"]:::js
    end

    %% Bridge
    Bridge["React Native Bridge"]:::bridge

    %% Native Modules
    subgraph "Native Modules"
        direction TB
        subgraph "Android Native Module"
            direction TB
            AndroidInit["TPStreamsRNModule"]:::android
            AndroidDownload["TPStreamsDownloadModule"]:::android
            AndroidViewManager["TPStreamsRNPlayerViewManager"]:::android
            AndroidView["TPStreamsRNPlayerView"]:::android
            AndroidPackage["TPStreamsRNPackage"]:::android
            JsonUtils["JsonUtils"]:::android
        end
        subgraph "iOS Native Module"
            direction TB
            IOSModuleObjc["TPStreamsModule (.m)"]:::ios
            IOSModuleSwift["TPStreamsModule.swift"]:::ios
            IOSDownloadObjc["TPStreamsDownloadModule.mm"]:::ios
            IOSDownloadSwift["TPStreamsDownloadModule.swift"]:::ios
            IOSViewManagerObjc["TPStreamsRNPlayerViewManager.m"]:::ios
            IOSViewManagerSwift["TPStreamsRNPlayerViewManager.swift"]:::ios
            IOSView["TPStreamsRNPlayerView.swift"]:::ios
        end
    end

    %% External Services
    Backend["TPStreams Backend\n(CDN/API)"]:::cloud
    Storage["Local Storage/SQLite"]:::storage

    %% Example App
    subgraph "Example App (Demo)"
        direction TB
        Apptsx["App.tsx"]:::example
        DownloadList["DownloadList.tsx"]:::example
        IndexJs["index.js"]:::example
        AndroidExample["android/"]:::example
        IOSExample["ios/"]:::example
    end

    %% CI/CD & Packaging
    subgraph "CI/CD & Packaging"
        direction TB
        CI[".github/workflows/ci.yml"]:::config
        Publish[".github/workflows/npm-publish.yml"]:::config
        Podspec["TPStreamsRNPlayerView.podspec"]:::config
        PackageJSON["package.json"]:::config
        AndroidGradle["android/build.gradle"]:::config
        Podfile["ios/Podfile"]:::config
    end

    %% Connections JS -> Bridge -> Native
    Index -->|calls| Bridge
    PlayerComp -->|renders| Bridge
    DownloadComp -->|calls| Bridge
    NativeComp -->|binds| Bridge

    Bridge -->|invoke init/play/download| AndroidInit
    Bridge -->|invoke view| AndroidViewManager
    Bridge -->|invoke download| AndroidDownload

    Bridge -->|invoke init/play/download| IOSModuleObjc
    Bridge -->|invoke view| IOSViewManagerObjc
    Bridge -->|invoke download| IOSDownloadObjc

    %% Native -> External
    AndroidInit -->|network calls| Backend
    AndroidDownload -->|download persistence| Storage
    AndroidDownload -->|token expiry callback| Bridge
    IOSModuleObjc -->|network calls| Backend
    IOSDownloadObjc -->|download persistence| Storage
    IOSDownloadObjc -->|token expiry callback| Bridge

    %% Events back to JS
    AndroidInit -.->|events| Bridge
    AndroidDownload -.->|onDownloadProgressChanged| Bridge
    IOSModuleObjc -.->|events| Bridge
    IOSDownloadObjc -.->|onDownloadProgressChanged| Bridge

    %% Example usage
    Apptsx -->|uses| Index
    Apptsx -->|uses| PlayerComp
    DownloadList -->|uses| DownloadComp
    IndexJs -->|bootstrap| Apptsx

    %% CI/CD usage
    CI -->|runs tests| PackageJSON
    Publish -->|publishes| PackageJSON
    Podspec -->|defines CocoaPod| IOSExample
    AndroidGradle -->|build config| AndroidExample
    Podfile -->|example iOS config| IOSExample

    %% Click Events
    click Index "https://github.com/testpress/react-native-tpstreams/blob/main/src/index.tsx"
    click PlayerComp "https://github.com/testpress/react-native-tpstreams/blob/main/src/TPStreamsPlayer.tsx"
    click DownloadComp "https://github.com/testpress/react-native-tpstreams/blob/main/src/TPStreamsDownload.tsx"
    click NativeComp "https://github.com/testpress/react-native-tpstreams/blob/main/src/TPStreamsPlayerViewNativeComponent.ts"
    click AndroidInit "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/TPStreamsRNModule.kt"
    click AndroidDownload "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/TPStreamsDownloadModule.kt"
    click AndroidViewManager "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/TPStreamsRNPlayerViewManager.kt"
    click AndroidView "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/TPStreamsRNPlayerView.kt"
    click AndroidPackage "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/TPStreamsRNPackage.kt"
    click JsonUtils "https://github.com/testpress/react-native-tpstreams/blob/main/android/src/main/java/com/tpstreams/JsonUtils.kt"
    click IOSModuleObjc "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsModule.m"
    click IOSModuleSwift "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsModule.swift"
    click IOSDownloadObjc "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsDownloadModule.mm"
    click IOSDownloadSwift "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsDownloadModule.swift"
    click IOSViewManagerObjc "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsRNPlayerViewManager.m"
    click IOSViewManagerSwift "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsRNPlayerViewManager.swift"
    click IOSView "https://github.com/testpress/react-native-tpstreams/blob/main/ios/TPStreamsRNPlayerView.swift"
    click Apptsx "https://github.com/testpress/react-native-tpstreams/blob/main/example/src/App.tsx"
    click DownloadList "https://github.com/testpress/react-native-tpstreams/blob/main/example/src/DownloadList.tsx"
    click IndexJs "https://github.com/testpress/react-native-tpstreams/blob/main/example/index.js"
    click AndroidExample "https://github.com/testpress/react-native-tpstreams/tree/main/example/android/"
    click IOSExample "https://github.com/testpress/react-native-tpstreams/tree/main/example/ios/"
    click CI "https://github.com/testpress/react-native-tpstreams/blob/main/.github/workflows/ci.yml"
    click Publish "https://github.com/testpress/react-native-tpstreams/blob/main/.github/workflows/npm-publish.yml"
    click Podspec "https://github.com/testpress/react-native-tpstreams/blob/main/TPStreamsRNPlayerView.podspec"
    click PackageJSON "https://github.com/testpress/react-native-tpstreams/blob/main/package.json"
    click AndroidGradle "https://github.com/testpress/react-native-tpstreams/blob/main/android/build.gradle"
    click Podfile "https://github.com/testpress/react-native-tpstreams/tree/main/ios/Podfile"

    %% Styles
    classDef js fill:#f9f,stroke:#333,stroke-width:1px
    classDef bridge fill:#ff9,stroke:#333,stroke-width:1px
    classDef android fill:#9f9,stroke:#333,stroke-width:1px
    classDef ios fill:#99f,stroke:#333,stroke-width:1px
    classDef cloud fill:#cff,stroke:#333,stroke-width:1px
    classDef storage fill:#fcc,stroke:#333,stroke-width:1px
    classDef example fill:#fcf,stroke:#333,stroke-width:1px
    classDef config fill:#ddd,stroke:#333,stroke-width:1px
```
