Download next.js/turbopack/crates/turbopack-dev-server/src/source/conditional.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/source/conditional.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-dev-server/src/source/conditional.rs
-
curl -L -o conditional.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/source/conditional.rs
5.25 kB
| use anyhow::Result; | |
| use turbo_rcstr::{RcStr, rcstr}; | |
| use turbo_tasks::{Completion, ResolvedVc, State, Vc}; | |
| use turbopack_core::introspect::{Introspectable, IntrospectableChildren}; | |
| use super::{ | |
| ContentSource, ContentSourceData, ContentSourceDataVary, ContentSourceSideEffect, | |
| GetContentSourceContent, | |
| route_tree::{MapGetContentSourceContent, RouteTree, RouteTrees}, | |
| }; | |
| use crate::source::{ContentSourceContent, ContentSources}; | |
| /// Combines two [ContentSource]s like the [CombinedContentSource], but only | |
| /// allows to serve from the second source when the first source has | |
| /// successfully served something once. | |
| /// | |
| /// This is a laziness optimization when the content of the second source can | |
| /// only be reached via references from the first source. | |
| /// | |
| /// For example, we use that in the content source that handles SSR rendering of | |
| /// pages. Here HTML and "other assets" are in different content sources. So we | |
| /// use this source to only serve (and process) "other assets" when the HTML was | |
| /// served once. | |
| pub struct ConditionalContentSource { | |
| activator: ResolvedVc<Box<dyn ContentSource>>, | |
| action: ResolvedVc<Box<dyn ContentSource>>, | |
| activated: State<bool>, | |
| } | |
| impl ConditionalContentSource { | |
| pub fn new( | |
| activator: ResolvedVc<Box<dyn ContentSource>>, | |
| action: ResolvedVc<Box<dyn ContentSource>>, | |
| ) -> Vc<Self> { | |
| ConditionalContentSource { | |
| activator, | |
| action, | |
| activated: State::new(false), | |
| } | |
| .cell() | |
| } | |
| } | |
| impl ContentSource for ConditionalContentSource { | |
| async fn get_routes(self: ResolvedVc<Self>) -> Result<Vc<RouteTree>> { | |
| let this = self.await?; | |
| Ok(if !*this.activated.get() { | |
| this.activator.get_routes().map_routes(Vc::upcast( | |
| ConditionalContentSourceMapper { source: self }.cell(), | |
| )) | |
| } else { | |
| Vc::<RouteTrees>::cell(vec![ | |
| this.activator.get_routes().to_resolved().await?, | |
| this.action.get_routes().to_resolved().await?, | |
| ]) | |
| .merge() | |
| }) | |
| } | |
| fn get_children(&self) -> Vc<ContentSources> { | |
| Vc::cell(vec![self.activator, self.action]) | |
| } | |
| } | |
| struct ConditionalContentSourceMapper { | |
| source: ResolvedVc<ConditionalContentSource>, | |
| } | |
| impl MapGetContentSourceContent for ConditionalContentSourceMapper { | |
| fn map_get_content( | |
| &self, | |
| get_content: ResolvedVc<Box<dyn GetContentSourceContent>>, | |
| ) -> Vc<Box<dyn GetContentSourceContent>> { | |
| Vc::upcast( | |
| ActivateOnGetContentSource { | |
| source: self.source, | |
| get_content, | |
| } | |
| .cell(), | |
| ) | |
| } | |
| } | |
| impl Introspectable for ConditionalContentSource { | |
| fn ty(&self) -> Vc<RcStr> { | |
| Vc::cell(rcstr!("conditional content source")) | |
| } | |
| fn details(&self) -> Vc<RcStr> { | |
| Vc::cell(if *self.activated.get() { | |
| rcstr!("activated") | |
| } else { | |
| rcstr!("not activated") | |
| }) | |
| } | |
| fn title(&self) -> Result<Vc<RcStr>> { | |
| if let Some(activator) = ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(self.activator) | |
| { | |
| Ok(activator.title()) | |
| } else { | |
| Ok(Vc::<RcStr>::default()) | |
| } | |
| } | |
| fn children(&self) -> Result<Vc<IntrospectableChildren>> { | |
| Ok(Vc::cell( | |
| [ | |
| ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(self.activator) | |
| .map(|i| (rcstr!("activator"), i)), | |
| ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(self.action) | |
| .map(|i| (rcstr!("action"), i)), | |
| ] | |
| .into_iter() | |
| .flatten() | |
| .collect(), | |
| )) | |
| } | |
| } | |
| struct ActivateOnGetContentSource { | |
| source: ResolvedVc<ConditionalContentSource>, | |
| get_content: ResolvedVc<Box<dyn GetContentSourceContent>>, | |
| } | |
| impl GetContentSourceContent for ActivateOnGetContentSource { | |
| fn vary(&self) -> Vc<ContentSourceDataVary> { | |
| self.get_content.vary() | |
| } | |
| async fn get( | |
| self: ResolvedVc<Self>, | |
| path: RcStr, | |
| data: ContentSourceData, | |
| ) -> Result<Vc<ContentSourceContent>> { | |
| turbo_tasks::emit(ResolvedVc::upcast::<Box<dyn ContentSourceSideEffect>>(self)); | |
| Ok(self.await?.get_content.get(path, data)) | |
| } | |
| } | |
| impl ContentSourceSideEffect for ActivateOnGetContentSource { | |
| async fn apply(&self) -> Result<Vc<Completion>> { | |
| self.source.await?.activated.set(true); | |
| Ok(Completion::new()) | |
| } | |
| } | |