1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![allow(dead_code)]
use chrono::{DateTime, TimeZone, Utc};
use crate::Dispatchable;
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct DispatchedEvent {
id: String,
created_at: i64,
data: String,
event: String,
}
impl DispatchedEvent {
pub(crate) fn new(data: String, event: String) -> Self {
Self {
id: ulid::Ulid::new().to_string().to_lowercase(),
created_at: chrono::Utc::now().timestamp(),
data,
event,
}
}
pub fn id(&self) -> String {
self.id.clone()
}
pub fn created_at(&self) -> DateTime<Utc> {
Utc.timestamp_opt(self.created_at, 0).unwrap()
}
pub(crate) fn event(&self) -> String {
self.event.clone()
}
/// Returns the actual instance of the event
/// ```
/// # use async_trait::async_trait;
/// # use orsomafo::{Dispatchable, DispatchedEvent, EventDispatcherBuilder, EventHandler};
/// # use tokio::time::{sleep, Duration};
///
/// # #[tokio::main]
/// # async fn main() {
/// # _ = EventDispatcherBuilder::new().build().await;
///
/// #[derive(Clone, serde::Serialize, serde::Deserialize)]
/// struct MyEvent;
/// impl Dispatchable for MyEvent {}
///
/// struct MyEventHandler;
///
/// #[orsomafo::async_trait]
/// impl EventHandler for MyEventHandler {
/// async fn handle(&self, dispatched: &DispatchedEvent) {
/// let event: MyEvent = dispatched.the_event().unwrap();
/// // or
/// // let event = dispatched.the_event::<MyEvent>().unwrap()
/// //...
/// }
/// }
///
/// }
/// ```
pub fn the_event<T: Dispatchable>(&self) -> Option<T> {
serde_json::from_str(&self.data).ok()
}
}