Ensure the game_view_model handler aborts when the view_model gets dropped
This commit is contained in:
parent
915028c32a
commit
da7b40c396
|
@ -14,10 +14,7 @@ General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License along with Kifu. If not, see <https://www.gnu.org/licenses/>.
|
You should have received a copy of the GNU General Public License along with Kifu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use async_std::{
|
use async_std::{channel::Receiver, task::yield_now};
|
||||||
channel::Receiver,
|
|
||||||
task::{spawn, yield_now, JoinHandle},
|
|
||||||
};
|
|
||||||
use kifu_core::{Color, Core, CoreNotification, Goban, Observable, Player};
|
use kifu_core::{Color, Core, CoreNotification, Goban, Observable, Player};
|
||||||
use std::{cell::RefCell, rc::Rc, time::Duration};
|
use std::{cell::RefCell, rc::Rc, time::Duration};
|
||||||
|
|
||||||
|
@ -40,6 +37,7 @@ struct GameViewModelPrivate {
|
||||||
/// The Game View Model manages the current state of the game. It shows the two player cards, the board, the current capture count, the current player, and it maintains the UI for the clock (bearing in mind that the real clock is managed in the core). This view model should only be created once the details of the game, whether a game in progress or a new game (this view model won't know the difference) is known.
|
/// The Game View Model manages the current state of the game. It shows the two player cards, the board, the current capture count, the current player, and it maintains the UI for the clock (bearing in mind that the real clock is managed in the core). This view model should only be created once the details of the game, whether a game in progress or a new game (this view model won't know the difference) is known.
|
||||||
pub struct GameViewModel {
|
pub struct GameViewModel {
|
||||||
core: Core,
|
core: Core,
|
||||||
|
notification_handler: glib::JoinHandle<()>,
|
||||||
widget: gtk::Box,
|
widget: gtk::Box,
|
||||||
data: Rc<RefCell<GameViewModelPrivate>>,
|
data: Rc<RefCell<GameViewModelPrivate>>,
|
||||||
}
|
}
|
||||||
|
@ -56,17 +54,18 @@ impl GameViewModel {
|
||||||
state: game,
|
state: game,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let s = Self {
|
let notification_handler = {
|
||||||
core,
|
let notifications = core.subscribe();
|
||||||
widget: gtk::Box::new(gtk::Orientation::Horizontal, 0),
|
let data: Rc<RefCell<GameViewModelPrivate>> = data.clone();
|
||||||
data,
|
glib::spawn_future_local(Self::listen(notifications, data))
|
||||||
};
|
};
|
||||||
|
|
||||||
let notifications = s.core.subscribe();
|
Self {
|
||||||
let data = s.data.clone();
|
core,
|
||||||
glib::spawn_future_local(Self::listen(notifications, data));
|
notification_handler,
|
||||||
|
widget: gtk::Box::new(gtk::Orientation::Horizontal, 0),
|
||||||
s
|
data,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn listen(
|
async fn listen(
|
||||||
|
@ -84,3 +83,9 @@ impl GameViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for GameViewModel {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.notification_handler.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue