Async Observer Trait (#147)
This commit is contained in:
parent
69b7c2445c
commit
0d6d705e46
|
@ -35,6 +35,7 @@ sqlx = { git = "https://github.com/zert3x/sqlx", branch="feature/skip", features
|
||||||
thiserror = "1.0.40"
|
thiserror = "1.0.40"
|
||||||
jsonwebtoken = "8.3.0"
|
jsonwebtoken = "8.3.0"
|
||||||
log = "0.4.19"
|
log = "0.4.19"
|
||||||
|
async-trait = "0.1.71"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = {version = "1.28.1", features = ["full"]}
|
tokio = {version = "1.28.1", features = ["full"]}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
use chorus::{
|
use chorus::{
|
||||||
self,
|
self,
|
||||||
gateway::{Gateway, Observer},
|
gateway::{Gateway, Observer},
|
||||||
|
@ -15,9 +16,10 @@ pub struct ExampleObserver {}
|
||||||
// This struct can observe GatewayReady events when subscribed, because it implements the trait Observer<GatewayReady>.
|
// This struct can observe GatewayReady events when subscribed, because it implements the trait Observer<GatewayReady>.
|
||||||
// The Observer trait can be implemented for a struct for a given websocketevent to handle observing it
|
// The Observer trait can be implemented for a struct for a given websocketevent to handle observing it
|
||||||
// One struct can be an observer of multiple websocketevents, if needed
|
// One struct can be an observer of multiple websocketevents, if needed
|
||||||
|
#[async_trait]
|
||||||
impl Observer<GatewayReady> for ExampleObserver {
|
impl Observer<GatewayReady> for ExampleObserver {
|
||||||
// After we subscribe to an event this function is called every time we receive it
|
// After we subscribe to an event this function is called every time we receive it
|
||||||
fn update(&self, _data: &GatewayReady) {
|
async fn update(&self, _data: &GatewayReady) {
|
||||||
println!("Observed Ready!");
|
println!("Observed Ready!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::errors::GatewayError;
|
||||||
use crate::gateway::events::Events;
|
use crate::gateway::events::Events;
|
||||||
use crate::types;
|
use crate::types;
|
||||||
use crate::types::WebSocketEvent;
|
use crate::types::WebSocketEvent;
|
||||||
|
use async_trait::async_trait;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use futures_util::stream::SplitSink;
|
use futures_util::stream::SplitSink;
|
||||||
|
@ -1667,8 +1668,9 @@ struct HeartbeatThreadCommunication {
|
||||||
/// an Observable. The Observer is notified when the Observable's data changes.
|
/// an Observable. The Observer is notified when the Observable's data changes.
|
||||||
/// In this case, the Observable is a [`GatewayEvent`], which is a wrapper around a WebSocketEvent.
|
/// In this case, the Observable is a [`GatewayEvent`], which is a wrapper around a WebSocketEvent.
|
||||||
/// Note that `Debug` is used to tell `Observer`s apart when unsubscribing.
|
/// Note that `Debug` is used to tell `Observer`s apart when unsubscribing.
|
||||||
|
#[async_trait]
|
||||||
pub trait Observer<T>: Sync + Send + std::fmt::Debug {
|
pub trait Observer<T>: Sync + Send + std::fmt::Debug {
|
||||||
fn update(&self, data: &T);
|
async fn update(&self, data: &T);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// GatewayEvent is a wrapper around a WebSocketEvent. It is used to notify the observers of a
|
/// GatewayEvent is a wrapper around a WebSocketEvent. It is used to notify the observers of a
|
||||||
|
@ -1703,7 +1705,7 @@ impl<T: WebSocketEvent> GatewayEvent<T> {
|
||||||
/// Notifies the observers of the GatewayEvent.
|
/// Notifies the observers of the GatewayEvent.
|
||||||
async fn notify(&self, new_event_data: T) {
|
async fn notify(&self, new_event_data: T) {
|
||||||
for observer in &self.observers {
|
for observer in &self.observers {
|
||||||
observer.update(&new_event_data);
|
observer.update(&new_event_data).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1880,8 +1882,9 @@ mod example {
|
||||||
events_received: AtomicI32,
|
events_received: AtomicI32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
impl Observer<types::GatewayResume> for Consumer {
|
impl Observer<types::GatewayResume> for Consumer {
|
||||||
fn update(&self, _data: &types::GatewayResume) {
|
async fn update(&self, _data: &types::GatewayResume) {
|
||||||
self.events_received.fetch_add(1, Relaxed);
|
self.events_received.fetch_add(1, Relaxed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue