@@ -39,6 +39,7 @@ use crate::types::task::Task;
3939use crate :: prelude:: * ;
4040use futures:: Stream ;
4141use rdkafka:: producer:: FutureProducer ;
42+ use crate :: types:: flows:: { CFlow , Flow } ;
4243
4344// TODO: not sure static dispatch is better here. Check on using State: 'static.
4445
6465 services : LOTable < usize , Arc < dyn Service < State > > > ,
6566 agents : LOTable < usize , Arc < dyn Agent < State > > > ,
6667 tables : LOTable < String , Arc < CTable < State > > > ,
68+ flows : LOTable < usize , Arc < dyn Service < State > > > ,
6769 table_agents : LOTable < usize , Arc < dyn TableAgent < State > > > ,
6870 routes : LOTable < String , Arc < dyn Router < State > > > ,
6971}
@@ -133,6 +135,7 @@ where
133135 services : LOTable :: default ( ) ,
134136 agents : LOTable :: default ( ) ,
135137 tables : LOTable :: default ( ) ,
138+ flows : LOTable :: default ( ) ,
136139 table_agents : LOTable :: default ( ) ,
137140 routes : LOTable :: default ( ) ,
138141 }
@@ -187,6 +190,12 @@ where
187190 self
188191 }
189192
193+ ///
194+ /// Get state on demand for global wide access.
195+ pub fn get_state ( & mut self ) -> State {
196+ self . state . clone ( )
197+ }
198+
190199 ///
191200 /// By default `callysto-app` is used internally as application name.
192201 /// If you want to change this you can use this method.
@@ -314,12 +323,53 @@ where
314323 self
315324 }
316325
326+ /// Helper to define custom service that skips or uses global shared state.
317327 pub fn service ( & self , s : impl Service < State > ) -> & Self {
318328 let stub = self . stubs . fetch_add ( 1 , Ordering :: AcqRel ) ;
319329 self . services . insert ( stub, Arc :: new ( s) ) ;
320330 self
321331 }
322332
333+ /// Helper to define flow that skips or uses global shared state.
334+ pub fn flow < T : AsRef < str > , F , S , R , Fut > ( & self , name : T , stream : S , clo : F ) -> & Self
335+ where
336+ R : ' static + Send ,
337+ S : Stream + Send + Sync + ' static ,
338+ State : Clone + Send + Sync + ' static ,
339+ F : Send + Sync + ' static + Fn ( & S , Context < State > ) -> Fut ,
340+ Fut : Future < Output = CResult < R > > + Send + ' static ,
341+ {
342+ let stub = self . stubs . fetch_add ( 1 , Ordering :: AcqRel ) ;
343+ let flow = CFlow :: new (
344+ stream,
345+ clo,
346+ self . app_name . clone ( ) ,
347+ name. as_ref ( ) . to_string ( ) ,
348+ self . state . clone ( ) ,
349+ Vec :: default ( ) ,
350+ ) ;
351+ self . flows . insert ( stub, Arc :: new ( flow) ) ;
352+ self
353+ }
354+
355+ /// Helper to define stateful service that uses global application level state.
356+ pub fn stateful_service < T , F , Fut > ( & self , name : T , clo : F , dependencies : Vec < Arc < dyn Service < State > > > ) -> & Self
357+ where
358+ T : AsRef < str > ,
359+ F : Send + Sync + ' static + Fn ( Context < State > ) -> Fut ,
360+ Fut : Future < Output = CResult < State > > + Send + ' static ,
361+ {
362+ let stub = self . stubs . fetch_add ( 1 , Ordering :: AcqRel ) ;
363+ let service = CService :: new (
364+ name,
365+ clo,
366+ self . state . clone ( ) ,
367+ Vec :: default ( ) ,
368+ ) ;
369+ self . services . insert ( stub, Arc :: new ( service) ) ;
370+ self
371+ }
372+
323373 pub fn crontab < C : AsRef < str > > ( & self , cron_expr : C , t : impl Task < State > ) -> & Self {
324374 let stub = self . stubs . fetch_add ( 1 , Ordering :: AcqRel ) ;
325375 let cron_job = Arc :: new ( CronJob :: new ( cron_expr, t) ) ;
0 commit comments