Skip to content

Commit

Permalink
Move version from Grid to GameOfLife struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Sep 12, 2020
1 parent 49076c6 commit 4f2962d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ struct GameOfLife {
queued_ticks: usize,
speed: usize,
next_speed: Option<usize>,
version: usize,
}

#[derive(Debug, Clone)]
enum Message {
Grid(grid::Message),
Grid(grid::Message, usize),
Tick(Instant),
TogglePlayback,
ToggleGrid(bool),
Expand Down Expand Up @@ -66,8 +67,10 @@ impl Application for GameOfLife {

fn update(&mut self, message: Message) -> Command<Message> {
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);
Expand All @@ -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 => {
Expand All @@ -90,6 +97,7 @@ impl Application for GameOfLife {
}
Message::Clear => {
self.grid.clear();
self.version += 1;
}
Message::SpeedChanged(speed) => {
if self.is_playing {
Expand All @@ -100,6 +108,7 @@ impl Application for GameOfLife {
}
Message::PresetPicked(new_preset) => {
self.grid = Grid::from_preset(new_preset);
self.version += 1;
}
}

Expand All @@ -116,6 +125,7 @@ impl Application for GameOfLife {
}

fn view(&mut self) -> Element<Message> {
let version = self.version;
let selected_speed = self.next_speed.unwrap_or(self.speed);
let controls = self.controls.view(
self.is_playing,
Expand All @@ -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)
Expand Down Expand Up @@ -161,7 +175,6 @@ mod grid {
show_lines: bool,
last_tick_duration: Duration,
last_queued_ticks: usize,
version: usize,
}

#[derive(Debug, Clone)]
Expand All @@ -171,7 +184,6 @@ mod grid {
Ticked {
result: Result<Life, TickError>,
tick_duration: Duration,
version: usize,
},
}

Expand Down Expand Up @@ -208,15 +220,13 @@ mod grid {
show_lines: true,
last_tick_duration: Duration::default(),
last_queued_ticks: 0,
version: 0,
}
}

pub fn tick(
&mut self,
amount: usize,
) -> Option<impl Future<Output = Message>> {
let version = self.version;
let tick = self.state.tick(amount)?;

self.last_queued_ticks = amount;
Expand All @@ -228,7 +238,6 @@ mod grid {

Message::Ticked {
result,
version,
tick_duration,
}
})
Expand All @@ -250,21 +259,18 @@ 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 {
result: Err(error), ..
} => {
dbg!(error);
}
Message::Ticked { .. } => {}
}
}

Expand All @@ -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();
}
Expand Down

0 comments on commit 4f2962d

Please sign in to comment.