Skip to content

Commit 0509236

Browse files
committed
doc(sdk): Improve documentation of Client::observe_events.
1 parent 6cef7f2 commit 0509236

File tree

1 file changed

+49
-21
lines changed
  • crates/matrix-sdk/src/client

1 file changed

+49
-21
lines changed

crates/matrix-sdk/src/client/mod.rs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,6 @@ impl Client {
682682
/// # Examples
683683
///
684684
/// ```no_run
685-
/// # use url::Url;
686-
/// # let homeserver = Url::parse("http://localhost:8080").unwrap();
687685
/// use matrix_sdk::{
688686
/// deserialized_responses::EncryptionInfo,
689687
/// event_handler::Ctx,
@@ -700,14 +698,7 @@ impl Client {
700698
/// };
701699
/// use serde::{Deserialize, Serialize};
702700
///
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) {
711702
/// client.add_event_handler(
712703
/// |ev: SyncRoomMessageEvent, room: Room, client: Client| async move {
713704
/// // Common usage: Room event plus room and client.
@@ -773,7 +764,7 @@ impl Client {
773764
/// client.add_event_handler(move |ev: SyncRoomMessageEvent | async move {
774765
/// println!("Calling the handler with identifier {data}");
775766
/// });
776-
/// # });
767+
/// # }
777768
/// ```
778769
pub fn add_event_handler<Ev, Ctx, H>(&self, handler: H) -> EventHandlerHandle
779770
where
@@ -809,7 +800,7 @@ impl Client {
809800
///
810801
/// `Ev` represents the kind of event that will be observed. `Ctx`
811802
/// 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
813804
/// that it returns an [`ObservableEventHandler`] and doesn't require a
814805
/// user-defined closure. It is possible to subscribe to the
815806
/// [`ObservableEventHandler`] to get an [`EventHandlerSubscriber`], which
@@ -818,21 +809,59 @@ impl Client {
818809
///
819810
/// # Example
820811
///
812+
/// Let's see a classical usage:
813+
///
821814
/// ```
822815
/// use futures_util::StreamExt as _;
823816
/// use matrix_sdk::{
824817
/// ruma::{events::room::message::SyncRoomMessageEvent, push::Action},
825818
/// Client, Room,
826819
/// };
827820
///
828-
/// # async fn example(client: Client) {
821+
/// # async fn example(client: Client) -> Option<()> {
829822
/// let observer =
830823
/// client.observe_events::<SyncRoomMessageEvent, (Room, Vec<Action>)>();
831824
///
832825
/// let mut subscriber = observer.subscribe();
833826
///
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, ()>();
836865
/// # }
837866
/// ```
838867
///
@@ -847,10 +876,9 @@ impl Client {
847876

848877
/// Observe a specific room, and event type.
849878
///
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.
854882
pub fn observe_room_events<Ev, Ctx>(
855883
&self,
856884
room_id: &RoomId,
@@ -862,8 +890,8 @@ impl Client {
862890
self.observe_room_events_impl(Some(room_id.to_owned()))
863891
}
864892

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`.
867895
fn observe_room_events_impl<Ev, Ctx>(
868896
&self,
869897
room_id: Option<OwnedRoomId>,

0 commit comments

Comments
 (0)