Frontend State Management
Relevant source files
The following files were used as context for generating this wiki page:
The Graphite frontend is built as a reactive web application using the Svelte framework, serving as the presentation layer for the editor. Its primary purpose is to display the Graphical User Interface (GUI) based on state received from the Rust-based backend and to provide interactive widgets that send user updates back to that backend. While the frontend maintains reactive state for UI components, the backend (compiled to WebAssembly) remains the ultimate source of truth for application state.
Sources: frontend/README.md:3-8, website/content/volunteer/guide/codebase-overview/_index.md:27-33
Architecture Overview
The frontend state management architecture is designed to be lightweight and minimal. It follows a delegation pattern where user inputs are captured by Svelte components and quickly handed off to the WebAssembly (Wasm) editor backend. The backend processes the business logic and responds by sending a queue of messages back to the frontend to update the UI state.
Core Architectural Components
| Component | Responsibility |
|---|---|
| Svelte Components | UI presentation and capturing user interactions. |
| Stores | Reactive TypeScript files providing state to Svelte components via a subscribe contract. |
| Managers | Self-contained systems managing browser APIs and linking them to the backend. |
| Subscriptions Router | A central dispatcher that routes backend messages to registered frontend subscribers. |
| Editor Wrapper | A Rust translation layer providing a JS-compatible API for the frontend to call backend functions. |
Sources: frontend/src/README.md:3-33, website/content/volunteer/guide/codebase-overview/_index.md:27-33, website/content/volunteer/guide/codebase-overview/_index.md:58-62
Data Flow and Communication
Communication between the frontend state and the backend engine is asynchronous and message-based. The frontend utilizes a Subscriptions Router to handle incoming updates from the backend.
Frontend-to-Backend
The frontend calls autogenerated TypeScript functions provided by wasm-bindgen. These functions act as the entry point into the Wasm binary, triggering business logic within the editor's Rust-based message system.
Backend-to-Frontend
The backend queues FrontendMessage objects, which are mapped to JavaScript data structures. The frontend message dispatcher then executes callbacks for any code that has subscribed to a specific message type using subscribeFrontendMessage.
The following diagram illustrates the lifecycle of a state update triggered by a user action:
The diagram shows the circular flow from user input to backend processing and the subsequent reactive UI update.
Sources: website/content/volunteer/guide/codebase-overview/_index.md:58-75, frontend/src/README.md:46-56
Reactive State with Stores
Stores in Graphite are TypeScript modules that persist Svelte writable stores. They are designed to survive Hot Module Replacement (HMR) by storing data in import.meta.hot.data.
- Initialization: Stores are created synchronously during the initialization of
Editor.svelte. - Accessibility: They are made available to the entire component tree using Svelte's
setContext()and accessed viagetContext(). - Reactivity: Stores export a function that returns an object with a
subscribemethod, adhering to the Svelte store contract. - Persistence: Unlike Managers, stores do not replace themselves during HMR; instead, the
Editor.sveltecomponent is remounted to refresh them.
Sources: frontend/src/README.md:18-27
Managers vs. Stores
Graphite makes a functional distinction between "Stores" and "Managers" for handling frontend logic and state.
| Feature | Stores | Managers |
|---|---|---|
| Purpose | Expose reactive state to Svelte components. | Manage browser APIs and self-contained systems. |
| Access | Accessed via getContext(). | Not accessed by Svelte components directly. |
| HMR Behavior | Survived via import.meta.hot.data; triggered remount. | Replace themselves during HMR if modified. |
| Lifetime | Tied to the Editor.svelte lifecycle. | Self-contained for the application lifetime. |
Sources: frontend/src/README.md:9-29, frontend/src/README.md:31-33
Tooling and Configuration
The frontend state management and development workflow are supported by a Vite-based build system.
- Vite: Used as the bundler and dev server, handling HMR for both frontend and backend (via file watchers).
- TypeScript: Provides type safety for state structures and message definitions.
- ESLint/Prettier: Enforces code quality and consistent style for JS, TS, and Svelte files.
- Wasm-Pack: Compiles the Rust backend and generates the TypeScript bindings used by the frontend to communicate with the state engine.
Sources: frontend/README.md:19-45, frontend/package.json:11-25, website/content/volunteer/guide/project-setup/_index.md:24-30
Conclusion
Frontend state management in Graphite is a bridge between reactive Svelte components and a high-performance Rust backend. By utilizing a central message router and separating reactive stores from system managers, the architecture maintains a thin, efficient frontend while ensuring that the backend remains the authoritative source for complex editor logic and document state.