pub struct MessageDebouncer { /* private fields */ }Available on non-WebAssembly only.
Expand description
Message debouncer that delays and coalesces rapid notifications.
Implementations§
Source§impl MessageDebouncer
impl MessageDebouncer
Sourcepub fn new(config: DebouncingConfig) -> Self
pub fn new(config: DebouncingConfig) -> Self
Create a new message debouncer.
§Examples
use pmcp::utils::{MessageDebouncer, DebouncingConfig};
use std::time::Duration;
use std::collections::HashMap;
// Default configuration
let debouncer = MessageDebouncer::new(DebouncingConfig::default());
// Custom configuration with per-method settings
let mut config = DebouncingConfig {
wait_time: Duration::from_millis(100),
debounced_methods: HashMap::new(),
};
// Different debounce times for different notification types
config.debounced_methods.insert(
"progress.update".to_string(),
Duration::from_millis(50)
);
config.debounced_methods.insert(
"file.changed".to_string(),
Duration::from_millis(500)
);
let custom_debouncer = MessageDebouncer::new(config);Sourcepub async fn add(&self, key: String, notification: Notification) -> Result<()>
pub async fn add(&self, key: String, notification: Notification) -> Result<()>
Add a notification to be debounced.
§Examples
use pmcp::utils::{MessageDebouncer, DebouncingConfig};
use pmcp::types::{Notification, ServerNotification};
use std::time::Duration;
let debouncer = MessageDebouncer::new(DebouncingConfig {
wait_time: Duration::from_millis(100),
debounced_methods: Default::default(),
});
// Rapid file change notifications
debouncer.add(
"file:/path/to/file.rs".to_string(),
Notification::Server(ServerNotification::ResourcesChanged)
).await?;
// Another change to same file within debounce window
tokio::time::sleep(Duration::from_millis(50)).await;
debouncer.add(
"file:/path/to/file.rs".to_string(),
Notification::Server(ServerNotification::ResourcesChanged)
).await?;
// Only the last notification will be sent after debounce periodSourcepub async fn receive(&self) -> Option<Notification>
pub async fn receive(&self) -> Option<Notification>
Receive the next debounced notification.
§Examples
use pmcp::utils::{MessageDebouncer, DebouncingConfig};
use pmcp::types::{Notification, ServerNotification};
use std::time::Duration;
let debouncer = MessageDebouncer::new(DebouncingConfig::default());
// Spawn a task to receive notifications
tokio::spawn(async move {
while let Some(notification) = debouncer.receive().await {
match notification {
Notification::Server(ServerNotification::ResourcesChanged) => {
println!("Resources changed (after debounce)");
}
_ => {}
}
}
});Sourcepub async fn flush(&self) -> Vec<Notification>
pub async fn flush(&self) -> Vec<Notification>
Flush all pending notifications immediately.
§Examples
use pmcp::utils::{MessageDebouncer, DebouncingConfig};
use pmcp::types::{Notification, ServerNotification, ClientNotification};
use std::time::Duration;
let debouncer = MessageDebouncer::new(DebouncingConfig {
wait_time: Duration::from_secs(5), // Long debounce
debounced_methods: Default::default(),
});
// Add several notifications
debouncer.add(
"resource1".to_string(),
Notification::Server(ServerNotification::ResourcesChanged)
).await?;
debouncer.add(
"resource2".to_string(),
Notification::Client(ClientNotification::RootsListChanged)
).await?;
// Flush immediately instead of waiting for debounce
let pending = debouncer.flush().await;
println!("Flushed {} pending notifications", pending.len());
// Process all flushed notifications
for notification in pending {
// Handle each notification immediately
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for MessageDebouncer
impl !RefUnwindSafe for MessageDebouncer
impl Send for MessageDebouncer
impl Sync for MessageDebouncer
impl Unpin for MessageDebouncer
impl UnsafeUnpin for MessageDebouncer
impl !UnwindSafe for MessageDebouncer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more