diff --git a/src/ui/album_carousel/album.rs b/src/ui/album_carousel/album.rs index df67327..06a2ddb 100644 --- a/src/ui/album_carousel/album.rs +++ b/src/ui/album_carousel/album.rs @@ -2,7 +2,8 @@ use crate::model; use adw::gdk; use adw::{prelude::*, subclass::prelude::*}; use glib::subclass::InitializingObject; -use std::cell::{Cell, RefCell}; +use std::cell::{Cell, OnceCell, RefCell}; +use std::rc::Rc; use tokio::sync::Semaphore; use tracing::{event, Level}; @@ -20,6 +21,8 @@ mod imp { cover_art: RefCell>, pub cover_art_loading: Cell>>, + + pub semaphore: OnceCell>, } #[glib::object_subclass] @@ -45,9 +48,6 @@ mod imp { impl BinImpl for Album {} } -// only fetch 10 thumbnails at a time -static SEM: Semaphore = Semaphore::const_new(10); - glib::wrapper! { pub struct Album(ObjectSubclass) @extends adw::Bin, gtk::Widget, @@ -55,6 +55,13 @@ glib::wrapper! { } impl Album { + fn semaphore(&self) -> &Rc { + // only load 5 at a time + self.imp() + .semaphore + .get_or_init(|| Rc::new(Semaphore::new(5))) + } + pub fn bind(&self, client: &crate::wrappers::Client, album: &model::Album) { self.set_item(Some(album)); @@ -62,8 +69,9 @@ impl Album { let id = album.id(); let this = self.downgrade(); let scale_factor = self.scale_factor() as u32; + let sem = Rc::clone(self.semaphore()); let handle = glib::spawn_future_local(async move { - let _permit = SEM.acquire().await.unwrap(); + let _permit = sem.acquire().await.unwrap(); // see pixel-size in album_carousel_album.blp let pixbuf = match client.cover_art(&id, Some(160 * scale_factor)).await {