use super::types::EchoEvent;
use async_stream::stream;
use futures::Stream;
use std::time::Duration;
#[derive(Clone)]
pub struct Echo;
impl Echo {
pub fn new() -> Self {
Echo
}
}
impl Default for Echo {
fn default() -> Self {
Self::new()
}
}
#[plexus_macros::activation(
namespace = "echo",
version = "1.0.0",
description = "Echo messages back - demonstrates hub-macro usage"
)]
#[allow(deprecated)]
impl Echo {
#[plexus_macros::method(
description = "Echo a message back the specified number of times",
params(
message = "The message to echo",
count = "Number of times to repeat (default: 1)"
)
)]
async fn echo(
&self,
message: String,
count: u32,
) -> impl Stream<Item = EchoEvent> + Send + 'static {
let count = if count == 0 { 1 } else { count };
stream! {
for i in 0..count {
if i > 0 {
tokio::time::sleep(Duration::from_millis(500)).await;
}
yield EchoEvent::Echo {
message: message.clone(),
count: i + 1,
};
}
}
}
#[plexus_macros::method(
description = "Echo a message once",
params(message = "The message to echo")
)]
async fn once(&self, message: String) -> impl Stream<Item = EchoEvent> + Send + 'static {
stream! {
yield EchoEvent::Echo {
message,
count: 1,
};
}
}
}