use anyhow::Result; use turbo_rcstr::{RcStr, rcstr}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; use turbopack_core::introspect::{Introspectable, IntrospectableChildren}; use super::{ ContentSource, route_tree::{RouteTree, RouteTrees}, }; use crate::source::ContentSources; /// Combines multiple [ContentSource]s by trying all content sources in order. /// /// The content source which responds with the most specific response (that is /// not a [ContentSourceContent::NotFound]) will be returned. #[turbo_tasks::value(shared)] pub struct CombinedContentSource { pub sources: Vec>>, } impl CombinedContentSource { pub fn new(sources: Vec>>) -> Vc { CombinedContentSource { sources }.cell() } } #[turbo_tasks::value_impl] impl ContentSource for CombinedContentSource { #[turbo_tasks::function] async fn get_routes(&self) -> Result> { let all_routes = self .sources .iter() .map(|s| async move { s.get_routes().to_resolved().await }) .try_join() .await?; Ok(Vc::::cell(all_routes).merge()) } #[turbo_tasks::function] fn get_children(&self) -> Vc { Vc::cell(self.sources.clone()) } } #[turbo_tasks::value_impl] impl Introspectable for CombinedContentSource { #[turbo_tasks::function] fn ty(&self) -> Vc { Vc::cell(rcstr!("combined content source")) } #[turbo_tasks::function] async fn title(&self) -> Result> { let titles = self .sources .iter() .map(|&source| async move { Ok( if let Some(source) = ResolvedVc::try_sidecast::>(source) { Some(source.title().await?) } else { None }, ) }) .try_join() .await?; let mut titles = titles.into_iter().flatten().collect::>(); titles.sort(); const NUMBER_OF_TITLES_TO_DISPLAY: usize = 5; let mut titles = titles .iter() .map(|t| t.as_str()) .filter(|t| !t.is_empty()) .take(NUMBER_OF_TITLES_TO_DISPLAY + 1) .collect::>(); if titles.len() > NUMBER_OF_TITLES_TO_DISPLAY { titles[NUMBER_OF_TITLES_TO_DISPLAY] = "..."; } Ok(Vc::cell(titles.join(", ").into())) } #[turbo_tasks::function] async fn children(&self) -> Result> { Ok(Vc::cell( self.sources .iter() .copied() .map(|s| async move { Ok(ResolvedVc::try_sidecast::>(s)) }) .try_join() .await? .into_iter() .flatten() .map(|i| (rcstr!("source"), i)) .collect(), )) } }