1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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(())
    }
}