backend: New library update mechanism

This commit is contained in:
Elias Projahn 2021-04-24 18:38:23 +02:00
parent 274ba719a3
commit 3e2f73fc56
2 changed files with 17 additions and 3 deletions

View file

@ -55,6 +55,12 @@ pub struct Backend {
/// is guaranteed to be Some, when the state is set to BackendState::Ready. /// is guaranteed to be Some, when the state is set to BackendState::Ready.
music_library_path: RefCell<Option<PathBuf>>, music_library_path: RefCell<Option<PathBuf>>,
/// The receiver to which library update notifications are sent.
library_updated_receiver: RefCell<mpsc::Receiver<()>>,
/// The sender for sending library update notifications.
library_updated_sender: RefCell<mpsc::Sender<()>>,
/// The database. This can be assumed to exist, when the state is set to BackendState::Ready. /// The database. This can be assumed to exist, when the state is set to BackendState::Ready.
database: RefCell<Option<Rc<DbThread>>>, database: RefCell<Option<Rc<DbThread>>>,
@ -71,12 +77,15 @@ impl Backend {
/// and call init() afterwards. /// and call init() afterwards.
pub fn new() -> Self { pub fn new() -> Self {
let (state_sender, state_stream) = mpsc::channel(1024); let (state_sender, state_stream) = mpsc::channel(1024);
let (library_updated_sender, library_updated_receiver) = mpsc::channel(1024);
Backend { Backend {
state_stream: RefCell::new(state_stream), state_stream: RefCell::new(state_stream),
state_sender: RefCell::new(state_sender), state_sender: RefCell::new(state_sender),
settings: gio::Settings::new("de.johrpan.musicus"), settings: gio::Settings::new("de.johrpan.musicus"),
music_library_path: RefCell::new(None), music_library_path: RefCell::new(None),
library_updated_sender: RefCell::new(library_updated_sender),
library_updated_receiver: RefCell::new(library_updated_receiver),
database: RefCell::new(None), database: RefCell::new(None),
player: RefCell::new(None), player: RefCell::new(None),
client: Client::new(), client: Client::new(),

View file

@ -1,7 +1,8 @@
use crate::{Backend, BackendState, Player, Result}; use crate::{Backend, BackendState, Player, Result};
use musicus_database::DbThread; use futures::prelude::*;
use gio::prelude::*; use gio::prelude::*;
use log::warn; use log::warn;
use musicus_database::DbThread;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
@ -72,10 +73,14 @@ impl Backend {
self.player.borrow().clone() self.player.borrow().clone()
} }
/// Wait for the next library update.
pub async fn library_update(&self) {
self.library_updated_receiver.borrow_mut().next().await;
}
/// Notify the frontend that the library was changed. /// Notify the frontend that the library was changed.
pub fn library_changed(&self) { pub fn library_changed(&self) {
self.set_state(BackendState::Loading); self.library_updated_sender.borrow_mut().try_send(()).unwrap();
self.set_state(BackendState::Ready);
} }
/// Get an interface to the player and panic if there is none. /// Get an interface to the player and panic if there is none.