jellyflow_runtime/runtime/
middleware.rs1use crate::profile::ApplyPipelineError;
6use crate::runtime::commit::NodeGraphPatch;
7use crate::runtime::events::NodeGraphStoreSnapshot;
8use jellyflow_core::ops::GraphTransaction;
9
10pub trait NodeGraphStoreMiddleware: 'static {
11 fn before_dispatch(
12 &mut self,
13 _snapshot: NodeGraphStoreSnapshot<'_>,
14 _tx: &mut GraphTransaction,
15 ) -> Result<(), ApplyPipelineError> {
16 Ok(())
17 }
18
19 fn after_dispatch(&mut self, _snapshot: NodeGraphStoreSnapshot<'_>, _patch: &NodeGraphPatch) {}
20}
21
22#[derive(Debug, Default, Clone, Copy)]
23pub struct NoopNodeGraphStoreMiddleware;
24
25impl NodeGraphStoreMiddleware for NoopNodeGraphStoreMiddleware {}
26
27#[derive(Debug, Clone)]
28pub struct NodeGraphStoreMiddlewareChain<A, B> {
29 pub first: A,
30 pub second: B,
31}
32
33impl<A, B> NodeGraphStoreMiddlewareChain<A, B> {
34 pub fn new(first: A, second: B) -> Self {
35 Self { first, second }
36 }
37}
38
39impl<A, B> NodeGraphStoreMiddleware for NodeGraphStoreMiddlewareChain<A, B>
40where
41 A: NodeGraphStoreMiddleware,
42 B: NodeGraphStoreMiddleware,
43{
44 fn before_dispatch(
45 &mut self,
46 snapshot: NodeGraphStoreSnapshot<'_>,
47 tx: &mut GraphTransaction,
48 ) -> Result<(), ApplyPipelineError> {
49 self.first.before_dispatch(snapshot, tx)?;
50 self.second.before_dispatch(snapshot, tx)?;
51 Ok(())
52 }
53
54 fn after_dispatch(&mut self, snapshot: NodeGraphStoreSnapshot<'_>, patch: &NodeGraphPatch) {
55 self.first.after_dispatch(snapshot, patch);
56 self.second.after_dispatch(snapshot, patch);
57 }
58}