use std::future::Future;
use crate::effect::sink::SinkEffect;
use crate::effect::Effect;
pub struct TapEmit<E, F> {
pub(crate) inner: E,
pub(crate) f: F,
}
impl<E, F> std::fmt::Debug for TapEmit<E, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TapEmit")
.field("inner", &"<effect>")
.field("f", &"<function>")
.finish()
}
}
impl<E, F> Effect for TapEmit<E, F>
where
E: SinkEffect,
E::Output: Clone + Send,
F: FnOnce(&E::Output) -> E::Item + Send,
{
type Output = E::Output;
type Error = E::Error;
type Env = E::Env;
async fn run(self, env: &Self::Env) -> Result<Self::Output, Self::Error> {
self.inner.run(env).await
}
}
impl<E, F> SinkEffect for TapEmit<E, F>
where
E: SinkEffect,
E::Output: Clone + Send,
F: FnOnce(&E::Output) -> E::Item + Send,
{
type Item = E::Item;
async fn run_with_sink<S, Fut>(
self,
env: &Self::Env,
sink: S,
) -> Result<Self::Output, Self::Error>
where
S: Fn(Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = ()> + Send,
{
let value = self.inner.run_with_sink(env, &sink).await?;
let derived = (self.f)(&value);
sink(derived).await;
Ok(value)
}
}