[−][src]Struct twitchchat::Dispatcher
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.
Implementations
impl Dispatcher
[src]
pub fn new() -> Self
[src]
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]
T: Event<'static> + 'static,
T: EventMapped<'static, T>,
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 (mut runner, control) = Runner::new(dispatcher.clone()); // You should spawn the run() away so it can start to process events let handle = spawn(async move { runner.run_to_completion(conn).await }); // 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]
T: Event<'a> + 'static,
T: EventMapped<'a, T>,
feature="tokio"
only.Subscribe to an Event which'll return a Stream of a corresponding Message.
Example
let dispatcher = Dispatcher::new(); let (mut runner, control) = Runner::new(dispatcher.clone()); // spawn the runner in the background, just to drive things for us // (you could select over it, or await at the end) spawn(async move { runner.run_to_completion(conn).await }); // 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
Event | Message | Description |
---|---|---|
Cap | Cap | Capability response from the server |
ClearChat | ClearChat | Someone cleared the chat |
ClearMsg | ClearMsg | Someone removed a users message(s) |
GlobalUserState | GlobalUserState | Your user information from the server |
HostTarget | HostTarget | When a channel hosts/unhosts another channel |
IrcReady | IrcReady | When the IRC connection is ready |
Join | Join | When a user joins a channel |
[Notice][Notice_event] | [Notice][Notice_message] | General notices from the server |
Part | Part | When a user leaves a channel |
Ping | Ping | Server requesting a response (for heartbeat/connection tracking) |
Pong | Pong | Server responding to a user-provided PING |
Privmsg | Privmsg | A normal message sent by a user |
Raw | Raw | The raw message before being specialized |
Ready | Ready | When the Twitch connection is ready |
Reconnect | Reconnect | Server asking you to reconnect |
RoomState | RoomState | Server giving you information about the room |
UserNotice | UserNotice | Metadata attached to an user event (e.g. a subscription) |
UserState | UserState | Identifies a user's chat settings or properties (e.g., chat color). |
--- | --- | --- |
All | AllCommands | This 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]
T: Event<'a> + 'static,
feature="tokio"
only.Get the subscriber count for a specific event
pub fn count_subscribers_all(&self) -> usize
[src]
feature="tokio"
only.Get the subscriber count for all events
pub fn clear_subscriptions<'a, T>(&self) -> usize where
T: Event<'a> + 'static,
[src]
T: Event<'a> + 'static,
feature="tokio"
only.Clear subscriptions for a specific event, returning how many subscribers were removed
pub fn clear_subscriptions_all(&self) -> usize
[src]
feature="tokio"
only.Clear all subscriptions, returning how many subscribers were removed
Trait Implementations
impl Clone for Dispatcher
[src]
fn clone(&self) -> Dispatcher
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for Dispatcher
[src]
impl Default for Dispatcher
[src]
Auto Trait Implementations
impl !RefUnwindSafe for Dispatcher
impl Send for Dispatcher
impl Sync for Dispatcher
impl Unpin for Dispatcher
impl !UnwindSafe for Dispatcher
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,