Skip to content

Commit

Permalink
factors-executor: Add ComponentLoader::load_instance_pre
Browse files Browse the repository at this point in the history
This lets embedders customize more of the component loading process.

Signed-off-by: Lann Martin <lann.martin@fermyon.com>
  • Loading branch information
lann committed Mar 4, 2025
1 parent 0126b61 commit 90c7a5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl App {
}

/// Returns an iterator of [`AppComponent`]s defined for this app.
pub fn components(&self) -> impl Iterator<Item = AppComponent<'_>> {
pub fn components(&self) -> impl ExactSizeIterator<Item = AppComponent<'_>> {
self.locked
.components
.iter()
Expand Down
25 changes: 17 additions & 8 deletions crates/factors-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
hooks.configure_app(&configured_app).await?;
}

let mut component_instance_pres = HashMap::new();
let components = configured_app.app().components();
let mut component_instance_pres = HashMap::with_capacity(components.len());

for app_component in configured_app.app().components() {
let component = component_loader
.load_component(self.core_engine.as_ref(), &app_component)
for component in components {
let instance_pre = component_loader
.load_instance_pre(&self.core_engine, &component)
.await?;
let instance_pre = self.core_engine.instantiate_pre(&component)?;

component_instance_pres.insert(app_component.id().to_string(), instance_pre);
component_instance_pres.insert(component.id().to_string(), instance_pre);
}

Ok(FactorsExecutorApp {
Expand Down Expand Up @@ -102,13 +101,23 @@ where

/// A ComponentLoader is responsible for loading Wasmtime [`Component`]s.
#[async_trait]
pub trait ComponentLoader {
pub trait ComponentLoader: Sync {
/// Loads a [`Component`] for the given [`AppComponent`].
async fn load_component(
&self,
engine: &spin_core::wasmtime::Engine,
component: &AppComponent,
) -> anyhow::Result<Component>;

/// Loads [`InstancePre`] for the given [`AppComponent`].
async fn load_instance_pre<T>(
&self,
engine: &spin_core::Engine<T>,
component: &AppComponent,
) -> anyhow::Result<spin_core::InstancePre<T>> {
let component = self.load_component(engine.as_ref(), component).await?;
Ok(engine.instantiate_pre(&component)?)
}
}

type InstancePre<T, U> =
Expand Down

0 comments on commit 90c7a5d

Please sign in to comment.