The app-bridge package provides a type-safe bridge between web games and the OpenGame App. It manages state and events across the bridge boundary, ensuring type safety and proper initialization.
graph TD
subgraph Web App
RC[React Components]
SC[Store Contexts]
WB[Web Bridge]
end
subgraph Native App
WV1[WebView 1]
WV2[WebView 2]
NB[Native Bridge]
NS[Native Stores]
end
RC --> SC
SC --> WB
WB --> WV1
WB --> WV2
WV1 --> NB
WV2 --> NB
NB --> NS
NS --> NB
NB --> WV1
NB --> WV2
WV1 --> WB
WV2 --> WB
sequenceDiagram
participant RC as React Component
participant SC as Store Context
participant WB as Web Bridge
participant WV as WebView
participant NB as Native Bridge
participant NS as Native Store
Note over WV: WebView Registration
WV->>NB: registerWebView
NB->>WV: injectJavaScript
NB->>NS: Get Initial State
NS-->>NB: Initial State
NB-->>WV: Send Initial State
Note over RC,WV: State Updates
RC->>SC: Dispatch Event
SC->>WB: dispatch
WB->>WV: postMessage
WV->>NB: onMessage
NB->>NS: Update State
NS-->>NB: State Updated
NB-->>WV: injectJavaScript
WV-->>WB: State Update
WB-->>SC: State Update
SC-->>RC: Re-render
sequenceDiagram
participant App as App
participant WV as WebView
participant NB as Native Bridge
participant NS as Native Store
App->>WV: Create WebView
WV->>NB: registerWebView
NB->>WV: Inject JavaScript
NB->>NS: Initialize Stores
NS-->>NB: Initial State
NB-->>WV: Send Initial State
WV-->>App: Ready
The bridge is implemented as a state management system that:
- Manages store initialization
- Handles bi-directional communication through WebView
- Provides type-safe event dispatch
- Maintains store state consistency
The WebView integration provides:
-
Registration
- Native app registers WebView with bridge
- Bridge injects necessary JavaScript
- Bridge sets up message handlers
-
Message Passing
- Web side sends events via
postMessage - Native side receives events via
onMessage - Native side sends state updates via
injectJavaScript
- Web side sends events via
-
State Synchronization
- Native side maintains source of truth
- State updates are sent to web via WebView
- Web side reflects state changes in UI
The React integration provides:
- Context-based store access
- Type-safe hooks for state and events
- Initialization state handling
- Support state management
The system implements a layered error handling approach:
-
Bridge Level
- Connection errors
- Communication failures
- State synchronization errors
-
Store Level
- Initialization errors
- State update failures
- Event processing errors
-
React Level
- Hook usage errors (when hooks are used outside of providers)
- Context errors
- Component rendering errors
graph TD
subgraph Test Environment
TC[Test Component]
MB[Mock Bridge]
MS1[Mock Store 1]
MS2[Mock Store 2]
EH[Event History]
end
TC --> MB
MB --> MS1
MB --> MS2
MS1 --> MB
MS2 --> MB
MB --> EH
MB --> TC
The testing architecture provides:
- Mock bridge implementation that mimics the real bridge behavior
- Individual mock stores with direct state manipulation
- Event tracking for verifying dispatched events
- State reset capabilities for test isolation
- Support for testing initialization and error scenarios
// Add example if any component name issues are found