Editor Message Dispatcher
Relevant source files
The following files were used as context for generating this wiki page:
The Editor Message Dispatcher is the central architectural hub of the Graphite editor backend. It manages a hierarchy of subsystems that communicate through sequential message passing. This system is designed to satisfy Rust's strict memory safety and mutable borrow rules by ensuring that the dispatcher is the sole owner of top-level message handlers, mutating them one at a time as it processes the message queue.
Sources: website/content/volunteer/guide/codebase-overview/editor-structure.md
Message System Architecture
The Graphite backend is organized into a tree-like hierarchy of subsystems. Each subsystem is defined by a triad of components: a Message enum, a MessageHandler struct, and a MessageContext struct. The dispatcher operates at the root of this hierarchy, maintaining a queue of messages that are processed in a flat, sequential order.
Core Components
| Component | Description |
|---|---|
| Message Enum | Defines the interface for a subsystem as enum variants, which can include data or nested child messages. |
| MessageHandler | A struct that owns persistent state and child handlers. It contains the logic for processing received messages. |
| MessageContext | A struct providing access to data from other subsystems (owned or borrowed) required during message execution. |
| Dispatcher | The root owner of top-level handlers that sequentially executes messages from the queue. |
Sources: website/content/volunteer/guide/codebase-overview/editor-structure.md
Hierarchical Communication
Subsystems are nested using the #[child] attribute on message enum variants. This attribute invokes a procedural macro that implements the From trait, allowing child messages to be automatically wrapped into the top-level Message type used by the dispatcher.
The diagram shows how specific commands like DeleteLayer are wrapped within parent message enums to reach the top-level Dispatcher queue.
Sources: website/content/volunteer/guide/codebase-overview/editor-structure.md, tools/editor-message-tree/src/main.rs
Data Flow and Execution
The message flow begins when a request is made—either from the frontend, a tool interaction, or another subsystem. Messages are added to the front or back of a queue and processed one by one.
Execution Cycle
- Enqueuing: A subsystem calls
responses.add(Message)orresponses.add_front(Message). - Dispatching: The dispatcher pulls the next message from the queue.
- Handling: The dispatcher calls the
process_messagemethod on the relevantMessageHandler. - Recursion: If the message is a child message, the parent handler forwards it to the child's
process_message. - State Mutation: The handler updates its internal state and potentially enqueues new messages for the same or different subsystems.
Sources: website/content/volunteer/guide/codebase-overview/editor-structure.md
This sequence illustrates the recursive nature of message processing through the subsystem hierarchy.
Sources: website/content/volunteer/guide/codebase-overview/editor-structure.md
Frontend/Backend Integration
The dispatcher acts as the bridge between the Rust-based Wasm backend and the TypeScript frontend.
- Frontend-to-Backend: User interactions in the Svelte GUI call the Wasm wrapper API. These wrapper functions act as entry points that enqueue messages for the backend dispatcher.
- Backend-to-Frontend: The dispatcher processes
FrontendMessagevariants. These are mapped to JavaScript data structures in the frontend'smessages.ts. TheSubscriptions Routerin the frontend then delivers these to registered TS callbacks.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
Debugging the Dispatcher
The sequential nature of the message system allows for detailed introspection of application activity.
- Message Logging: Developers can activate message logs via Help > Debug: Print Messages. Options include "Only Names" for a high-level flow or "Full Contents" to see the data payloads within each message.
- Trace Logs: The system supports
log::trace!()for verbose internal activity, which can be toggled in the editor's debug menu. - Message Tree Tool: A dedicated tool (
editor-message-tree) generates a hierarchical outline of the entire message system, showing the relationship between Messages, Handlers, and Contexts.
Sources: website/content/volunteer/guide/codebase-overview/debugging-tips.md, tools/editor-message-tree/src/main.rs
Summary
The Editor Message Dispatcher provides a structured, predictable way to manage complex application state in Rust. By flattening function-like calls into a sequential queue, it avoids the complexities of nested mutable borrows while providing a unified communication bus for both internal backend subsystems and the external frontend interface.