turbopack_dev_server/source/
lazy_instantiated.rs

1use anyhow::Result;
2use turbo_rcstr::{RcStr, rcstr};
3use turbo_tasks::{ResolvedVc, Vc};
4use turbopack_core::introspect::{Introspectable, IntrospectableChildren};
5
6use super::{ContentSource, route_tree::RouteTree};
7
8/// A functor to get a [ContentSource]. Will be invoked when needed when using
9/// [LazyInstantiatedContentSource].
10#[turbo_tasks::value_trait]
11pub trait GetContentSource {
12    /// Returns the [ContentSource]
13    #[turbo_tasks::function]
14    fn content_source(self: Vc<Self>) -> Vc<Box<dyn ContentSource>>;
15}
16
17/// Wraps the [ContentSource] creation in a way that only creates it when
18/// actually used.
19#[turbo_tasks::value(shared)]
20pub struct LazyInstantiatedContentSource {
21    pub get_source: ResolvedVc<Box<dyn GetContentSource>>,
22}
23
24#[turbo_tasks::value_impl]
25impl ContentSource for LazyInstantiatedContentSource {
26    #[turbo_tasks::function]
27    fn get_routes(&self) -> Vc<RouteTree> {
28        self.get_source.content_source().get_routes()
29    }
30}
31
32#[turbo_tasks::value_impl]
33impl Introspectable for LazyInstantiatedContentSource {
34    #[turbo_tasks::function]
35    fn ty(&self) -> Vc<RcStr> {
36        Vc::cell(rcstr!("lazy instantiated content source"))
37    }
38
39    #[turbo_tasks::function]
40    async fn children(&self) -> Result<Vc<IntrospectableChildren>> {
41        Ok(Vc::cell(
42            [ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(
43                self.get_source.content_source().to_resolved().await?,
44            )
45            .map(|i| (rcstr!("source"), i))]
46            .into_iter()
47            .flatten()
48            .collect(),
49        ))
50    }
51}