//! This example demonstrates how to hot patch systems.1//!2//! It needs to be run with the dioxus CLI:3//! ```sh4//! dx serve --hot-patch --example hotpatching_systems --features hotpatching5//! ```6//!7//! All systems are automatically hot patchable.8//!9//! You can change the text in the `update_text` system, or the color in the10//! `on_click` system, and those changes will be hotpatched into the running11//! application.12//!13//! It's also possible to make any function hot patchable by wrapping it with14//! `bevy::dev_tools::hotpatch::call`.1516use std::time::Duration;1718use bevy::{color::palettes, prelude::*};1920fn main() {21let (sender, receiver) = crossbeam_channel::unbounded::<()>();2223// This function is here to demonstrate how to make something hot patchable outside of a system24// It uses a thread for simplicity but could be an async task, an asset loader, ...25start_thread(receiver);2627App::new()28.add_plugins(DefaultPlugins)29.insert_resource(TaskSender(sender))30.add_systems(Startup, setup)31.add_systems(Update, update_text)32.run();33}3435fn update_text(mut text: Single<&mut Text>) {36// Anything in the body of a system can be changed.37// Changes to this string should be immediately visible in the example.38text.0 = "before".to_string();39}4041fn on_click(42_click: On<Pointer<Click>>,43mut color: Single<&mut TextColor>,44task_sender: Res<TaskSender>,45) {46// Observers are also hot patchable.47// If you change this color and click on the text in the example, it will have the new color.48color.0 = palettes::tailwind::RED_600.into();4950let _ = task_sender.0.send(());51}5253#[derive(Resource)]54struct TaskSender(crossbeam_channel::Sender<()>);5556fn setup(mut commands: Commands) {57commands.spawn(Camera2d);5859commands60.spawn((61Node {62width: percent(100),63height: percent(100),64align_items: AlignItems::Center,65justify_content: JustifyContent::Center,66flex_direction: FlexDirection::Column,67..default()68},69children![(70Text::default(),71TextFont {72font_size: 100.0,73..default()74},75)],76))77.observe(on_click);78}7980fn start_thread(receiver: crossbeam_channel::Receiver<()>) {81std::thread::spawn(move || {82while receiver.recv().is_ok() {83let start = bevy::platform::time::Instant::now();8485// You can also make any part outside of a system hot patchable by wrapping it86// In this part, only the duration is hot patchable:87let duration = bevy::app::hotpatch::call(|| Duration::from_secs(2));8889std::thread::sleep(duration);90info!("done after {:?}", start.elapsed());91}92});93}949596