plexus_core/activations/echo/
activation.rs1use super::types::EchoEvent;
14use async_stream::stream;
15use futures::Stream;
16use std::time::Duration;
17
18#[derive(Clone)]
20pub struct Echo;
21
22impl Echo {
23 pub fn new() -> Self {
24 Echo
25 }
26}
27
28impl Default for Echo {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34#[plexus_macros::activation(
40 namespace = "echo",
41 version = "1.0.0",
42 description = "Echo messages back - demonstrates hub-macro usage"
43)]
44#[allow(deprecated)]
45impl Echo {
46 #[plexus_macros::method(
48 description = "Echo a message back the specified number of times",
49 params(
50 message = "The message to echo",
51 count = "Number of times to repeat (default: 1)"
52 )
53 )]
54 async fn echo(
55 &self,
56 message: String,
57 count: u32,
58 ) -> impl Stream<Item = EchoEvent> + Send + 'static {
59 let count = if count == 0 { 1 } else { count };
60 stream! {
61 for i in 0..count {
62 if i > 0 {
63 tokio::time::sleep(Duration::from_millis(500)).await;
64 }
65 yield EchoEvent::Echo {
66 message: message.clone(),
67 count: i + 1,
68 };
69 }
70 }
71 }
72
73 #[plexus_macros::method(
75 description = "Echo a message once",
76 params(message = "The message to echo")
77 )]
78 async fn once(&self, message: String) -> impl Stream<Item = EchoEvent> + Send + 'static {
79 stream! {
80 yield EchoEvent::Echo {
81 message,
82 count: 1,
83 };
84 }
85 }
86}