use std::{ mem, sync::{Arc, Mutex}, }; use web_push::{WebPushClient, WebPushError, WebPushMessage}; #[derive(Clone)] pub struct Client { sent: Arc>>, } 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 { let mut sent = self.sent.lock().unwrap(); mem::replace(&mut *sent, Vec::new()) } } #[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(()) } }