pub trait ComponentRunnable:
Send
+ Sync
+ Inputs
+ Outputs
+ 'static {
type Global: Send + Sync;
// Required method
fn run<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 mut Ctx<Self::Global>,
) -> Pin<Box<dyn Future<Output = Result<Next>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
Define the function that will excuted when a Component run
Global define tha data that this component can be access, the data is like a global state of Flow that any component can be read or write
A Flow hava a unique Global type, what means that only component with the same Self::Global can be use for contruct the flow.
§Examples
use rs_flow::prelude::*;
struct GlobalA;
struct GlobalB;
#[inputs]
#[outputs]
struct ComponentA;
#[async_trait]
impl ComponentRunnable for ComponentA {
type Global = GlobalA;
async fn run(&self, ctx: &mut Ctx<Self::Global>) -> Result<Next> {
Ok(Next::Continue)
}
}
#[inputs]
#[outputs]
struct ComponentB;
#[async_trait]
impl ComponentRunnable for ComponentB {
type Global = GlobalB;
async fn run(&self, ctx: &mut Ctx<Self::Global>) -> Result<Next> {
Ok(Next::Continue)
}
}
let mut flow = Flow::new();
flow = flow.add_component(Component::new(1, ComponentA)).unwrap();
// flow = flow.add_component(Component::new(2, ComponentB)).unwrap();
// Will fail because ComponentA and ComponentB not have same Global