Move `version` from `Grid` to `GameOfLife` struct

This commit is contained in:
Héctor Ramón Jiménez 2020-09-12 20:34:58 +02:00
parent 49076c6ac2
commit 4f2962d73f
1 changed files with 20 additions and 15 deletions

View File

@ -31,11 +31,12 @@ struct GameOfLife {
queued_ticks: usize, queued_ticks: usize,
speed: usize, speed: usize,
next_speed: Option<usize>, next_speed: Option<usize>,
version: usize,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
Grid(grid::Message), Grid(grid::Message, usize),
Tick(Instant), Tick(Instant),
TogglePlayback, TogglePlayback,
ToggleGrid(bool), ToggleGrid(bool),
@ -66,8 +67,10 @@ impl Application for GameOfLife {
fn update(&mut self, message: Message) -> Command<Message> { fn update(&mut self, message: Message) -> Command<Message> {
match message { match message {
Message::Grid(message) => { Message::Grid(message, version) => {
self.grid.update(message); if version == self.version {
self.grid.update(message);
}
} }
Message::Tick(_) | Message::Next => { Message::Tick(_) | Message::Next => {
self.queued_ticks = (self.queued_ticks + 1).min(self.speed); self.queued_ticks = (self.queued_ticks + 1).min(self.speed);
@ -79,7 +82,11 @@ impl Application for GameOfLife {
self.queued_ticks = 0; 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 => { Message::TogglePlayback => {
@ -90,6 +97,7 @@ impl Application for GameOfLife {
} }
Message::Clear => { Message::Clear => {
self.grid.clear(); self.grid.clear();
self.version += 1;
} }
Message::SpeedChanged(speed) => { Message::SpeedChanged(speed) => {
if self.is_playing { if self.is_playing {
@ -100,6 +108,7 @@ impl Application for GameOfLife {
} }
Message::PresetPicked(new_preset) => { Message::PresetPicked(new_preset) => {
self.grid = Grid::from_preset(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<Message> { fn view(&mut self) -> Element<Message> {
let version = self.version;
let selected_speed = self.next_speed.unwrap_or(self.speed); let selected_speed = self.next_speed.unwrap_or(self.speed);
let controls = self.controls.view( let controls = self.controls.view(
self.is_playing, self.is_playing,
@ -125,7 +135,11 @@ impl Application for GameOfLife {
); );
let content = Column::new() let content = Column::new()
.push(self.grid.view().map(Message::Grid)) .push(
self.grid
.view()
.map(move |message| Message::Grid(message, version)),
)
.push(controls); .push(controls);
Container::new(content) Container::new(content)
@ -161,7 +175,6 @@ mod grid {
show_lines: bool, show_lines: bool,
last_tick_duration: Duration, last_tick_duration: Duration,
last_queued_ticks: usize, last_queued_ticks: usize,
version: usize,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -171,7 +184,6 @@ mod grid {
Ticked { Ticked {
result: Result<Life, TickError>, result: Result<Life, TickError>,
tick_duration: Duration, tick_duration: Duration,
version: usize,
}, },
} }
@ -208,7 +220,6 @@ mod grid {
show_lines: true, show_lines: true,
last_tick_duration: Duration::default(), last_tick_duration: Duration::default(),
last_queued_ticks: 0, last_queued_ticks: 0,
version: 0,
} }
} }
@ -216,7 +227,6 @@ mod grid {
&mut self, &mut self,
amount: usize, amount: usize,
) -> Option<impl Future<Output = Message>> { ) -> Option<impl Future<Output = Message>> {
let version = self.version;
let tick = self.state.tick(amount)?; let tick = self.state.tick(amount)?;
self.last_queued_ticks = amount; self.last_queued_ticks = amount;
@ -228,7 +238,6 @@ mod grid {
Message::Ticked { Message::Ticked {
result, result,
version,
tick_duration, tick_duration,
} }
}) })
@ -250,13 +259,11 @@ mod grid {
} }
Message::Ticked { Message::Ticked {
result: Ok(life), result: Ok(life),
version,
tick_duration, tick_duration,
} if version == self.version => { } => {
self.state.update(life); self.state.update(life);
self.life_cache.clear(); self.life_cache.clear();
self.version += 1;
self.last_tick_duration = tick_duration; self.last_tick_duration = tick_duration;
} }
Message::Ticked { Message::Ticked {
@ -264,7 +271,6 @@ mod grid {
} => { } => {
dbg!(error); dbg!(error);
} }
Message::Ticked { .. } => {}
} }
} }
@ -278,7 +284,6 @@ mod grid {
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.state = State::default(); self.state = State::default();
self.preset = Preset::Custom; self.preset = Preset::Custom;
self.version += 1;
self.life_cache.clear(); self.life_cache.clear();
} }