-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from lann/spin-core-refactor
Spin core refactor
- Loading branch information
Showing
67 changed files
with
3,701 additions
and
2,240 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "spin-app" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1.0" | ||
async-trait = "0.1" | ||
ouroboros = "0.15" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
spin-core = { path = "../core" } | ||
thiserror = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::sync::Arc; | ||
|
||
use spin_core::{EngineBuilder, HostComponent, HostComponentsData}; | ||
|
||
use crate::AppComponent; | ||
|
||
/// A trait for "dynamic" Spin host components. | ||
/// | ||
/// This extends [`HostComponent`] to support per-[`AppComponent`] dynamic | ||
/// runtime configuration. | ||
pub trait DynamicHostComponent: HostComponent { | ||
/// Called on [`AppComponent`] instance initialization. | ||
/// | ||
/// The `data` returned by [`HostComponent::build_data`] is passed, along | ||
/// with a reference to the `component` being instantiated. | ||
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()>; | ||
} | ||
|
||
impl<DHC: DynamicHostComponent> DynamicHostComponent for Arc<DHC> { | ||
fn update_data(&self, data: &mut Self::Data, component: &AppComponent) -> anyhow::Result<()> { | ||
(**self).update_data(data, component) | ||
} | ||
} | ||
|
||
type DataUpdater = | ||
Box<dyn Fn(&mut HostComponentsData, &AppComponent) -> anyhow::Result<()> + Send + Sync>; | ||
|
||
#[derive(Default)] | ||
pub struct DynamicHostComponents { | ||
data_updaters: Vec<DataUpdater>, | ||
} | ||
|
||
impl DynamicHostComponents { | ||
pub fn add_dynamic_host_component<T: Send + Sync, DHC: DynamicHostComponent>( | ||
&mut self, | ||
engine_builder: &mut EngineBuilder<T>, | ||
host_component: DHC, | ||
) -> anyhow::Result<()> { | ||
let host_component = Arc::new(host_component); | ||
let handle = engine_builder.add_host_component(host_component.clone())?; | ||
self.data_updaters | ||
.push(Box::new(move |host_components_data, component| { | ||
let data = host_components_data.get_or_insert(handle); | ||
host_component.update_data(data, component) | ||
})); | ||
Ok(()) | ||
} | ||
|
||
pub fn update_data( | ||
&self, | ||
host_components_data: &mut HostComponentsData, | ||
component: &AppComponent, | ||
) -> anyhow::Result<()> { | ||
for data_updater in &self.data_updaters { | ||
data_updater(host_components_data, component)?; | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.