use std::{future::Future, pin::Pin};
use datum::ReplyPort;
use crate::SerializedPayload;
pub(crate) type BoxEffectFuture<C, E, S> =
Pin<Box<dyn Future<Output = Effect<C, E, S>> + Send + 'static>>;
pub(crate) struct EventWithMetadata<E> {
pub(crate) event: E,
pub(crate) metadata: Option<SerializedPayload>,
}
pub(crate) enum MainEffect<C, E, S> {
Persist(Vec<EventWithMetadata<E>>),
None,
Unhandled,
Stash(C),
Async(BoxEffectFuture<C, E, S>),
}
pub(crate) enum ChainedEffect<S> {
Run(Box<dyn FnOnce(&S) + Send + 'static>),
Stop,
UnstashAll,
}
#[must_use]
pub struct Effect<C, E, S> {
pub(crate) main: MainEffect<C, E, S>,
pub(crate) chained: Vec<ChainedEffect<S>>,
}
impl<C, E, S> Effect<C, E, S> {
pub fn persist(event: E) -> Self {
Self::persist_with_metadata(event, None)
}
pub fn persist_with_metadata(event: E, metadata: Option<SerializedPayload>) -> Self {
Self {
main: MainEffect::Persist(vec![EventWithMetadata { event, metadata }]),
chained: Vec::new(),
}
}
pub fn persist_all(events: impl IntoIterator<Item = E>) -> Self {
Self::persist_all_with_metadata(events.into_iter().map(|event| (event, None)))
}
pub fn persist_all_with_metadata(
events: impl IntoIterator<Item = (E, Option<SerializedPayload>)>,
) -> Self {
Self {
main: MainEffect::Persist(
events
.into_iter()
.map(|(event, metadata)| EventWithMetadata { event, metadata })
.collect(),
),
chained: Vec::new(),
}
}
pub fn none() -> Self {
Self {
main: MainEffect::None,
chained: Vec::new(),
}
}
pub fn unhandled() -> Self {
Self {
main: MainEffect::Unhandled,
chained: Vec::new(),
}
}
pub fn stop() -> Self {
Self::none().then_stop()
}
pub fn stash(command: C) -> Self {
Self {
main: MainEffect::Stash(command),
chained: Vec::new(),
}
}
pub fn unstash_all() -> Self {
Self::none().then_unstash_all()
}
pub fn reply<R>(reply_to: ReplyPort<R>, reply: R) -> ReplyEffect<C, E, S>
where
R: Send + 'static,
{
Self::none().then_reply(reply_to, move |_| reply)
}
pub fn no_reply() -> ReplyEffect<C, E, S> {
Self::none().then_no_reply()
}
pub fn async_effect<F>(future: F) -> Self
where
F: Future<Output = Self> + Send + 'static,
{
Self {
main: MainEffect::Async(Box::pin(future)),
chained: Vec::new(),
}
}
pub fn async_reply<F>(future: F) -> ReplyEffect<C, E, S>
where
C: Send + 'static,
E: Send + 'static,
S: Send + 'static,
F: Future<Output = ReplyEffect<C, E, S>> + Send + 'static,
{
ReplyEffect(Self::async_effect(
async move { future.await.into_effect() },
))
}
pub fn then_run<F>(mut self, callback: F) -> Self
where
F: FnOnce(&S) + Send + 'static,
{
self.chained.push(ChainedEffect::Run(Box::new(callback)));
self
}
pub fn then_stop(mut self) -> Self {
self.chained.push(ChainedEffect::Stop);
self
}
pub fn then_unstash_all(mut self) -> Self {
self.chained.push(ChainedEffect::UnstashAll);
self
}
pub fn then_reply<R, F>(mut self, reply_to: ReplyPort<R>, reply: F) -> ReplyEffect<C, E, S>
where
R: Send + 'static,
F: FnOnce(&S) -> R + Send + 'static,
{
self.chained.push(ChainedEffect::Run(Box::new(move |state| {
let _ = reply_to.send(reply(state));
})));
ReplyEffect(self)
}
pub fn then_no_reply(self) -> ReplyEffect<C, E, S> {
ReplyEffect(self)
}
}
#[must_use]
pub struct ReplyEffect<C, E, S>(Effect<C, E, S>);
impl<C, E, S> ReplyEffect<C, E, S> {
pub fn then_run<F>(self, callback: F) -> Self
where
F: FnOnce(&S) + Send + 'static,
{
Self(self.0.then_run(callback))
}
pub fn then_stop(self) -> Self {
Self(self.0.then_stop())
}
pub fn then_unstash_all(self) -> Self {
Self(self.0.then_unstash_all())
}
pub(crate) fn into_effect(self) -> Effect<C, E, S> {
self.0
}
}