Skip to main content

bevy_react/
message.rs

1//! Typed app messages emitted by the React app for the Bevy world to consume.
2//!
3//! This is the complement to the UI bridge: where ops describe UI mutations, an
4//! app message carries an app-level signal (e.g. "set the count") from React
5//! into the ECS. The JS side calls `emit(name, value)`; the plugin owns a single
6//! consumption point that routes each message by name to the typed payload the
7//! user registered with [`ReactAppExt::add_react_handler`], deserializing the
8//! JSON for them and triggering it for [observers](bevy::ecs::observer) to handle.
9//!
10//! ```ignore
11//! use bevy::prelude::*;
12//! use bevy_react::{react_message, ReactAppExt};
13//!
14//! #[react_message]
15//! struct Count(usize); // name defaults to "count"
16//!
17//! app.add_react_handler(|on: On<Count>| {
18//!     let n = on.event().0; // typed — no serde_json::Value juggling
19//! });
20//! ```
21
22use std::any::TypeId;
23use std::collections::HashMap;
24use std::path::Path;
25
26use bevy::ecs::system::IntoObserverSystem;
27use bevy::prelude::*;
28use serde::de::DeserializeOwned;
29use ts_rs::TS;
30
31use crate::event::{ReactEvent, ReactEventRegistry};
32use crate::filters::{FilterRegistry, ReactFilter};
33use crate::registry::{NamedEntry, register_entry};
34use crate::request::{ReactRequest, ReactRequestRegistry, RequestEvent};
35use crate::ts_codegen::TsCollector;
36
37/// A named, JSON-valued signal sent from the React app to Bevy.
38///
39/// This is the raw wire form carried across the JS↔Bevy channel. Consumers
40/// don't read it directly: register a typed [`ReactPayload`] and observe that
41/// instead. The plugin deserializes the [`value`](ReactMessage::value) into the
42/// payload type whose [`ReactPayload::NAME`] matches [`name`](ReactMessage::name).
43#[derive(Clone, Debug)]
44pub struct ReactMessage {
45    /// Application-defined message name (the first argument to `emit`).
46    pub name: String,
47    /// The payload (the second argument to `emit`), as JSON.
48    pub value: serde_json::Value,
49}
50
51/// A typed payload a React `emit(NAME, value)` call deserializes into.
52///
53/// Usually you don't implement this by hand — apply [`#[react_message]`](crate::react_message),
54/// which derives `Deserialize` and `TS` and implements both `Event` and this trait. The
55/// JSON `value` is deserialized straight into `Self`, so the payload's shape must
56/// match what JS emits: `emit("count", 5)` needs a payload that deserializes from
57/// a number (e.g. `struct Count(usize)`), while `emit("move", { x, y })` needs a struct.
58///
59/// The [`TS`] bound lets [`ReactAppExt::export_react_typescript`] mirror the payload's
60/// shape into a TypeScript type, so the JS `emit` is type-checked against the same struct.
61pub trait ReactPayload: Event + DeserializeOwned + TS + Send + Sync + 'static {
62    /// The `emit` name this type is routed from.
63    const NAME: &'static str;
64}
65
66/// Type-erased deserialize-and-trigger closures keyed by `emit` name. Owned by
67/// the plugin; the single dispatch system looks up each incoming message here.
68/// The [`TypeId`] lets us treat re-registering the *same* payload (e.g. attaching
69/// several observers via [`ReactAppExt::add_react_handler`]) as a harmless no-op,
70/// while still warning when two *different* types claim one name.
71#[derive(Resource, Default)]
72pub(crate) struct ReactRegistry {
73    pub(crate) handlers: HashMap<&'static str, Registration>,
74}
75
76/// What we record per registered payload: the dispatch closure plus the TypeScript
77/// metadata [`ReactAppExt::export_react_typescript`] needs to mirror the type.
78pub(crate) struct Registration {
79    /// Distinguishes re-registering the same type (a no-op) from a name collision.
80    type_id: TypeId,
81    /// Deserialize-and-trigger for this payload.
82    handler: Handler,
83    /// The payload's TypeScript reference name (e.g. `Count`), used in the message map.
84    pub(crate) ts_name: fn() -> String,
85    /// Records this payload's declaration and all its transitive dependencies.
86    pub(crate) ts_collect: fn(&mut TsCollector),
87}
88
89/// Deserializes a JSON payload and queues a trigger for it, or returns the serde
90/// error if the JSON doesn't match the registered payload type.
91type Handler =
92    Box<dyn Fn(serde_json::Value, &mut Commands) -> Result<(), serde_json::Error> + Send + Sync>;
93
94impl NamedEntry for Registration {
95    fn type_id(&self) -> TypeId {
96        self.type_id
97    }
98}
99
100impl ReactRegistry {
101    /// Register the deserialize-and-trigger handler for payload `T`. Idempotent
102    /// for a given type; warns only if a different type already owns `T::NAME`.
103    pub(crate) fn register<T>(&mut self)
104    where
105        T: ReactPayload,
106        for<'a> <T as Event>::Trigger<'a>: Default,
107    {
108        register_entry(
109            &mut self.handlers,
110            T::NAME,
111            "message",
112            Registration {
113                type_id: TypeId::of::<T>(),
114                handler: Box::new(|value, commands| {
115                    // `T` is concrete here, so serde and the trigger are baked in.
116                    let payload: T = serde_json::from_value(value)?;
117                    commands.trigger(payload);
118                    Ok(())
119                }),
120                // `T` is concrete here too, so its TS shape is baked into these fns.
121                ts_name: T::name,
122                ts_collect: |c| c.add::<T>(),
123            },
124        );
125    }
126
127    /// Route one message: deserialize into its registered payload and trigger it.
128    /// Logs a warning for an unregistered name and an error for malformed JSON.
129    pub(crate) fn dispatch(&self, msg: ReactMessage, commands: &mut Commands) {
130        match self.handlers.get(msg.name.as_str()) {
131            None => warn!("no handler registered for react message {:?}", msg.name),
132            Some(reg) => {
133                if let Err(e) = (reg.handler)(msg.value, commands) {
134                    error!("malformed react message {:?}: {e}", msg.name);
135                }
136            }
137        }
138    }
139}
140
141/// Registers typed React message payloads on a Bevy [`App`].
142pub trait ReactAppExt {
143    /// Register a typed React message payload without attaching an observer.
144    ///
145    /// After this, an `emit(T::NAME, value)` from the React app deserializes
146    /// `value` into `T` and triggers it. Prefer [`add_react_handler`](Self::add_react_handler)
147    /// unless you want to register the type and observe it separately.
148    fn add_react_message<T>(&mut self) -> &mut Self
149    where
150        T: ReactPayload,
151        for<'a> <T as Event>::Trigger<'a>: Default;
152
153    /// Register a payload and attach an observer for it in one call.
154    ///
155    /// The payload type is inferred from the observer's `On<T>` parameter, so you
156    /// never name it twice. Call it again with another observer to add more
157    /// handlers for the same message — registration is idempotent.
158    ///
159    /// ```ignore
160    /// app.add_react_handler(|count: On<Count>, mut desired: ResMut<DesiredCubes>| {
161    ///     desired.0 = count.event().0;
162    /// });
163    /// ```
164    fn add_react_handler<E, B, M, S>(&mut self, observer: S) -> &mut Self
165    where
166        E: ReactPayload,
167        for<'a> <E as Event>::Trigger<'a>: Default,
168        B: Bundle,
169        S: IntoObserverSystem<E, B, M>;
170
171    /// Register a typed React request without attaching an observer. Prefer
172    /// [`add_react_request_handler`](Self::add_react_request_handler).
173    fn add_react_request<T>(&mut self) -> &mut Self
174    where
175        T: ReactRequest;
176
177    /// Register a request and attach its observer in one call.
178    ///
179    /// The request type is inferred from the observer's `On<Request<T>>` parameter.
180    /// The observer answers the request via [`Request::respond`](crate::Request::respond).
181    ///
182    /// ```ignore
183    /// app.add_react_request_handler(|req: On<Request<BoardGet>>, board: Res<Board>| {
184    ///     req.respond(board.clone());
185    /// });
186    /// ```
187    fn add_react_request_handler<E, B, M, S>(&mut self, observer: S) -> &mut Self
188    where
189        E: Event + RequestEvent,
190        for<'a> <E as Event>::Trigger<'a>: Default,
191        B: Bundle,
192        S: IntoObserverSystem<E, B, M>;
193
194    /// Register a Bevy → React event type so it appears in the generated
195    /// `ReactEvents` map and `bevy.on` typing. Sending an event with
196    /// [`ReactEvents`](crate::ReactEvents) does not require this, but then the type
197    /// won't be known to the exporter.
198    fn add_react_event<E>(&mut self) -> &mut Self
199    where
200        E: ReactEvent;
201
202    /// Register a custom filter type (usually a
203    /// [`#[react_filter]`](crate::react_filter) struct) so the `filter` style
204    /// chain can resolve it by `T::NAME` — and so the exporter can mirror its
205    /// params type into the generated TypeScript.
206    ///
207    /// The built-in filters (`blur`, `brightness`, …) are registered
208    /// automatically by [`ReactUiPlugin`](crate::ReactUiPlugin); call this
209    /// only for your own filters. Like events, keep the call in your single
210    /// `register_bindings` site so the exporter path sees the same filters
211    /// the running app does — a filter registered only at runtime never
212    /// appears in the generated typing (see
213    /// [`export_react_typescript`](Self::export_react_typescript)).
214    ///
215    /// To shadow a built-in name, register **after** `ReactUiPlugin` is
216    /// added: the plugin's `build` registers the built-ins and would replace
217    /// an earlier custom (with a warn), while the exporter — which never
218    /// runs the plugin — would still show yours, silently diverging the
219    /// generated types from runtime.
220    fn add_react_filter<T>(&mut self) -> &mut Self
221    where
222        T: ReactFilter + DeserializeOwned + TS;
223
224    /// Write a self-contained TypeScript module (conventionally `src/bevy.ts`)
225    /// mirroring every registered React binding to `path`.
226    ///
227    /// The generated module covers all four typed surfaces in one pass: a type
228    /// declaration per payload (mirrored from the `#[react_message]` /
229    /// `#[react_request]` / `#[react_event]` structs and the filter params types
230    /// via `ts-rs`), the `ReactMessages`/`ReactRequests`/`ReactEvents` name→type
231    /// maps, a `declare module "bevy-react"` block augmenting the `BevyFilters`
232    /// interface with every registered filter (built-ins included, so the
233    /// `filter` style field types each name's params), typed `emit`/`request`/
234    /// `on` wrappers, and a structured `bevy` proxy whose nested methods come
235    /// from dotted request names (`"board.get"` → `bevy.board.get()`).
236    /// App code imports that typed surface from `./bevy` instead of the untyped
237    /// functions from `"bevy-react"`, so every call is checked against the same
238    /// structs Bevy serializes and deserializes.
239    ///
240    /// Keep a **single registration site**: put your `add_react_*` calls in a
241    /// `register_bindings(app)` function that both the real app (e.g. your
242    /// plugin's `build`) and a small exporter entry point call, so a binding can
243    /// never exist at runtime without appearing in the generated types. Wire the
244    /// exporter to a CLI flag that returns before `app.run()`, commit the output,
245    /// and have CI regenerate + `git diff --exit-code` to guarantee the
246    /// TypeScript never drifts from Rust. (See `examples/demos/main.rs` and its
247    /// `--export-bindings` flag, exposed as `npm run bevy:generate`.)
248    ///
249    /// ```ignore
250    /// if std::env::args().nth(1).as_deref() == Some("--export-bindings") {
251    ///     let path = std::env::args().nth(2).expect("output path");
252    ///     let mut app = App::new();
253    ///     register_bindings(&mut app); // the same fn the real app calls
254    ///     app.export_react_typescript(&path)?;
255    ///     return;
256    /// }
257    /// ```
258    fn export_react_typescript(&self, path: impl AsRef<Path>) -> std::io::Result<()>;
259}
260
261impl ReactAppExt for App {
262    fn add_react_message<T>(&mut self) -> &mut Self
263    where
264        T: ReactPayload,
265        for<'a> <T as Event>::Trigger<'a>: Default,
266    {
267        self.world_mut()
268            .get_resource_or_init::<ReactRegistry>()
269            .register::<T>();
270        self
271    }
272
273    fn add_react_handler<E, B, M, S>(&mut self, observer: S) -> &mut Self
274    where
275        E: ReactPayload,
276        for<'a> <E as Event>::Trigger<'a>: Default,
277        B: Bundle,
278        S: IntoObserverSystem<E, B, M>,
279    {
280        self.add_react_message::<E>();
281        self.add_observer(observer);
282        self
283    }
284
285    fn add_react_request<T>(&mut self) -> &mut Self
286    where
287        T: ReactRequest,
288    {
289        self.world_mut()
290            .get_resource_or_init::<ReactRequestRegistry>()
291            .register::<T>();
292        self
293    }
294
295    fn add_react_request_handler<E, B, M, S>(&mut self, observer: S) -> &mut Self
296    where
297        E: Event + RequestEvent,
298        for<'a> <E as Event>::Trigger<'a>: Default,
299        B: Bundle,
300        S: IntoObserverSystem<E, B, M>,
301    {
302        // `E` is `Request<T>`; register the underlying request type `T`.
303        self.add_react_request::<E::Req>();
304        self.add_observer(observer);
305        self
306    }
307
308    fn add_react_event<E>(&mut self) -> &mut Self
309    where
310        E: ReactEvent,
311    {
312        self.world_mut()
313            .get_resource_or_init::<ReactEventRegistry>()
314            .register::<E>();
315        self
316    }
317
318    fn add_react_filter<T>(&mut self) -> &mut Self
319    where
320        T: ReactFilter + DeserializeOwned + TS,
321    {
322        self.world_mut()
323            .get_resource_or_init::<FilterRegistry>()
324            .register::<T>();
325        self
326    }
327
328    fn export_react_typescript(&self, path: impl AsRef<Path>) -> std::io::Result<()> {
329        crate::ts_codegen::export(self.world(), path.as_ref())
330    }
331}
332
333#[cfg(test)]
334mod tests {
335    use super::*;
336    use crate::react_message;
337    use bevy::ecs::world::CommandQueue;
338
339    #[react_message]
340    struct Count(usize);
341
342    // Only used to assert their derived `NAME`, so their fields go unread.
343    #[react_message(name = "hp")]
344    #[allow(dead_code)]
345    struct Health(u32);
346
347    #[react_message]
348    #[allow(dead_code)]
349    struct PlayerScore(i64);
350
351    #[derive(Resource, Default)]
352    struct LastCount(usize);
353
354    /// The macro defaults the name to the struct ident, first letter lowered, and
355    /// honours an explicit override.
356    #[test]
357    fn derives_emit_name() {
358        assert_eq!(Count::NAME, "count");
359        assert_eq!(PlayerScore::NAME, "playerScore");
360        assert_eq!(Health::NAME, "hp");
361    }
362
363    fn test_app() -> App {
364        let mut app = App::new();
365        app.init_resource::<LastCount>();
366        // Single call registers the deserializer and attaches the observer.
367        app.add_react_handler(|on: On<Count>, mut last: ResMut<LastCount>| last.0 = on.event().0);
368        app
369    }
370
371    /// Run one message through the plugin's dispatch path, applying the trigger
372    /// it queues so observers run before we assert.
373    fn dispatch(app: &mut App, msg: ReactMessage) {
374        app.world_mut()
375            .resource_scope(|world, registry: Mut<ReactRegistry>| {
376                let mut queue = CommandQueue::default();
377                let mut commands = Commands::new(&mut queue, world);
378                registry.dispatch(msg, &mut commands);
379                queue.apply(world);
380            });
381    }
382
383    /// A registered payload deserializes and reaches its observer.
384    #[test]
385    fn dispatches_to_observer() {
386        let mut app = test_app();
387        dispatch(
388            &mut app,
389            ReactMessage {
390                name: "count".into(),
391                value: serde_json::json!(3),
392            },
393        );
394        assert_eq!(app.world().resource::<LastCount>().0, 3);
395    }
396
397    /// An unknown name and malformed JSON are tolerated (logged, not panicked).
398    #[test]
399    fn tolerates_unknown_and_malformed() {
400        let mut app = test_app();
401        dispatch(
402            &mut app,
403            ReactMessage {
404                name: "nope".into(),
405                value: serde_json::json!(1),
406            },
407        );
408        dispatch(
409            &mut app,
410            ReactMessage {
411                name: "count".into(),
412                value: serde_json::json!("not a number"),
413            },
414        );
415        // Neither message should have reached the observer.
416        assert_eq!(app.world().resource::<LastCount>().0, 0);
417    }
418}