From 4f2962d73f3bdeeca8a11817e404c45e91e2c2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 12 Sep 2020 20:34:58 +0200 Subject: [PATCH] Move `version` from `Grid` to `GameOfLife` struct --- examples/game_of_life/src/main.rs | 35 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 332d2d95..3f087f88 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -31,11 +31,12 @@ struct GameOfLife { queued_ticks: usize, speed: usize, next_speed: Option, + version: usize, } #[derive(Debug, Clone)] enum Message { - Grid(grid::Message), + Grid(grid::Message, usize), Tick(Instant), TogglePlayback, ToggleGrid(bool), @@ -66,8 +67,10 @@ impl Application for GameOfLife { fn update(&mut self, message: Message) -> Command { match message { - Message::Grid(message) => { - self.grid.update(message); + Message::Grid(message, version) => { + if version == self.version { + self.grid.update(message); + } } Message::Tick(_) | Message::Next => { self.queued_ticks = (self.queued_ticks + 1).min(self.speed); @@ -79,7 +82,11 @@ impl Application for GameOfLife { self.queued_ticks = 0; - return Command::perform(task, Message::Grid); + let version = self.version; + + return Command::perform(task, move |message| { + Message::Grid(message, version) + }); } } Message::TogglePlayback => { @@ -90,6 +97,7 @@ impl Application for GameOfLife { } Message::Clear => { self.grid.clear(); + self.version += 1; } Message::SpeedChanged(speed) => { if self.is_playing { @@ -100,6 +108,7 @@ impl Application for GameOfLife { } Message::PresetPicked(new_preset) => { self.grid = Grid::from_preset(new_preset); + self.version += 1; } } @@ -116,6 +125,7 @@ impl Application for GameOfLife { } fn view(&mut self) -> Element { + let version = self.version; let selected_speed = self.next_speed.unwrap_or(self.speed); let controls = self.controls.view( self.is_playing, @@ -125,7 +135,11 @@ impl Application for GameOfLife { ); let content = Column::new() - .push(self.grid.view().map(Message::Grid)) + .push( + self.grid + .view() + .map(move |message| Message::Grid(message, version)), + ) .push(controls); Container::new(content) @@ -161,7 +175,6 @@ mod grid { show_lines: bool, last_tick_duration: Duration, last_queued_ticks: usize, - version: usize, } #[derive(Debug, Clone)] @@ -171,7 +184,6 @@ mod grid { Ticked { result: Result, tick_duration: Duration, - version: usize, }, } @@ -208,7 +220,6 @@ mod grid { show_lines: true, last_tick_duration: Duration::default(), last_queued_ticks: 0, - version: 0, } } @@ -216,7 +227,6 @@ mod grid { &mut self, amount: usize, ) -> Option> { - let version = self.version; let tick = self.state.tick(amount)?; self.last_queued_ticks = amount; @@ -228,7 +238,6 @@ mod grid { Message::Ticked { result, - version, tick_duration, } }) @@ -250,13 +259,11 @@ mod grid { } Message::Ticked { result: Ok(life), - version, tick_duration, - } if version == self.version => { + } => { self.state.update(life); self.life_cache.clear(); - self.version += 1; self.last_tick_duration = tick_duration; } Message::Ticked { @@ -264,7 +271,6 @@ mod grid { } => { dbg!(error); } - Message::Ticked { .. } => {} } } @@ -278,7 +284,6 @@ mod grid { pub fn clear(&mut self) { self.state = State::default(); self.preset = Preset::Custom; - self.version += 1; self.life_cache.clear(); }