@@ -29,11 +29,17 @@ pub use spin_capabilities::InheritConfiguration;
2929/// dependent component. Finally, the composer will export all exports from the
3030/// dependent component to its dependents. The composer will then encode the
3131/// composition graph into a byte array and return it.
32- pub async fn compose < L : ComponentSourceLoader > (
32+ pub async fn compose <
33+ L : ComponentSourceLoader ,
34+ Fut : std:: future:: Future < Output = Result < Vec < u8 > , ComposeError > > ,
35+ > (
3336 loader : & L ,
3437 component : & L :: Component ,
38+ apply_trigger_deps : impl Fn ( Vec < u8 > ) -> Fut ,
3539) -> Result < Vec < u8 > , ComposeError > {
36- Composer :: new ( loader) . compose ( component) . await
40+ Composer :: new ( loader)
41+ . compose ( component, apply_trigger_deps)
42+ . await
3743}
3844
3945/// A Spin component dependency. This abstracts over the metadata associated with the
@@ -90,8 +96,10 @@ impl DependencyLike for spin_app::locked::LockedComponentDependency {
9096pub trait ComponentSourceLoader {
9197 type Component : ComponentLike < Dependency = Self :: Dependency > ;
9298 type Dependency : DependencyLike ;
99+ type Source ;
93100 async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > ;
94101 async fn load_dependency_source ( & self , source : & Self :: Dependency ) -> anyhow:: Result < Vec < u8 > > ;
102+ async fn load_source ( & self , source : & Self :: Source ) -> anyhow:: Result < Vec < u8 > > ;
95103}
96104
97105/// A ComponentSourceLoader that loads component sources from the filesystem.
@@ -101,6 +109,7 @@ pub struct ComponentSourceLoaderFs;
101109impl ComponentSourceLoader for ComponentSourceLoaderFs {
102110 type Component = spin_app:: locked:: LockedComponent ;
103111 type Dependency = spin_app:: locked:: LockedComponentDependency ;
112+ type Source = spin_app:: locked:: LockedComponentSource ;
104113
105114 async fn load_component_source ( & self , source : & Self :: Component ) -> anyhow:: Result < Vec < u8 > > {
106115 Self :: load_from_locked_source ( & source. source ) . await
@@ -109,6 +118,10 @@ impl ComponentSourceLoader for ComponentSourceLoaderFs {
109118 async fn load_dependency_source ( & self , source : & Self :: Dependency ) -> anyhow:: Result < Vec < u8 > > {
110119 Self :: load_from_locked_source ( & source. source ) . await
111120 }
121+
122+ async fn load_source ( & self , source : & Self :: Source ) -> anyhow:: Result < Vec < u8 > > {
123+ Self :: load_from_locked_source ( source) . await
124+ }
112125}
113126
114127impl ComponentSourceLoaderFs {
@@ -195,39 +208,47 @@ struct Composer<'a, L> {
195208}
196209
197210impl < ' a , L : ComponentSourceLoader > Composer < ' a , L > {
198- async fn compose ( mut self , component : & L :: Component ) -> Result < Vec < u8 > , ComposeError > {
211+ async fn compose < Fut : std:: future:: Future < Output = Result < Vec < u8 > , ComposeError > > > (
212+ mut self ,
213+ component : & L :: Component ,
214+ apply_trigger_deps : impl Fn ( Vec < u8 > ) -> Fut ,
215+ ) -> Result < Vec < u8 > , ComposeError > {
199216 let source = self
200217 . loader
201218 . load_component_source ( component)
202219 . await
203220 . map_err ( ComposeError :: PrepareError ) ?;
204221
205- if component. dependencies ( ) . len ( ) == 0 {
206- return Ok ( source) ;
207- }
222+ let fulfilled_source = if component. dependencies ( ) . len ( ) == 0 {
223+ source
224+ } else {
225+ let ( world_id, instantiation_id) = self
226+ . register_package ( component. id ( ) , None , source)
227+ . map_err ( ComposeError :: PrepareError ) ?;
208228
209- let ( world_id, instantiation_id) = self
210- . register_package ( component. id ( ) , None , source)
211- . map_err ( ComposeError :: PrepareError ) ?;
229+ let prepared = self . prepare_dependencies ( world_id, component) . await ?;
212230
213- let prepared = self . prepare_dependencies ( world_id, component) . await ?;
231+ let arguments = self
232+ . build_instantiation_arguments ( world_id, prepared)
233+ . await ?;
214234
215- let arguments = self
216- . build_instantiation_arguments ( world_id, prepared)
217- . await ?;
235+ for ( argument_name, argument) in arguments {
236+ self . graph
237+ . set_instantiation_argument ( instantiation_id, & argument_name, argument)
238+ . map_err ( |e| ComposeError :: PrepareError ( e. into ( ) ) ) ?;
239+ }
240+
241+ self . export_dependents_exports ( world_id, instantiation_id)
242+ . map_err ( ComposeError :: PrepareError ) ?;
218243
219- for ( argument_name, argument) in arguments {
220244 self . graph
221- . set_instantiation_argument ( instantiation_id , & argument_name , argument )
222- . map_err ( |e| ComposeError :: PrepareError ( e. into ( ) ) ) ?;
223- }
245+ . encode ( Default :: default ( ) )
246+ . map_err ( |e| ComposeError :: EncodeError ( e. into ( ) ) ) ?
247+ } ;
224248
225- self . export_dependents_exports ( world_id, instantiation_id)
226- . map_err ( ComposeError :: PrepareError ) ?;
249+ let with_extras = apply_trigger_deps ( fulfilled_source) . await ?;
227250
228- self . graph
229- . encode ( Default :: default ( ) )
230- . map_err ( |e| ComposeError :: EncodeError ( e. into ( ) ) )
251+ Ok ( with_extras)
231252 }
232253
233254 fn new ( loader : & ' a L ) -> Self {
0 commit comments