Skip to content

Commit

Permalink
Fix infinite render cycle (#53)
Browse files Browse the repository at this point in the history
* add sequence diagram about rendering cycle in docs

* rerender only on user triggered event

* fix render mode
  • Loading branch information
JunichiSugiura authored Aug 27, 2022
1 parent 8b76a03 commit eab4780
Show file tree
Hide file tree
Showing 28 changed files with 816 additions and 1,004 deletions.
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ members = [
]

[[example]]
name = "counter-channel"
path = "examples/counter/channel.rs"

[[example]]
name = "counter-global-state-ecs"
name = "counter"
path = "examples/counter/global-state-ecs.rs"

[[example]]
Expand All @@ -50,8 +46,8 @@ name = "window-settings"
path = "examples/window/settings.rs"

[[example]]
name = "update-mode"
path = "examples/window/update-mode.rs"
name = "render-mode"
path = "examples/window/render-mode.rs"

[[example]]
name = "multiple-windows"
Expand Down
63 changes: 0 additions & 63 deletions examples/channel.rs

This file was deleted.

107 changes: 0 additions & 107 deletions examples/counter/channel.rs

This file was deleted.

66 changes: 16 additions & 50 deletions examples/counter/global-state-ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn main() {
.add_plugin(LogPlugin)
.add_plugin(GlobalStatePlugin)
.add_plugin(DioxusPlugin::<GlobalState, CoreCommand, ()>::new(Root))
.add_event::<UpdateGlobalState>()
.add_startup_system(setup)
.add_plugin(GlobalStatePlugin)
.init_resource::<Count>()
.add_system(handle_core_cmd)
.add_system(update_global_state)
.run();
Expand All @@ -20,16 +20,9 @@ struct GlobalState {
disabled: bool,
}

#[derive(Component, Clone, Debug, Default)]
struct Count(u32);

#[derive(Component, Clone, Debug)]
struct Disabled(bool);

impl Default for Disabled {
fn default() -> Self {
Self(true)
}
#[derive(Clone, Debug, Default)]
struct Count {
value: u32,
}

#[derive(Clone, Debug)]
Expand All @@ -39,68 +32,41 @@ enum CoreCommand {
Reset,
}

struct UpdateGlobalState;

fn setup(mut commands: Commands, mut update_global_state: EventWriter<UpdateGlobalState>) {
info!("🧠 Spawn count");
commands
.spawn()
.insert(Count::default())
.insert(Disabled::default());

update_global_state.send(UpdateGlobalState);
}

fn handle_core_cmd(
mut events: EventReader<CoreCommand>,
mut query: Query<(&mut Count, &mut Disabled)>,
mut update_global_state: EventWriter<UpdateGlobalState>,
) {
fn handle_core_cmd(mut events: EventReader<CoreCommand>, mut count: ResMut<Count>) {
for cmd in events.iter() {
let (mut count, mut disabled) = query.single_mut();
match cmd {
CoreCommand::Increment => {
info!("🧠 Increment");
count.0 += 1;
count.value += 1;
}
CoreCommand::Decrement => {
if count.0 > 0 {
if count.value > 0 {
info!("🧠 Decrement");
count.0 -= 1;
count.value -= 1;
}
}
CoreCommand::Reset => {
if count.0 != 0 {
if count.value != 0 {
info!("🧠 Reset");
count.0 = 0;
count.value = 0;
}
}
};
disabled.0 = count.0 == 0;

update_global_state.send(UpdateGlobalState);
}
}

fn update_global_state(
mut events: EventReader<UpdateGlobalState>,
query: Query<(&Count, &Disabled)>,
mut global_state: EventWriter<GlobalState>,
) {
for _ in events.iter() {
let (count, disabled) = query.single();

global_state.send(GlobalState::Count(count.0));
global_state.send(GlobalState::Disabled(disabled.0));
fn update_global_state(count: Res<Count>, mut global_state: EventWriter<GlobalState>) {
if count.is_changed() {
global_state.send(GlobalState::Count(count.value));
}
}

#[allow(non_snake_case)]
fn Root(cx: Scope) -> Element {
let count = use_read(&cx, COUNT);
let disabled = use_read(&cx, DISABLED);
let disabled = *count == 0;

let window = use_window::<CoreCommand, ()>(&cx);
let window = use_window::<CoreCommand>(&cx);

cx.render(rsx! {
h1 { "Counter Example" }
Expand Down
Loading

1 comment on commit eab4780

@vercel
Copy link

@vercel vercel bot commented on eab4780 Aug 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.