crb_superagent/supervisor/
stacker.rs1use super::{Supervisor, SupervisorContext};
2use crb_agent::{Address, Agent, Context, RunAgent};
3use crb_runtime::{InteractiveRuntime, Runtime};
4
5pub struct Stacker<S: Supervisor> {
6 scheduled: Vec<(Box<dyn Runtime>, S::GroupBy)>,
7}
8
9impl<S: Supervisor> Default for Stacker<S> {
10 fn default() -> Self {
11 Self::new()
12 }
13}
14
15impl<S: Supervisor> Stacker<S> {
16 pub fn new() -> Self {
17 Self {
18 scheduled: Vec::new(),
19 }
20 }
21
22 pub fn schedule<A>(&mut self, agent: A, group: S::GroupBy) -> Address<A>
23 where
24 A: Agent,
25 A::Context: Default,
26 {
27 let runtime = RunAgent::<A>::new(agent);
28 let addr = runtime.address();
29 self.scheduled.push((Box::new(runtime), group));
30 addr
31 }
32
33 pub fn spawn_scheduled(&mut self, ctx: &mut Context<S>)
34 where
35 S::Context: SupervisorContext<S>,
36 {
37 let runtimes: Vec<_> = self.scheduled.drain(..).collect();
38 for (runtime, group) in runtimes {
39 ctx.session().spawn_trackable(runtime, group);
40 }
41 }
42}