From 720b73f2a8a3a7e2646607c2a7b57f13972194c2 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Sun, 1 Dec 2024 00:48:04 +0100 Subject: [PATCH] per carousel semaphore --- src/ui/album_carousel/album.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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 {