From 057090cfe03947229c3e1bcd9801c5821c9d17a5 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 27 Feb 2024 23:21:54 -0500 Subject: [PATCH] Extract notification receiving into an observer --- kifu/gtk/src/lib.rs | 43 +++++++++++++++++-- .../src/view_models/database_view_model.rs | 17 +++----- kifu/gtk/src/view_models/game_view_model.rs | 37 ++++------------ 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/kifu/gtk/src/lib.rs b/kifu/gtk/src/lib.rs index 0a96a10..45106ab 100644 --- a/kifu/gtk/src/lib.rs +++ b/kifu/gtk/src/lib.rs @@ -3,9 +3,10 @@ pub mod ui; mod view_models; mod views; -use kifu_core::{Core, CoreRequest, CoreResponse}; -use std::sync::Arc; -use tokio::{runtime::Runtime, spawn}; +use async_std::task::yield_now; +use kifu_core::{Core, CoreRequest, CoreResponse, Observable}; +use std::{rc::Rc, sync::Arc}; +use tokio::runtime::Runtime; #[derive(Clone)] pub struct CoreApi { @@ -37,3 +38,39 @@ where println!("[Trace: {}] {:?}", trace_name, end - start); result } + +struct LocalObserver { + join_handle: glib::JoinHandle<()>, + handler: Rc, +} + +impl LocalObserver { + fn new(observable: &dyn Observable, handler: impl Fn(T) + 'static) -> Self { + let listener = observable.subscribe(); + let handler = Rc::new(handler); + let join_handle = glib::spawn_future_local({ + let handler = handler.clone(); + async move { + loop { + match listener.recv().await { + Ok(msg) => handler(msg), + Err(err) => { + unimplemented!("Should display an error message in the UI: {}", err) + } + } + yield_now().await; + } + } + }); + Self { + join_handle, + handler, + } + } +} + +impl Drop for LocalObserver { + fn drop(&mut self) { + self.join_handle.abort(); + } +} diff --git a/kifu/gtk/src/view_models/database_view_model.rs b/kifu/gtk/src/view_models/database_view_model.rs index 7582ff4..1afe39d 100644 --- a/kifu/gtk/src/view_models/database_view_model.rs +++ b/kifu/gtk/src/view_models/database_view_model.rs @@ -14,30 +14,25 @@ General Public License for more details. You should have received a copy of the GNU General Public License along with Kifu. If not, see . */ -use async_channel::Receiver; -use async_std::task::{spawn, spawn_blocking, yield_now, JoinHandle}; +use crate::LocalObserver; use kifu_core::{Core, CoreNotification}; /// DatabaseViewModel controls the view that the user sees when starting the application if the application has been configured and if there are no games in progress. It provides a window into the database, showing a list of recently recorded games (whether from this app or from a main database). It also provides the UI for starting a new game. This will render an empty database view if the user hasn't configured a database yet. pub struct DatabaseViewModel { core: Core, - notification_handler: JoinHandle<()>, + notification_observer: LocalObserver, widget: gtk::Box, } impl DatabaseViewModel { - fn new(core: Core, notifications: Receiver) -> Self { - let handler = spawn(async move { - loop { - let message = notifications.recv().await; - println!("Message received in DatabaseViewModel: {:?}", message); - yield_now().await; - } + fn new(core: Core) -> Self { + let notification_observer = LocalObserver::new(&core, |msg| { + println!("DatabaseViewModelHandler called with message: {:?}", msg) }); Self { core, - notification_handler: handler, + notification_observer, widget: gtk::Box::new(gtk::Orientation::Horizontal, 0), } } diff --git a/kifu/gtk/src/view_models/game_view_model.rs b/kifu/gtk/src/view_models/game_view_model.rs index 273df4e..658b7c7 100644 --- a/kifu/gtk/src/view_models/game_view_model.rs +++ b/kifu/gtk/src/view_models/game_view_model.rs @@ -18,6 +18,8 @@ use async_std::{channel::Receiver, task::yield_now}; use kifu_core::{Color, Core, CoreNotification, Goban, Observable, Player}; use std::{cell::RefCell, rc::Rc, time::Duration}; +use crate::LocalObserver; + pub struct GameState { goban: Goban, white_clock: Duration, @@ -37,13 +39,13 @@ 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. pub struct GameViewModel { core: Core, - notification_handler: glib::JoinHandle<()>, + notification_observer: LocalObserver, widget: gtk::Box, data: Rc>, } impl GameViewModelPrivate { - fn handle(&mut self, message: CoreNotification) {} + fn handle(&mut self, _message: CoreNotification) {} } impl GameViewModel { @@ -54,38 +56,15 @@ impl GameViewModel { state: game, })); - let notification_handler = { - let notifications = core.subscribe(); - let data: Rc> = data.clone(); - glib::spawn_future_local(Self::listen(notifications, data)) - }; + let notification_observer = LocalObserver::new(&core, |msg| { + println!("GameViewModelHandler called with message: {:?}", msg) + }); Self { core, - notification_handler, + notification_observer, widget: gtk::Box::new(gtk::Orientation::Horizontal, 0), data, } } - - async fn listen( - notifications: Receiver, - data: Rc>, - ) { - loop { - match notifications.recv().await { - Ok(msg) => data.borrow_mut().handle(msg), - Err(err) => { - unimplemented!("Should display an error message in the UI: {}", err) - } - } - yield_now().await; - } - } -} - -impl Drop for GameViewModel { - fn drop(&mut self) { - self.notification_handler.abort(); - } }