use datum::ReplyPort;
use crate::Revision;
type SideEffect<S> = dyn FnOnce(&S, Revision) + Send + 'static;
pub(crate) enum MainEffect<C, S> {
Persist(S),
Delete,
None,
Unhandled,
Stash(C),
}
pub(crate) enum ChainedEffect<S> {
Run(Box<SideEffect<S>>),
Stop,
UnstashAll,
}
#[must_use]
pub struct Effect<C, S> {
pub(crate) main: MainEffect<C, S>,
pub(crate) chained: Vec<ChainedEffect<S>>,
}
impl<C, S> Effect<C, S> {
pub fn persist(new_state: S) -> Self {
Self {
main: MainEffect::Persist(new_state),
chained: Vec::new(),
}
}
pub fn delete() -> Self {
Self {
main: MainEffect::Delete,
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, S>
where
R: Send + 'static,
{
Self::none().then_reply(reply_to, move |_| reply)
}
pub fn no_reply() -> ReplyEffect<C, S> {
Self::none().then_no_reply()
}
pub fn then_run<F>(mut self, callback: F) -> Self
where
F: FnOnce(&S) + Send + 'static,
{
self.chained
.push(ChainedEffect::Run(Box::new(move |state, _revision| {
callback(state)
})));
self
}
pub fn then_run_with_revision<F>(mut self, callback: F) -> Self
where
F: FnOnce(&S, Revision) + 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, S>
where
R: Send + 'static,
F: FnOnce(&S) -> R + Send + 'static,
{
self.chained
.push(ChainedEffect::Run(Box::new(move |state, _revision| {
let _ = reply_to.send(reply(state));
})));
ReplyEffect(self)
}
pub fn then_reply_with_revision<R, F>(
mut self,
reply_to: ReplyPort<R>,
reply: F,
) -> ReplyEffect<C, S>
where
R: Send + 'static,
F: FnOnce(&S, Revision) -> R + Send + 'static,
{
self.chained
.push(ChainedEffect::Run(Box::new(move |state, revision| {
let _ = reply_to.send(reply(state, revision));
})));
ReplyEffect(self)
}
pub fn then_no_reply(self) -> ReplyEffect<C, S> {
ReplyEffect(self)
}
}
#[must_use]
pub struct ReplyEffect<C, S>(Effect<C, S>);
impl<C, S> ReplyEffect<C, 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_run_with_revision<F>(self, callback: F) -> Self
where
F: FnOnce(&S, Revision) + Send + 'static,
{
Self(self.0.then_run_with_revision(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, S> {
self.0
}
}