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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! Stream builder utilities for common async patterns
//!
//! This module provides helper functions for creating common types of streams
//! that can be used with the `Program::subscribe()` method.
use ;
use Duration;
/// Create a stream that emits at regular intervals
///
/// This is useful for creating timers, periodic updates, or any task that needs
/// to run at regular intervals.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::interval_stream;
/// use std::time::Duration;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let mut stream = interval_stream(Duration::from_millis(10));
/// let first = stream.next().await;
/// assert_eq!(first, Some(()));
/// # });
/// ```
/// Create a stream from a channel receiver
///
/// This allows you to bridge between channel-based APIs and the stream-based
/// subscription system.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::channel_stream;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let (tx, rx) = tokio::sync::mpsc::channel(1);
/// let mut stream = channel_stream(rx);
///
/// tx.send(42).await.unwrap();
/// let value = stream.next().await;
/// assert_eq!(value, Some(42));
/// # });
/// ```
+ Send + Unpin
where
T: Send + 'static,
/// Create a stream from an unbounded channel receiver
///
/// Similar to `channel_stream` but for unbounded channels.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::unbounded_channel_stream;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
/// let mut stream = unbounded_channel_stream(rx);
///
/// tx.send(42).unwrap();
/// let value = stream.next().await;
/// assert_eq!(value, Some(42));
/// # });
/// ```
+ Send + Unpin
where
T: Send + 'static,
/// Create a stream that emits once after a delay
///
/// This is useful for delayed actions or one-time timeouts.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::delayed_stream;
/// use std::time::Duration;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let mut stream = delayed_stream(Duration::from_millis(10));
/// let result = stream.next().await;
/// assert_eq!(result, Some(()));
/// # });
/// ```
+ Send + Unpin
/// Create a stream that completes after a delay (alias for delayed_stream)
///
/// This is useful for timeouts or delayed actions.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::timeout_stream;
/// use std::time::Duration;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let mut stream = timeout_stream(Duration::from_millis(10));
/// let result = stream.next().await;
/// assert_eq!(result, Some(()));
/// # });
/// ```
+ Send + Unpin
/// Create a stream that merges multiple streams
///
/// This allows you to combine multiple event sources into a single stream.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::{interval_stream, merge_streams};
/// use std::time::Duration;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let stream1 = interval_stream(Duration::from_millis(10)).take(2).map(|_| "fast");
/// let stream2 = interval_stream(Duration::from_millis(15)).take(1).map(|_| "slow");
///
/// let merged = merge_streams(vec![
/// Box::new(stream1) as Box<dyn futures::Stream<Item = &str> + Send + Unpin>,
/// Box::new(stream2) as Box<dyn futures::Stream<Item = &str> + Send + Unpin>,
/// ]);
/// let results: Vec<_> = merged.collect().await;
/// assert_eq!(results.len(), 3);
/// # });
/// ```
/// Create a stream from a watch channel
///
/// This creates a stream that emits whenever the watched value changes.
///
/// # Example
/// ```
/// use hojicha_runtime::stream_builders::watch_stream;
/// use std::time::Duration;
/// use futures::StreamExt;
///
/// # tokio_test::block_on(async {
/// let (tx, rx) = tokio::sync::watch::channel(0);
///
/// // Update the watched value
/// tokio::spawn(async move {
/// tokio::time::sleep(Duration::from_millis(5)).await;
/// tx.send(42).ok();
/// });
///
/// let mut stream = watch_stream(rx).take(1);
/// let value = stream.next().await;
/// assert_eq!(value, Some(42));
/// # });
/// ```
+ Send + Unpin
where
T: Clone + Send + Sync + 'static,