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
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
//! Extension trait providing the `scan_ordered` operator for streams.
//!
//! This operator accumulates state across stream items, emitting intermediate
//! accumulated values. It's similar to `Iterator::fold` but emits a result
//! for each input item rather than just the final result.
//!
//! # Key Characteristics
//!
//! - **Stateful**: Maintains accumulator state across all stream items
//! - **Emits per item**: Produces output for each input (unlike fold which emits once)
//! - **Type transformation**: Can change output type from input type
//! - **Error preserving**: Errors propagate without resetting state
//!
//! # Behavior
//!
//! - **Timestamp Preservation**: Output items preserve the timestamp of their source items
//! - **State Accumulation**: The accumulator state persists across all items
//! - **Error Handling**: Errors are propagated immediately without affecting accumulator state
//! - **Type Transformation**: Can transform input type to different output type
//!
//! # Examples
//!
//! ## Running Sum
//!
//! ```rust
//! use fluxion_stream::ScanOrderedExt;
//! use fluxion_test_utils::{Sequenced, test_channel, unwrap_stream, unwrap_value};
//!
//! # async fn example() -> anyhow::Result<()> {
//! let (tx, stream) = test_channel::<Sequenced<i32>>();
//!
//! // Accumulate sum
//! let mut sums = stream.scan_ordered::<Sequenced<i32>, _, _>(0, |acc, val| {
//! *acc += val;
//! *acc
//! });
//!
//! tx.unbounded_send((10, 1).into())?;
//! tx.unbounded_send((20, 2).into())?;
//! tx.unbounded_send((30, 3).into())?;
//!
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut sums, 500).await)).value, 10); // 0 + 10
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut sums, 500).await)).value, 30); // 10 + 20
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut sums, 500).await)).value, 60); // 30 + 30
//! # Ok(())
//! # }
//! ```
//!
//! ## Type Transformation: Count to String
//!
//! ```rust
//! use fluxion_stream::ScanOrderedExt;
//! use fluxion_test_utils::{Sequenced, test_channel, unwrap_stream, unwrap_value};
//!
//! # async fn example() -> anyhow::Result<()> {
//! let (tx, stream) = test_channel::<Sequenced<String>>();
//!
//! // Count items and produce formatted strings
//! let mut counts = stream.scan_ordered::<Sequenced<String>, _, _>(0i32, |count: &mut i32, _item: &String| {
//! *count += 1;
//! format!("Item #{}", count)
//! });
//!
//! tx.unbounded_send(("apple".to_string(), 1).into())?;
//! tx.unbounded_send(("banana".to_string(), 2).into())?;
//!
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut counts, 500).await)).value, "Item #1");
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut counts, 500).await)).value, "Item #2");
//! # Ok(())
//! # }
//! ```
//!
//! ## Complex State: Building a List
//!
//! ```rust
//! use fluxion_stream::ScanOrderedExt;
//! use fluxion_test_utils::{Sequenced, test_channel, unwrap_stream, unwrap_value};
//!
//! # async fn example() -> anyhow::Result<()> {
//! let (tx, stream) = test_channel::<Sequenced<i32>>();
//!
//! // Collect all values seen so far
//! let mut history = stream.scan_ordered::<Sequenced<Vec<i32>>, _, _>(Vec::<i32>::new(), |list: &mut Vec<i32>, val: &i32| {
//! list.push(*val);
//! list.clone() // Return snapshot
//! });
//!
//! tx.unbounded_send((10, 1).into())?;
//! tx.unbounded_send((20, 2).into())?;
//! tx.unbounded_send((30, 3).into())?;
//!
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut history, 500).await)).value, vec![10]);
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut history, 500).await)).value, vec![10, 20]);
//! assert_eq!(unwrap_value(Some(unwrap_stream(&mut history, 500).await)).value, vec![10, 20, 30]);
//! # Ok(())
//! # }
//! ```
//!
//! # Use Cases
//!
//! - Running totals, averages, or statistics
//! - Building collections or aggregations over time
//! - Counting occurrences or tracking frequencies
//! - State machines with accumulated context
//! - Moving window calculations
//!
//! # Advanced Example: State Machine
//!
//! ```rust
//! use fluxion_stream::ScanOrderedExt;
//! use fluxion_test_utils::{Sequenced, test_channel, unwrap_stream};
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
//! enum Event { Login, Logout, Action(String) }
//!
//! #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
//! struct SessionState {
//! logged_in: bool,
//! action_count: usize,
//! }
//!
//! # async fn example() -> anyhow::Result<()> {
//! let (tx, stream) = test_channel::<Sequenced<Event>>();
//!
//! let mut states = stream.scan_ordered::<Sequenced<SessionState>, _, _>(
//! SessionState { logged_in: false, action_count: 0 },
//! |state, event| {
//! match event {
//! Event::Login => state.logged_in = true,
//! Event::Logout => {
//! state.logged_in = false;
//! state.action_count = 0;
//! }
//! Event::Action(_) if state.logged_in => {
//! state.action_count += 1;
//! }
//! _ => {}
//! }
//! state.clone()
//! }
//! );
//!
//! tx.unbounded_send(Sequenced::new(Event::Login))?;
//! let s1 = unwrap_stream(&mut states, 500).await.unwrap().into_inner();
//! assert!(s1.logged_in && s1.action_count == 0);
//!
//! tx.unbounded_send(Sequenced::new(Event::Action("click".to_string())))?;
//! let s2 = unwrap_stream(&mut states, 500).await.unwrap().into_inner();
//! assert!(s2.logged_in && s2.action_count == 1);
//!
//! tx.unbounded_send(Sequenced::new(Event::Logout))?;
//! let s3 = unwrap_stream(&mut states, 500).await.unwrap().into_inner();
//! assert!(!s3.logged_in && s3.action_count == 0);
//! # Ok(())
//! # }
//! ```
//!
//! # Performance
//!
//! - O(1) time per item (depends on accumulator function)
//! - Stores only the accumulator state (not all items)
//! - Minimal overhead - just function calls and locking
pub use ScanOrderedExt;
pub use ScanOrderedExt;