react-code-dataset
/
next.js
/turbopack
/crates
/turbopack-dev-server
/src
/source
/lazy_instantiated.rs
| use anyhow::Result; | |
| use turbo_rcstr::{RcStr, rcstr}; | |
| use turbo_tasks::{ResolvedVc, Vc}; | |
| use turbopack_core::introspect::{Introspectable, IntrospectableChildren}; | |
| use super::{ContentSource, route_tree::RouteTree}; | |
| /// A functor to get a [ContentSource]. Will be invoked when needed when using | |
| /// [LazyInstantiatedContentSource]. | |
| pub trait GetContentSource { | |
| /// Returns the [ContentSource] | |
| fn content_source(self: Vc<Self>) -> Vc<Box<dyn ContentSource>>; | |
| } | |
| /// Wraps the [ContentSource] creation in a way that only creates it when | |
| /// actually used. | |
| pub struct LazyInstantiatedContentSource { | |
| pub get_source: ResolvedVc<Box<dyn GetContentSource>>, | |
| } | |
| impl ContentSource for LazyInstantiatedContentSource { | |
| fn get_routes(&self) -> Vc<RouteTree> { | |
| self.get_source.content_source().get_routes() | |
| } | |
| } | |
| impl Introspectable for LazyInstantiatedContentSource { | |
| fn ty(&self) -> Vc<RcStr> { | |
| Vc::cell(rcstr!("lazy instantiated content source")) | |
| } | |
| async fn children(&self) -> Result<Vc<IntrospectableChildren>> { | |
| Ok(Vc::cell( | |
| [ResolvedVc::try_sidecast::<Box<dyn Introspectable>>( | |
| self.get_source.content_source().to_resolved().await?, | |
| ) | |
| .map(|i| (rcstr!("source"), i))] | |
| .into_iter() | |
| .flatten() | |
| .collect(), | |
| )) | |
| } | |
| } | |