@@ -682,8 +682,6 @@ impl Client {
682
682
/// # Examples
683
683
///
684
684
/// ```no_run
685
- /// # use url::Url;
686
- /// # let homeserver = Url::parse("http://localhost:8080").unwrap();
687
685
/// use matrix_sdk::{
688
686
/// deserialized_responses::EncryptionInfo,
689
687
/// event_handler::Ctx,
@@ -700,14 +698,7 @@ impl Client {
700
698
/// };
701
699
/// use serde::{Deserialize, Serialize};
702
700
///
703
- /// # futures_executor::block_on(async {
704
- /// # let client = matrix_sdk::Client::builder()
705
- /// # .homeserver_url(homeserver)
706
- /// # .server_versions([ruma::api::MatrixVersion::V1_0])
707
- /// # .build()
708
- /// # .await
709
- /// # .unwrap();
710
- /// #
701
+ /// # async fn example(client: Client) {
711
702
/// client.add_event_handler(
712
703
/// |ev: SyncRoomMessageEvent, room: Room, client: Client| async move {
713
704
/// // Common usage: Room event plus room and client.
@@ -773,7 +764,7 @@ impl Client {
773
764
/// client.add_event_handler(move |ev: SyncRoomMessageEvent | async move {
774
765
/// println!("Calling the handler with identifier {data}");
775
766
/// });
776
- /// # });
767
+ /// # }
777
768
/// ```
778
769
pub fn add_event_handler < Ev , Ctx , H > ( & self , handler : H ) -> EventHandlerHandle
779
770
where
@@ -809,7 +800,7 @@ impl Client {
809
800
///
810
801
/// `Ev` represents the kind of event that will be observed. `Ctx`
811
802
/// represents the context that will come with the event. It relies on the
812
- /// same mechanism as [`Self ::add_event_handler`]. The main difference is
803
+ /// same mechanism as [`Client ::add_event_handler`]. The main difference is
813
804
/// that it returns an [`ObservableEventHandler`] and doesn't require a
814
805
/// user-defined closure. It is possible to subscribe to the
815
806
/// [`ObservableEventHandler`] to get an [`EventHandlerSubscriber`], which
@@ -818,21 +809,59 @@ impl Client {
818
809
///
819
810
/// # Example
820
811
///
812
+ /// Let's see a classical usage:
813
+ ///
821
814
/// ```
822
815
/// use futures_util::StreamExt as _;
823
816
/// use matrix_sdk::{
824
817
/// ruma::{events::room::message::SyncRoomMessageEvent, push::Action},
825
818
/// Client, Room,
826
819
/// };
827
820
///
828
- /// # async fn example(client: Client) {
821
+ /// # async fn example(client: Client) -> Option<()> {
829
822
/// let observer =
830
823
/// client.observe_events::<SyncRoomMessageEvent, (Room, Vec<Action>)>();
831
824
///
832
825
/// let mut subscriber = observer.subscribe();
833
826
///
834
- /// let (message_event, (room, push_actions)) =
835
- /// subscriber.next().await.unwrap();
827
+ /// let (event, (room, push_actions)) = subscriber.next().await?;
828
+ /// # Some(())
829
+ /// # }
830
+ /// ```
831
+ ///
832
+ /// Now let's see how to get several contexts that can be useful for you:
833
+ ///
834
+ /// ```
835
+ /// use matrix_sdk::{
836
+ /// deserialized_responses::EncryptionInfo,
837
+ /// ruma::{
838
+ /// events::room::{
839
+ /// message::SyncRoomMessageEvent, topic::SyncRoomTopicEvent,
840
+ /// },
841
+ /// push::Action,
842
+ /// },
843
+ /// Client, Room,
844
+ /// };
845
+ ///
846
+ /// # async fn example(client: Client) {
847
+ /// // Observe `SyncRoomMessageEvent` and fetch `Room` + `Client`.
848
+ /// let _ = client.observe_events::<SyncRoomMessageEvent, (Room, Client)>();
849
+ ///
850
+ /// // Observe `SyncRoomMessageEvent` and fetch `Room` + `EncryptionInfo`
851
+ /// // to distinguish between unencrypted events and events that were decrypted
852
+ /// // by the SDK.
853
+ /// let _ = client
854
+ /// .observe_events::<SyncRoomMessageEvent, (Room, Option<EncryptionInfo>)>(
855
+ /// );
856
+ ///
857
+ /// // Observe `SyncRoomMessageEvent` and fetch `Room` + push actions.
858
+ /// // For example, an event with `Action::SetTweak(Tweak::Highlight(true))`
859
+ /// // should be highlighted in the timeline.
860
+ /// let _ =
861
+ /// client.observe_events::<SyncRoomMessageEvent, (Room, Vec<Action>)>();
862
+ ///
863
+ /// // Observe `SyncRoomTopicEvent` and fetch nothing else.
864
+ /// let _ = client.observe_events::<SyncRoomTopicEvent, ()>();
836
865
/// # }
837
866
/// ```
838
867
///
@@ -847,10 +876,9 @@ impl Client {
847
876
848
877
/// Observe a specific room, and event type.
849
878
///
850
- /// This method works the same way as
851
- /// [`observe_events`][Self::observe_events], except that the observability
852
- /// will only be applied for events in the room with the specified ID.
853
- /// See that method for more details.
879
+ /// This method works the same way as [`Client::observe_events`], except
880
+ /// that the observability will only be applied for events in the room with
881
+ /// the specified ID. See that method for more details.
854
882
pub fn observe_room_events < Ev , Ctx > (
855
883
& self ,
856
884
room_id : & RoomId ,
@@ -862,8 +890,8 @@ impl Client {
862
890
self . observe_room_events_impl ( Some ( room_id. to_owned ( ) ) )
863
891
}
864
892
865
- /// Shared implementation for `Self ::observe_events` and
866
- /// `Self ::observe_room_events`.
893
+ /// Shared implementation for `Client ::observe_events` and
894
+ /// `Client ::observe_room_events`.
867
895
fn observe_room_events_impl < Ev , Ctx > (
868
896
& self ,
869
897
room_id : Option < OwnedRoomId > ,
0 commit comments