use std::future::Future;
use navree_kernel::BoxFuture;
use crate::error::Result;
use crate::exec::ExecContext;
use crate::hooks::{Flow, HookKind, ModelEvent, ModelHook};
use crate::model::Model;
use crate::record::Record;
pub trait Observer<M: Model>: Send + Sync + 'static {
const IN_TRANSACTION: bool = false;
fn retrieved(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn saving(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn creating(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn created(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn updating(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn updated(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn saved(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn deleting(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn deleted(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn trashed(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn force_deleting(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn force_deleted(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn restoring(
&self,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (record, ctx);
async { Ok(Flow::Continue) }
}
fn restored(
&self,
record: &Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (record, ctx);
async { Ok(()) }
}
fn replicating(
&self,
copy: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<()>> + Send {
let _ = (copy, ctx);
async { Ok(()) }
}
fn custom(
&self,
event: &'static str,
record: &mut Record<M>,
ctx: &ExecContext,
) -> impl Future<Output = Result<Flow>> + Send {
let _ = (event, record, ctx);
async { Ok(Flow::Continue) }
}
}
pub struct ObserverHook<O>(O);
impl<O> ObserverHook<O> {
pub fn new(observer: O) -> Self {
ObserverHook(observer)
}
pub fn observer(&self) -> &O {
&self.0
}
}
impl<M: Model, O: Observer<M>> ModelHook<M> for ObserverHook<O> {
fn before<'a>(
&'a self,
event: ModelEvent,
ctx: &'a ExecContext,
record: &'a mut Record<M>,
) -> BoxFuture<'a, Result<Flow>> {
let observer = &self.0;
match event {
ModelEvent::Saving => Box::pin(observer.saving(record, ctx)),
ModelEvent::Creating => Box::pin(observer.creating(record, ctx)),
ModelEvent::Updating => Box::pin(observer.updating(record, ctx)),
ModelEvent::Deleting => Box::pin(observer.deleting(record, ctx)),
ModelEvent::ForceDeleting => Box::pin(observer.force_deleting(record, ctx)),
ModelEvent::Restoring => Box::pin(observer.restoring(record, ctx)),
ModelEvent::Custom(name) => Box::pin(observer.custom(name, record, ctx)),
_ => Box::pin(async { Ok(Flow::Continue) }),
}
}
fn after<'a>(
&'a self,
event: ModelEvent,
ctx: &'a ExecContext,
record: &'a mut Record<M>,
) -> BoxFuture<'a, Result<()>> {
let observer = &self.0;
match event {
ModelEvent::Retrieved => Box::pin(observer.retrieved(record, ctx)),
ModelEvent::Created => Box::pin(observer.created(record, ctx)),
ModelEvent::Updated => Box::pin(observer.updated(record, ctx)),
ModelEvent::Saved => Box::pin(observer.saved(record, ctx)),
ModelEvent::Deleted => Box::pin(observer.deleted(record, ctx)),
ModelEvent::Trashed => Box::pin(observer.trashed(record, ctx)),
ModelEvent::ForceDeleted => Box::pin(observer.force_deleted(record, ctx)),
ModelEvent::Restored => Box::pin(observer.restored(record, ctx)),
ModelEvent::Replicating => Box::pin(observer.replicating(record, ctx)),
_ => Box::pin(async { Ok(()) }),
}
}
fn kind(&self) -> HookKind {
if O::IN_TRANSACTION {
HookKind::TransactionalListener
} else {
HookKind::Listener
}
}
}