oxide_mvu/effect.rs
1//! Declarative effect system for describing deferred event processing.
2
3#[cfg(feature = "no_std")]
4use alloc::boxed::Box;
5#[cfg(feature = "no_std")]
6use alloc::vec::Vec;
7
8use core::future::Future;
9use core::pin::Pin;
10
11use crate::Emitter;
12use crate::Event as EventTrait;
13
14/// Declarative description of events to be processed.
15///
16/// Effects allow you to describe asynchronous or deferred work that will
17/// produce events. They are returned from [`MvuLogic::init`](crate::MvuLogic::init)
18/// and [`MvuLogic::update`](crate::MvuLogic::update) with the new model state.
19///
20/// # Example
21///
22/// ```rust
23/// use oxide_mvu::Effect;
24///
25/// #[derive(Clone)]
26/// enum Event {
27/// LoadData,
28/// DataLoaded(String),
29/// }
30///
31/// // Trigger a follow-up event
32/// let effect = Effect::just(Event::LoadData);
33///
34/// // Combine multiple effects
35/// let effect = Effect::batch(vec![
36/// Effect::just(Event::LoadData),
37/// Effect::just(Event::DataLoaded("cached".to_string())),
38/// ]);
39///
40/// // No side effects
41/// let effect: Effect<Event> = Effect::none();
42/// ```
43pub struct Effect<Event: EventTrait>(Box<dyn FnOnceBox<Event> + Send>);
44
45impl<Event: EventTrait> Effect<Event> {
46 /// Execute the effect, consuming it and returning a future.
47 ///
48 /// The returned future will be spawned on your async runtime using the provided spawner.
49 pub fn execute(self, emitter: &Emitter<Event>) -> Pin<Box<dyn Future<Output = ()> + Send>> {
50 self.0.call_box(emitter)
51 }
52
53 /// Create an empty effect.
54 ///
55 /// This is private - use [`Effect::none()`] instead.
56 fn new() -> Self {
57 fn empty_fn<Event: EventTrait>(
58 _: &Emitter<Event>,
59 ) -> Pin<Box<dyn Future<Output = ()> + Send>> {
60 Box::pin(async {})
61 }
62 Self(Box::new(empty_fn))
63 }
64
65 /// Create an effect that just emits a single event.
66 ///
67 /// Useful for triggering immediate follow-up events.
68 ///
69 /// # Example
70 ///
71 /// ```rust
72 /// use oxide_mvu::Effect;
73 ///
74 /// #[derive(Clone)]
75 /// enum Event { Refresh }
76 ///
77 /// let effect = Effect::just(Event::Refresh);
78 /// ```
79 pub fn just(event: Event) -> Self {
80 Self(Box::new(move |emitter: &Emitter<Event>| {
81 let emitter = emitter.clone();
82 Box::pin(async move { emitter.emit(event).await })
83 as Pin<Box<dyn Future<Output = ()> + Send>>
84 }))
85 }
86
87 /// Create an empty effect.
88 ///
89 /// Prefer this when semantically indicating "no side effects".
90 ///
91 /// # Example
92 ///
93 /// ```rust
94 /// use oxide_mvu::Effect;
95 ///
96 /// #[derive(Clone)]
97 /// enum Event { Increment }
98 ///
99 /// let effect: Effect<Event> = Effect::none();
100 /// ```
101 pub fn none() -> Self {
102 Self::new()
103 }
104
105 /// Combine multiple effects into a single effect.
106 ///
107 /// All events from all effects will be queued for processing.
108 ///
109 /// # Example
110 ///
111 /// ```rust
112 /// use oxide_mvu::Effect;
113 ///
114 /// #[derive(Clone)]
115 /// enum Event { A, B, C }
116 ///
117 /// let combined = Effect::batch(vec![
118 /// Effect::just(Event::A),
119 /// Effect::just(Event::B),
120 /// Effect::just(Event::C),
121 /// ]);
122 /// ```
123 pub fn batch(effects: Vec<Effect<Event>>) -> Self {
124 Self(Box::new(move |emitter: &Emitter<Event>| {
125 let emitter = emitter.clone();
126 Box::pin(async move {
127 for effect in effects {
128 effect.execute(&emitter).await;
129 }
130 }) as Pin<Box<dyn Future<Output = ()> + Send>>
131 }))
132 }
133
134 /// Create an effect from an async function using a runtime-agnostic spawner.
135 ///
136 /// This allows you to use async/await syntax with any async runtime (tokio,
137 /// async-std, smol, etc.) by providing a spawner function that knows how to
138 /// execute futures on your chosen runtime.
139 ///
140 /// The async function receives a cloned `Emitter` that can be used to emit
141 /// events when the async work completes.
142 ///
143 /// # Arguments
144 ///
145 /// * `spawner` - A function that spawns the future on your async runtime
146 /// * `f` - An async function that receives an Emitter and returns a Future
147 ///
148 /// # Example with tokio
149 ///
150 /// ```rust,no_run
151 /// use oxide_mvu::Effect;
152 /// use std::time::Duration;
153 ///
154 /// #[derive(Clone)]
155 /// enum Event {
156 /// FetchData,
157 /// DataLoaded(String),
158 /// DataFailed(String),
159 /// }
160 ///
161 /// async fn fetch_from_api() -> Result<String, String> {
162 /// // Await some async operation...
163 /// Ok("data from API".to_string())
164 /// }
165 ///
166 /// let effect = Effect::from_async(
167 /// |emitter| async move {
168 /// match fetch_from_api().await {
169 /// Ok(data) => emitter.emit(Event::DataLoaded(data)).await,
170 /// Err(err) => emitter.emit(Event::DataFailed(err)).await,
171 /// }
172 /// }
173 /// );
174 /// ```
175 ///
176 /// # Example with async-std
177 ///
178 /// ```rust,no_run
179 /// use oxide_mvu::Effect;
180 ///
181 /// #[derive(Clone)]
182 /// enum Event { TimerAlert }
183 ///
184 /// let await_timer_effect = Effect::from_async(
185 /// |emitter| async move {
186 /// // Await timer
187 /// emitter.emit(Event::TimerAlert).await;
188 /// }
189 /// );
190 /// ```
191 pub fn from_async<F, Fut>(f: F) -> Self
192 where
193 F: FnOnce(Emitter<Event>) -> Fut + Send + 'static,
194 Fut: Future<Output = ()> + Send + 'static,
195 {
196 Self(Box::new(move |emitter: &Emitter<Event>| {
197 let future = f(emitter.clone());
198 Box::pin(future) as Pin<Box<dyn Future<Output = ()> + Send>>
199 }))
200 }
201}
202
203trait FnOnceBox<Event: EventTrait> {
204 fn call_box(
205 self: Box<Self>,
206 emitter: &Emitter<Event>,
207 ) -> Pin<Box<dyn Future<Output = ()> + Send>>;
208}
209
210impl<F, Event: EventTrait> FnOnceBox<Event> for F
211where
212 F: for<'a> FnOnce(&'a Emitter<Event>) -> Pin<Box<dyn Future<Output = ()> + Send>>,
213{
214 fn call_box(
215 self: Box<Self>,
216 emitter: &Emitter<Event>,
217 ) -> Pin<Box<dyn Future<Output = ()> + Send>> {
218 (*self)(emitter)
219 }
220}