App: Fix more refference cycles

parent d54e15cd
Pipeline #24736 passed with stages
in 10 minutes and 47 seconds
#![cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
use gdk::FrameClockExt;
use gdk_pixbuf::Pixbuf;
use glib;
use gdk_pixbuf::{Object, Pixbuf};
use glib::{self, object::WeakRef};
use gtk;
use gtk::prelude::*;
use gtk::{IsA, Widget};
......@@ -64,16 +64,26 @@ use i18n::i18n;
/// let list = gtk::ListBox::new();
/// lazy_load(widgets, list, |w| w, || {});
/// ```
pub(crate) fn lazy_load<T, C, F, W, U>(data: T, container: C, mut contructor: F, callback: U)
where
pub(crate) fn lazy_load<T, C, F, W, U>(
data: T,
container: WeakRef<C>,
mut contructor: F,
callback: U,
) where
T: IntoIterator + 'static,
T::Item: 'static,
C: ContainerExt + 'static,
// FIXME: leaking a strong refference here
C: IsA<Object> + ContainerExt + 'static,
F: FnMut(T::Item) -> W + 'static,
W: IsA<Widget> + WidgetExt,
U: Fn() + 'static,
{
let func = move |x| {
let container = match container.upgrade() {
Some(c) => c,
None => return,
};
let widget = contructor(x);
container.add(&widget);
widget.show();
......
use glib;
use gtk::{self, prelude::*, Adjustment};
use crossbeam_channel::Sender;
use crossbeam_channel::{bounded, Sender};
use failure::Error;
use fragile::Fragile;
use html2text;
......@@ -103,14 +103,11 @@ impl ShowWidget {
/// Populate the listbox with the shows episodes.
fn populate_listbox(
// FIXME: we are leaking strong refs here
show: &Rc<ShowWidget>,
pd: Arc<Show>,
sender: Sender<Action>,
vadj: Option<Adjustment>,
) -> Result<(), Error> {
use crossbeam_channel::bounded;
let count = dbqueries::get_pd_episodes_count(&pd)?;
let (sender_, receiver) = bounded(1);
......@@ -128,7 +125,8 @@ fn populate_listbox(
return Ok(());
}
let show_ = show.clone();
let show_weak = Rc::downgrade(&show);
let list_weak = show.episodes.downgrade();
gtk::idle_add(move || {
let episodes = match receiver.try_recv() {
Some(e) => e,
......@@ -136,18 +134,18 @@ fn populate_listbox(
};
debug_assert!(episodes.len() as i64 == count);
let list = show_.episodes.clone();
let constructor = clone!(sender => move |ep| {
EpisodeWidget::new(ep, &sender).container.clone()
});
let callback = clone!(show_, vadj => move || {
if let Some(ref v) = vadj {
show_.view.set_adjutments(None, Some(v))
let callback = clone!(show_weak, vadj => move || {
match (show_weak.upgrade(), &vadj) {
(Some(ref shows), Some(ref v)) => shows.view.set_adjutments(None, Some(v)),
_ => (),
};
});
lazy_load(episodes, list.clone(), constructor, callback);
lazy_load(episodes, list_weak.clone(), constructor, callback);
glib::Continue(false)
});
......
......@@ -64,17 +64,18 @@ impl ShowsView {
fn populate_flowbox(shows: &Rc<ShowsView>, vadj: Option<Adjustment>) -> Result<(), Error> {
let ignore = get_ignored_shows()?;
let podcasts = dbqueries::get_podcasts_filter(&ignore)?;
let show_weak = Rc::downgrade(&shows);
let flowbox_weak = shows.flowbox.downgrade();
let constructor = move |parent| ShowsChild::new(&parent).child;
// FIXME: We are, possibly,leaking the strong ref here
let callback = clone!(shows => move || {
if let Some(ref v) = vadj {
shows.view.set_adjutments(None, Some(v))
let callback = move || {
match (show_weak.upgrade(), &vadj) {
(Some(ref shows), Some(ref v)) => shows.view.set_adjutments(None, Some(v)),
_ => (),
};
});
};
let flowbox = shows.flowbox.clone();
lazy_load(podcasts, flowbox, constructor, callback);
lazy_load(podcasts, flowbox_weak, constructor, callback);
Ok(())
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment