[fractal/fractal-next] room: Make sure Room is disposed



commit e4f446ebeebcce7359dadc118636c5e3ad90fa60
Author: Julian Sparber <julian sparber net>
Date:   Wed Nov 24 12:24:26 2021 +0100

    room: Make sure Room is disposed

 src/session/room/event.rs    | 19 ++++++++-----------
 src/session/room/timeline.rs | 15 ++++++++-------
 2 files changed, 16 insertions(+), 18 deletions(-)
---
diff --git a/src/session/room/event.rs b/src/session/room/event.rs
index 5cf4d87c..ebf39cd1 100644
--- a/src/session/room/event.rs
+++ b/src/session/room/event.rs
@@ -22,6 +22,7 @@ pub struct BoxedSyncRoomEvent(SyncRoomEvent);
 
 mod imp {
     use super::*;
+    use glib::object::WeakRef;
     use glib::subclass::Signal;
     use once_cell::{sync::Lazy, unsync::OnceCell};
     use std::cell::{Cell, RefCell};
@@ -34,7 +35,7 @@ mod imp {
         pub pure_event: RefCell<Option<SyncRoomEvent>>,
         pub relates_to: RefCell<Vec<super::Event>>,
         pub show_header: Cell<bool>,
-        pub room: OnceCell<Room>,
+        pub room: OnceCell<WeakRef<Room>>,
     }
 
     #[glib::object_subclass]
@@ -127,7 +128,9 @@ mod imp {
                     let _ = obj.set_show_header(show_header);
                 }
                 "room" => {
-                    let _ = self.room.set(value.get().unwrap());
+                    self.room
+                        .set(value.get::<Room>().unwrap().downgrade())
+                        .unwrap();
                 }
                 _ => unimplemented!(),
             }
@@ -162,18 +165,12 @@ impl Event {
     }
 
     pub fn sender(&self) -> Member {
-        let priv_ = imp::Event::from_instance(self);
-        priv_
-            .room
-            .get()
-            .unwrap()
-            .members()
-            .member_by_id(&self.matrix_sender())
+        self.room().members().member_by_id(&self.matrix_sender())
     }
 
-    pub fn room(&self) -> &Room {
+    pub fn room(&self) -> Room {
         let priv_ = imp::Event::from_instance(self);
-        priv_.room.get().unwrap()
+        priv_.room.get().unwrap().upgrade().unwrap()
     }
 
     /// Get the matrix event
diff --git a/src/session/room/timeline.rs b/src/session/room/timeline.rs
index 3a4b268f..d6feec72 100644
--- a/src/session/room/timeline.rs
+++ b/src/session/room/timeline.rs
@@ -14,13 +14,14 @@ use crate::{spawn, spawn_tokio};
 
 mod imp {
     use super::*;
+    use glib::object::WeakRef;
     use once_cell::{sync::Lazy, unsync::OnceCell};
     use std::cell::{Cell, RefCell};
     use std::collections::{HashMap, VecDeque};
 
     #[derive(Debug, Default)]
     pub struct Timeline {
-        pub room: OnceCell<Room>,
+        pub room: OnceCell<WeakRef<Room>>,
         /// A store to keep track of related events that aren't known
         pub relates_to_events: RefCell<HashMap<EventId, Vec<EventId>>>,
         /// All events shown in the room history
@@ -98,7 +99,7 @@ mod imp {
 
         fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
             match pspec.name() {
-                "room" => self.room.get().unwrap().to_value(),
+                "room" => obj.room().to_value(),
                 "loading" => obj.loading().to_value(),
                 "empty" => obj.is_empty().to_value(),
                 "complete" => obj.is_complete().to_value(),
@@ -458,12 +459,12 @@ impl Timeline {
 
     fn set_room(&self, room: Room) {
         let priv_ = imp::Timeline::from_instance(self);
-        priv_.room.set(room).unwrap();
+        priv_.room.set(room.downgrade()).unwrap();
     }
 
-    pub fn room(&self) -> &Room {
+    pub fn room(&self) -> Room {
         let priv_ = imp::Timeline::from_instance(self);
-        priv_.room.get().unwrap()
+        priv_.room.get().unwrap().upgrade().unwrap()
     }
 
     fn set_loading(&self, loading: bool) {
@@ -555,11 +556,11 @@ impl Timeline {
                                             events
                                            .into_iter()
                                            .skip(1)
-                                           .map(|event| Event::new(event, obj.room())).collect()
+                                           .map(|event| Event::new(event, &obj.room())).collect()
                             } else {
                                             events
                                            .into_iter()
-                                           .map(|event| Event::new(event, obj.room())).collect()
+                                           .map(|event| Event::new(event, &obj.room())).collect()
                             };
                             obj.set_complete(events.iter().any(|event| matches!(event.matrix_event(), 
Some(AnySyncRoomEvent::State(AnySyncStateEvent::RoomCreate(_))))));
                             obj.prepend(events)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]