[][src]Struct twitchchat::Dispatcher

pub struct Dispatcher { /* fields omitted */ }

An event dispatcher

This allows multiple sources to subscribe to specific Events which'll produce a corresponding Message.

The subscription will return a EventStream which can be used as a Stream.

Methods

impl Dispatcher[src]

pub fn new() -> Self[src]

This is supported on feature="tokio" only.

Create a new event dispatcher

pub async fn wait_for<'_, T>(&'_ self) -> Result<Arc<T::Owned>, Error> where
    T: Event<'static> + 'static,
    T: EventMapped<'static, T>, 
[src]

This is supported on feature="tokio" only.

Subscribes to an event and blocks until the next item is available

This is useful when you want to wait, for say, the IrcReady event before you join channels.


NOTE Any subsequent calls to wait_for for this event will return a cached value.

Example

let dispatcher = Dispatcher::new();
let (runner, control) = Runner::new(dispatcher.clone(), RateLimit::default());
// You should spawn the run() away so it can start to process events
let handle = spawn(runner.run(conn));
// block until we get an IrcReady
let _ = dispatcher.wait_for::<events::IrcReady>().await.unwrap();
// it'll cache the event
let _ = dispatcher.wait_for::<events::IrcReady>()
    .now_or_never()
    .expect("cached value")
    .unwrap();
// stop the runner
control.stop();
// just to wait for the spawned task to end
let _ = handle.await.unwrap().unwrap();

pub fn subscribe<'a, T>(&self) -> EventStream<Arc<T::Owned>> where
    T: Event<'a> + 'static,
    T: EventMapped<'a, T>, 
[src]

This is supported on feature="tokio" only.

Subscribe to an Event which'll return a Stream of a corresponding Message.

Example

let dispatcher = Dispatcher::new();
let (runner, control) = Runner::new(dispatcher.clone(), RateLimit::default());
// spawn the runner in the background, just to drive things for us
// (you could select over it, or await at the end)
spawn(runner.run(conn));

// get some streams for events you're interested in
// when you drop the streams it'll unsubscribe them
let mut join_stream = dispatcher.subscribe::<events::Join>();
let privmsg_stream = dispatcher.subscribe::<events::Privmsg>();
// you can subscribe multiple times to the same event
let another_one = dispatcher.subscribe::<events::Privmsg>();
// you can also get an enum of all possible events
let mut all_events = dispatcher.subscribe::<events::All>();
// or the raw IRC message
let raw_event = dispatcher.subscribe::<events::Raw>();

// using for each
let print_raw = raw_event.for_each(|msg| async move {
    println!("{}", msg.raw.escape_debug());
});
// and spawn that future on another task
spawn(print_raw);

// loop and select
loop {
    tokio::select!{
        Some(msg) = &mut join_stream.next() => {}
        Some(all) = &mut all_events.next() => {}
        else => break
    }
}

Mapping

Use an event from Events and subscribe will produce an EventStream<Arc<T>> which corresponds to the message in Messages.

A table of mappings

EventMessageDescription
CapCapCapability response from the server
ClearChatClearChatSomeone cleared the chat
ClearMsgClearMsgSomeone removed a users message(s)
GlobalUserStateGlobalUserStateYour user information from the server
HostTargetHostTargetWhen a channel hosts/unhosts another channel
IrcReadyIrcReadyWhen the IRC connection is ready
JoinJoinWhen a user joins a channel
ModeModeWhen someone gains/loses moderator status
NamesNamesServer giving you a stream of names for a channel
NoticeNoticeGeneral notices from the server
PartPartWhen a user leaves a channel
PingPingServer requesting a response (for heartbeat/connection tracking)
PongPongServer responding to a user-provided PING
PrivmsgPrivmsgA normal message sent by a user
RawRawThe raw message before being specialized
ReadyReadyWhen the Twitch connection is ready
ReconnectReconnectServer asking you to reconnect
RoomStateRoomStateServer giving you information about the room
UserNoticeUserNoticeMetadata attached to an user event (e.g. a subscription)
UserStateUserStateIdentifies a user's chat settings or properties (e.g., chat color).
---------
AllAllCommandsThis bundles all above messages into a single enum.

Disconnection

The stream will also yield None when the Dispatcher is dropped.

Or if the subscriptions were cleared.

Tip

If you hold onto clones of the dispatcher, you can remove the event, or all events to force the respective Stream(s) to end

pub fn count_subscribers<'a, T>(&self) -> usize where
    T: Event<'a> + 'static, 
[src]

This is supported on feature="tokio" only.

Get the subscriber count for a specific event

pub fn count_subscribers_all(&self) -> usize[src]

This is supported on feature="tokio" only.

Get the subscriber count for all events

pub fn clear_subscriptions<'a, T>(&self) -> usize where
    T: Event<'a> + 'static, 
[src]

This is supported on feature="tokio" only.

Clear subscriptions for a specific event, returning how many subscribers were removed

pub fn clear_subscriptions_all(&self) -> usize[src]

This is supported on feature="tokio" only.

Clear all subscriptions, returning how many subscribers were removed

Trait Implementations

impl Clone for Dispatcher[src]

impl Debug for Dispatcher[src]

impl Default for Dispatcher[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.