@@ -354,6 +354,24 @@ impl SpaceRoomList {
354354 }
355355 }
356356
357+ /// Clears the room list back to its initial state so that any new changes
358+ /// to the hierarchy will be included the next time [`Self::paginate`] is
359+ /// called.
360+ ///
361+ /// This is useful when you've added or removed children from the space as
362+ /// the list is based on a cached state that lives server-side, meaning
363+ /// the /hierarchy request needs to be restarted from scratch to pick up
364+ /// the changes.
365+ pub async fn reset ( & self ) {
366+ let mut pagination_token = self . token . lock ( ) . await ;
367+ * pagination_token = None . into ( ) ;
368+
369+ self . rooms . lock ( ) . clear ( ) ;
370+ self . children_state . lock ( ) . take ( ) ;
371+
372+ self . pagination_state . set ( SpaceRoomListPaginationState :: Idle { end_reached : false } ) ;
373+ }
374+
357375 /// Sorts spare rooms by various criteria as defined in
358376 /// https://spec.matrix.org/latest/client-server-api/#ordering-of-children-within-a-space
359377 fn compare_rooms (
@@ -888,6 +906,44 @@ mod tests {
888906 ) ;
889907 }
890908
909+ #[ async_test]
910+ async fn test_reset ( ) {
911+ let server = MatrixMockServer :: new ( ) . await ;
912+ let client = server. client_builder ( ) . build ( ) . await ;
913+ let space_service = SpaceService :: new ( client. clone ( ) ) . await ;
914+
915+ let parent_space_id = room_id ! ( "!parent_space:example.org" ) ;
916+ let child_space_id_1 = room_id ! ( "!1:example.org" ) ;
917+
918+ server
919+ . mock_get_hierarchy ( )
920+ . ok_with_room_ids ( vec ! [ child_space_id_1] )
921+ . expect ( 2 )
922+ . mount ( )
923+ . await ;
924+
925+ let room_list = space_service. space_room_list ( parent_space_id. to_owned ( ) ) . await ;
926+
927+ room_list. paginate ( ) . await . unwrap ( ) ;
928+
929+ // This space contains 1 room
930+ assert_eq ! ( room_list. rooms( ) . len( ) , 1 ) ;
931+
932+ // Resetting the room list
933+ room_list. reset ( ) . await ;
934+
935+ // Clears the rooms and pagination token
936+ assert_eq ! ( room_list. rooms( ) . len( ) , 0 ) ;
937+ assert_matches ! (
938+ room_list. pagination_state( ) ,
939+ SpaceRoomListPaginationState :: Idle { end_reached: false }
940+ ) ;
941+
942+ // Allows paginating again
943+ room_list. paginate ( ) . await . unwrap ( ) ;
944+ assert_eq ! ( room_list. rooms( ) . len( ) , 1 ) ;
945+ }
946+
891947 fn make_space_room (
892948 room_id : OwnedRoomId ,
893949 order : Option < & str > ,
0 commit comments