[fractal/fractal-next] room: Load members only when the room is displayed



commit 7a93447d288c9e28503b02e94d17ae8b292811ef
Author: Julian Sparber <julian sparber net>
Date:   Thu Jul 15 13:42:24 2021 +0200

    room: Load members only when the room is displayed
    
    Loading members for all rooms at the app start makes the app hange and
    is pretty slow, therefore only load the members once the room is opened
    by the user.

 src/session/content/room_history.rs |  1 +
 src/session/room/room.rs            | 16 +++++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)
---
diff --git a/src/session/content/room_history.rs b/src/session/content/room_history.rs
index 212a1274..570cc3e0 100644
--- a/src/session/content/room_history.rs
+++ b/src/session/content/room_history.rs
@@ -248,6 +248,7 @@ impl RoomHistory {
             );
 
             priv_.empty_timeline_handler.replace(Some(handler_id));
+            room.load_members();
         }
 
         // TODO: use gtk::MultiSelection to allow selection
diff --git a/src/session/room/room.rs b/src/session/room/room.rs
index dfa47d44..78e91b7c 100644
--- a/src/session/room/room.rs
+++ b/src/session/room/room.rs
@@ -57,6 +57,7 @@ mod imp {
         pub room_members: RefCell<HashMap<UserId, User>>,
         /// The user who sent the invite to this room. This is only set when this room is an invitiation.
         pub inviter: RefCell<Option<User>>,
+        pub members_loaded: Cell<bool>,
     }
 
     #[glib::object_subclass]
@@ -261,7 +262,6 @@ impl Room {
 
         priv_.matrix_room.replace(Some(matrix_room));
 
-        self.load_members();
         self.load_display_name();
         self.load_category();
     }
@@ -593,16 +593,26 @@ impl Room {
         user.update_from_member_event(event);
     }
 
-    fn load_members(&self) {
+    pub fn load_members(&self) {
+        let priv_ = imp::Room::from_instance(self);
+        if priv_.members_loaded.get() {
+            return;
+        }
+
+        priv_.members_loaded.set(true);
         let matrix_room = self.matrix_room();
         do_async(
             glib::PRIORITY_LOW,
             async move { matrix_room.active_members().await },
             clone!(@weak self as obj => move |members| async move {
                 // FIXME: We should retry to load the room members if the request failed
+                let priv_ = imp::Room::from_instance(&obj);
                 match members {
                         Ok(members) => obj.add_members(members),
-                        Err(error) => error!("Couldn’t load room members: {}", error),
+                        Err(error) => {
+                            priv_.members_loaded.set(false);
+                            error!("Couldn’t load room members: {}", error)
+                        },
                 };
             }),
         );


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