blob: a611ad02f39e984557304e650a93b65cdf0cbe7c (
plain)
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
|
use std::{
mem,
sync::{Arc, Mutex},
};
use web_push::{WebPushClient, WebPushError, WebPushMessage};
#[derive(Clone)]
pub struct Client {
sent: Arc<Mutex<Vec<WebPushMessage>>>,
}
impl Client {
pub fn new() -> Self {
Self {
sent: Arc::default(),
}
}
// Clears the list of sent messages (for all clones of this Client) when called, because we
// can't clone `WebPushMessage`s so we either need to move them or try to reconstruct them,
// either of which sucks but moving them sucks less.
pub fn sent(&self) -> Vec<WebPushMessage> {
let mut sent = self.sent.lock().unwrap();
mem::take(&mut *sent)
}
}
#[async_trait::async_trait]
impl WebPushClient for Client {
async fn send(&self, message: WebPushMessage) -> Result<(), WebPushError> {
let mut sent = self.sent.lock().unwrap();
sent.push(message);
Ok(())
}
}
|