Frontend-Backend IPC
Relevant source files
The following files were used as context for generating this wiki page:
The Frontend-Backend Inter-Process Communication (IPC) system in Graphite facilitates communication between the TypeScript-based user interface and the Rust-based editor engine. Because the core business logic resides in a WebAssembly (Wasm) binary, this system acts as a bridge to synchronize state, handle user input, and dispatch UI updates.
The IPC architecture is divided into two primary flows: synchronous calls from the frontend into the backend via a thin Rust translation layer, and asynchronous message dispatching from the backend to the frontend using a subscription-based router. This separation ensures that the frontend remains a lightweight presentation layer while the backend maintains the "source of truth" for the application state.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/README.md
IPC Architecture Overview
The communication system relies on wasm-bindgen to bridge the gap between JavaScript/TypeScript data types and Rust. The frontend initiates actions by calling exported Wasm functions, while the backend responds by queuing FrontendMessage objects that are then dispatched to specific UI components.
Bidirectional Communication Flow
| Direction | Mechanism | Implementation Detail |
|---|---|---|
| Frontend → Backend | Wasm Wrapper API | Calls into editor_wrapper.rs functions bound via wasm-bindgen. |
| Backend → Frontend | Message Dispatcher | Queues FrontendMessage variants mapped to JS data structures. |
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
The following diagram illustrates the high-level loop between the Svelte frontend and the Wasm editor backend:
The diagram shows how user input flows from the UI into the Rust backend and how the resulting state changes are routed back to the UI through a subscription system.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
Frontend to Backend: The Wrapper API
The frontend communicates with the backend through a translation layer located in /frontend/wrapper/src/editor_wrapper.rs. This file wraps the editor's internal message system and provides TypeScript-compatible entry points.
Key Components
- Wasm Bindings: Generated by
wasm-bindgen, these allow TS to call Rust functions directly. - Editor Instance: The wrapper maintains the state of the editor, allowing the frontend to trigger logic like tool selection, document manipulation, or node graph updates.
- Input Handling: Managers in the frontend (e.g., input managers) capture browser events and transform them into calls to the
EditorWrapper.
Sources: frontend/src/README.md, website/content/volunteer/guide/codebase-overview/_index.md
Backend to Frontend: Message Dispatching
Backend-to-frontend communication is asynchronous and driven by a queue of FrontendMessage objects. Instead of returning values directly from every function call, the backend queues messages that describe UI updates.
The Subscriptions Router
The subscriptions-router.ts is the central hub for handling messages arriving from the Wasm module. It provides a mechanism for different parts of the frontend to listen for specific message types.
subscribeFrontendMessage(type, callback): Used by Svelte stores and managers to register as the exclusive handler for a specific message.handleFrontendMessage(type, data): Called by theEditorWrapperwhen the backend dispatches the message queue.
Sources: frontend/src/README.md, website/content/volunteer/guide/codebase-overview/_index.md
Sequence of a Message Update
When an action occurs in the backend (e.g., a layer is added), the following sequence occurs:
This sequence ensures that a single backend action can trigger multiple UI updates by dispatching a batch of messages through the router.
Sources: frontend/src/README.md, website/content/volunteer/guide/codebase-overview/_index.md
Data Mapping and Serialization
Messages are defined as Enums in Rust (FrontendMessage). During IPC, these are mapped to JavaScript objects.
Implementation Details
- Location: Rust definitions are in
/editor/src/messages/frontend/frontend_message.rs, while JS mappings are in/frontend/src/messages.ts. - Complex Types: The
EditorWrapperis specifically designed to handle types that are normally incompatible between Rust and JS, providing a "JS-centric API." - Reactive State: Svelte stores subscribe to these messages to provide reactive data to the UI components.
Sources: frontend/README.md, frontend/src/README.md, website/content/volunteer/guide/codebase-overview/_index.md
Summary
The Frontend-Backend IPC system in Graphite uses a "Thin Wrapper" pattern for entering the backend and a "Message Router" pattern for updating the frontend. By utilizing wasm-bindgen and a centralized subscription router, the project maintains a strict separation of concerns: the TypeScript frontend handles presentation and browser APIs, while the Rust backend handles the complex geometric and document logic.
Sources: frontend/src/README.md, website/content/volunteer/guide/codebase-overview/_index.md