per carousel semaphore

This commit is contained in:
Erica Z 2024-12-01 00:48:04 +01:00
parent abfd5ab568
commit 720b73f2a8

View file

@ -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<Option<gdk::Paintable>>,
pub cover_art_loading: Cell<Option<glib::JoinHandle<()>>>,
pub semaphore: OnceCell<Rc<Semaphore>>,
}
#[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<imp::Album>)
@extends adw::Bin, gtk::Widget,
@ -55,6 +55,13 @@ glib::wrapper! {
}
impl Album {
fn semaphore(&self) -> &Rc<Semaphore> {
// 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 {