Skip to content

Type-Safe and Unified RPC Communication#3

Merged
JumpLink merged 18 commits intomainfrom
refactor/rpc-communication
Aug 31, 2025
Merged

Type-Safe and Unified RPC Communication#3
JumpLink merged 18 commits intomainfrom
refactor/rpc-communication

Conversation

@JumpLink
Copy link
Contributor

@JumpLink JumpLink commented Aug 30, 2025

This PR improves RPC communication across the entire project by enhancing type safety and unifying implementations across all platform packages.

🎯 Motivation

The previous RPC communication had several issues:

  • Inconsistent type definitions across different packages
  • Lack of type safety in message handling
  • Redundant implementations
  • Weak error handling
  • Unclear interfaces between core and platform-specific implementations

🔧 Key Changes

Core Package Improvements (message-channel-core)

New Type Definitions:

  • RpcEngineType enum for unified message types
  • RpcEngineParamMap for type-safe parameter mapping
  • EngineRpcRegistry interface for registry typing

New Utility Functions:

  • Type guards for runtime type checking (isRpcRequest, isRpcResponse)
  • Serialization/deserialization for secure message transmission
  • Standardized error handling with createRpcError

Platform-Specific Updates

GJS Package (message-channel-gjs):

  • RpcEndpoint as generic class with RpcMethodRegistry
  • WebKit integration for GTK WebViews
  • Type-safe handler registration

Web Package (message-channel-web):

  • Iframe context handling
  • PostMessage API with type safety
  • Bidirectional communication

WebView Package (message-channel-webview):

  • WebKit message handler integration
  • Native bridge for Electron/WebView

Engine Integration

Excalibur Engine:

  • RpcEngineType integration instead of old EngineMessageType
  • Type-safe event dispatcher
  • Improved error handling

GJS Engine:

  • Unified RPC handlers
  • Type-safe parameter validation

📁 Affected Files

Core Packages

  • packages/message-channel-core/src/types/rpc.ts - New RPC types
  • packages/message-channel-core/src/utils/serialization.ts - Serialization
  • packages/message-channel-core/src/utils/validation.ts - Type guards

Platform Packages

  • packages/message-channel-gjs/src/rpc-endpoint.ts - GJS RPC endpoint
  • packages/message-channel-web/src/iframe-context.ts - Web iframe context
  • packages/message-channel-webview/src/webkit-message-handler.ts - WebView handler

Engine Packages

  • packages/engine-excalibur/src/systems/editor-input.system.ts - Input system
  • packages/engine-gjs/src/widgets/webview.ts - GJS WebView widget

⚠️ Breaking Changes

API Changes

  • EngineMessageTypeRpcEngineType (enum values remain the same)
  • RpcEndpoint is now generic with RpcMethodRegistry<T>
  • Message handler signatures have changed

Migration Guide

// Before
const endpoint = new RpcEndpoint();
endpoint.registerHandler('loadProject', (params: any) => { ... });

// After
const endpoint = new RpcEndpoint<EngineRpcRegistry>();
endpoint.registerHandler(RpcEngineType.LOAD_PROJECT, (params: LoadProjectParams) => { ... });

- Update all instances of EngineMessageType to RpcEngineType across the codebase for consistency in RPC communication.
- Remove the deprecated engine-message.ts file and its associated types.
- Refactor event handling in various components to utilize the new RpcEngineType for commands and events.
- Ensure type safety and clarity in RPC method registrations and event emissions.

This change enhances the overall structure of the engine's messaging system, aligning it with the new RPC framework.
- Update RPC event handlers in the engine to utilize RpcEngineType for better type safety and clarity.
- Replace generic event handling with specific types for notify-engine-event and handle-input-event.
- Ensure proper error handling for undefined events in the registration of handlers.
- Refactor WebView and engine components to align with the new type-safe RPC structure.

These changes improve the robustness of the messaging system and enhance maintainability across the codebase.
- Update RPC method calls in the engine to use RpcEngineType for improved clarity and type safety.
- Refactor loadProject and input event handling to align with the new RPC structure.
- Enhance maintainability by ensuring consistent usage of RPC types across the codebase.

These changes contribute to a more robust and understandable messaging system within the engine.
…nication

- Introduce RpcEngineParamMap for improved type safety in event handling and RPC method calls.
- Refactor engine components to utilize the new type-safe RPC structure, ensuring consistent usage across the codebase.
- Update message handling in WebView and GJS to align with the enhanced RPC framework.
- Clean up deprecated types and streamline the messaging system for better maintainability.

These changes contribute to a more robust and understandable messaging system within the engine, enhancing clarity and reducing potential errors.
…ation

- Enhance RpcEngineParamMap to exclude START and STOP types for better clarity in event data.
- Refactor EngineRpcRegistry to define specific parameters and responses for each RPC method, improving type safety.
- Introduce a new RpcErrorCode enum for standardized error handling in RPC responses.
- Update message conversion utilities to utilize the new error handling structure, ensuring consistent error reporting.

These changes contribute to a more robust and maintainable RPC communication framework within the engine.
…e readability

- Consolidate RPC handler registration for loadProject, loadMap, start, and stop methods to enhance clarity.
- Rename setupEventListeners to registerRpcHandlers for better alignment with functionality.
- Remove unnecessary type annotations for parameters in handler functions to simplify the code.

These changes contribute to a cleaner and more maintainable engine implementation.
- Update maker-web to use EngineRpcRegistry for proper typing
- Translate German comments to English in maker-web app
- Remove any types and use proper TypeScript types
- Enhance error handling with robust type checking
- Update all README.md files with comprehensive documentation
- Improve project path configuration for better development setup
- Ensure consistent typing patterns across all implementations
- Change iframe source URL from localhost:4173 to localhost:5000 for game-iframe.
- Update project path from localhost:3000 to localhost:5001 for game-project.json.

These adjustments ensure the application points to the correct local development servers.
…ssues

- Fix TypeScript spread operator errors in message conversion
- Align RPC response validation with type definitions
- Add standardized message serialization with undefined handling
- Update package exports for serialization utilities
- Ensure consistent behavior across all platforms
…s platforms

- Update all RPC endpoints to use standardized serialization
- Remove debug logging infrastructure from endpoints
- Ensure consistent message handling across GJS, Web, and WebView platforms
- Improve error handling with transmission error utilities
- Fix unhandled promise rejection in ProjectView event handler
- Remove debug logging from engine methods
- Simplify parallel project loading logic
- Ensure graceful handling of timing differences in parallel loading
- Fix TypeScript return type inconsistencies in engine methods
@JumpLink JumpLink changed the title refactor RPC communication Type-Safe and Unified RPC Communication Aug 31, 2025
@JumpLink JumpLink merged commit a1e48bb into main Aug 31, 2025
1 check passed
@JumpLink JumpLink deleted the refactor/rpc-communication branch August 31, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant