pub trait AsyncChildContext:
Send
+ Sync
+ Debug {
// Required methods
fn parent_id(&self) -> &str;
fn emit_output(&self, message: &str);
fn emit_output_with_level(&self, message: &str, level: &str);
fn spawn_child<'life0, 'async_trait>(
&'life0 self,
config: ChildConfig,
) -> Pin<Box<dyn Future<Output = Result<Box<dyn AsyncChildHandle>, SpawnError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn child_count(&self) -> usize;
fn max_children(&self) -> usize;
fn send_to_child(
&self,
child_id: &str,
input: Value,
) -> Result<ChildResult, RunError>;
fn clone_box(&self) -> Box<dyn AsyncChildContext>;
// Provided method
fn send_to_child_async(
&self,
_child_id: &str,
_input: Value,
) -> Result<(), RunError> { ... }
}Expand description
Async context provided to Children for runtime interaction.
This trait provides async versions of ChildContext methods
for children that need to spawn async children.
§Example
ⓘ
#[async_trait]
impl AsyncRunnableChild for MyWorker {
async fn run(&mut self, input: Value) -> ChildResult {
if let Some(ctx) = &self.async_ctx {
ctx.emit_output("Starting async work...");
// Spawn an async child
let config = ChildConfig::from_inline("sub-1", "...");
if let Ok(mut handle) = ctx.spawn_child(config).await {
let result = handle.run(input.clone()).await;
// ...
}
}
ChildResult::Ok(input)
}
}Required Methods§
Sourcefn parent_id(&self) -> &str
fn parent_id(&self) -> &str
Returns the parent’s ID (Component or Child that owns this context).
Sourcefn emit_output(&self, message: &str)
fn emit_output(&self, message: &str)
Emits output to the parent (displayed to user via IO).
Sourcefn emit_output_with_level(&self, message: &str, level: &str)
fn emit_output_with_level(&self, message: &str, level: &str)
Emits output with a specific level.
Sourcefn spawn_child<'life0, 'async_trait>(
&'life0 self,
config: ChildConfig,
) -> Pin<Box<dyn Future<Output = Result<Box<dyn AsyncChildHandle>, SpawnError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn spawn_child<'life0, 'async_trait>(
&'life0 self,
config: ChildConfig,
) -> Pin<Box<dyn Future<Output = Result<Box<dyn AsyncChildHandle>, SpawnError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Spawns a child and returns an async handle to control it.
§Arguments
config- Configuration for the child
§Returns
An async handle to the spawned child, or an error if spawn failed.
§Errors
SpawnError::MaxChildrenReachedif limit exceededSpawnError::ScriptNotFoundif script file doesn’t existSpawnError::InvalidScriptif script is malformedSpawnError::AlreadyExistsif ID is already in use
Sourcefn child_count(&self) -> usize
fn child_count(&self) -> usize
Returns the number of active children.
Sourcefn max_children(&self) -> usize
fn max_children(&self) -> usize
Returns the maximum allowed children.
Sourcefn send_to_child(
&self,
child_id: &str,
input: Value,
) -> Result<ChildResult, RunError>
fn send_to_child( &self, child_id: &str, input: Value, ) -> Result<ChildResult, RunError>
Sends input to a child by ID and returns its result.
§Arguments
child_id- The child’s IDinput- Input data to pass to the child
Sourcefn clone_box(&self) -> Box<dyn AsyncChildContext>
fn clone_box(&self) -> Box<dyn AsyncChildContext>
Clones this context into a boxed trait object.