Fix serialization.

This commit is contained in:
Paul Makles
2020-04-13 16:29:48 +01:00
parent 577f25642e
commit 0f793f84a2
7 changed files with 83 additions and 31 deletions

View File

@@ -1,3 +1,7 @@
use once_cell::sync::OnceCell;
use std::sync::mpsc::{channel, Sender};
use std::thread;
pub mod events;
pub mod pubsub;
pub mod state;
@@ -11,11 +15,50 @@ pub fn send_message<U: Into<Option<Vec<String>>>, G: Into<Option<String>>>(
let users = users.into();
let guild = guild.into();
if pubsub::send_message(users.clone(), guild.clone(), data) {
state::send_message(users, guild, "bruh".to_string());
if pubsub::send_message(users.clone(), guild.clone(), data.clone()) {
state::send_message(users, guild, data.serialize());
true
} else {
false
}
}
struct NotificationArguments {
users: Option<Vec<String>>,
guild: Option<String>,
data: events::Notification,
}
static mut SENDER: OnceCell<Sender<NotificationArguments>> = OnceCell::new();
pub fn start_worker() {
let (sender, receiver) = channel();
unsafe {
SENDER.set(sender).unwrap();
}
thread::spawn(move || {
while let Ok(data) = receiver.recv() {
send_message(data.users, data.guild, data.data);
}
});
}
pub fn send_message_threaded<U: Into<Option<Vec<String>>>, G: Into<Option<String>>>(
users: U,
guild: G,
data: events::Notification,
) -> bool {
unsafe {
SENDER
.get()
.unwrap()
.send(NotificationArguments {
users: users.into(),
guild: guild.into(),
data,
})
.is_ok()
}
}