Skip to content

Commit

Permalink
move references() to specific traits (#5555)
Browse files Browse the repository at this point in the history
### Description

preparation for making different typed references

next.js PR: vercel/next.js#52822
  • Loading branch information
sokra authored Jul 18, 2023
1 parent 56edd9e commit 46bb9b7
Show file tree
Hide file tree
Showing 38 changed files with 269 additions and 278 deletions.
6 changes: 3 additions & 3 deletions crates/turbopack-build/src/ecmascript/node/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ impl OutputAsset for EcmascriptBuildNodeChunk {
let ident = self.chunk.ident().with_modifier(modifier());
AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string()))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand All @@ -92,7 +89,10 @@ impl Asset for EcmascriptBuildNodeChunk {

Ok(Vc::cell(references))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeChunk {
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<AssetContent> {
self.own_content().content()
Expand Down
6 changes: 3 additions & 3 deletions crates/turbopack-build/src/ecmascript/node/entry/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,7 @@ impl OutputAsset for EcmascriptBuildNodeEntryChunk {
fn ident(&self) -> Vc<AssetIdent> {
AssetIdent::from_path(self.path)
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeEntryChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand All @@ -212,7 +209,10 @@ impl Asset for EcmascriptBuildNodeEntryChunk {

Ok(Vc::cell(references))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeEntryChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?;
Expand Down
6 changes: 3 additions & 3 deletions crates/turbopack-build/src/ecmascript/node/entry/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {

AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string()))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand All @@ -116,7 +113,10 @@ impl Asset for EcmascriptBuildNodeRuntimeChunk {

Ok(Vc::cell(references))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn content(self: Vc<Self>) -> Result<Vc<AssetContent>> {
let code = self.code().await?;
Expand Down
10 changes: 1 addition & 9 deletions crates/turbopack-core/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
};

use crate::{
reference::AssetReferences,
version::{VersionedAssetContent, VersionedContent},
};
use crate::version::{VersionedAssetContent, VersionedContent};

/// A list of [Asset]s
#[turbo_tasks::value(transparent)]
Expand All @@ -34,11 +31,6 @@ pub trait Asset {
/// The content of the [Asset].
fn content(self: Vc<Self>) -> Vc<AssetContent>;

/// Other things (most likely [Asset]s) referenced from this [Asset].
fn references(self: Vc<Self>) -> Vc<AssetReferences> {
AssetReferences::empty()
}

/// The content of the [Asset] alongside its version.
async fn versioned_content(self: Vc<Self>) -> Result<Vc<Box<dyn VersionedContent>>> {
Ok(Vc::upcast(VersionedAssetContent::new(self.content())))
Expand Down
51 changes: 42 additions & 9 deletions crates/turbopack-core/src/changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@ use turbo_tasks::{
Completion, Completions, Vc,
};

use crate::{asset::Asset, output::OutputAssets, reference::all_referenced_assets};
use crate::{
asset::Asset,
module::Module,
output::{OutputAsset, OutputAssets},
reference::{all_referenced_modules, all_referenced_output_assets},
};

async fn get_referenced_output_assets(
parent: Vc<Box<dyn OutputAsset>>,
) -> Result<impl Iterator<Item = Vc<Box<dyn OutputAsset>>> + Send> {
Ok(all_referenced_output_assets(parent)
.await?
.clone_value()
.into_iter())
}

async fn get_referenced_assets(
parent: Vc<Box<dyn Asset>>,
) -> Result<impl Iterator<Item = Vc<Box<dyn Asset>>> + Send> {
Ok(all_referenced_assets(parent)
async fn get_referenced_modules(
parent: Vc<Box<dyn Module>>,
) -> Result<impl Iterator<Item = Vc<Box<dyn Module>>> + Send> {
Ok(all_referenced_modules(parent)
.await?
.clone_value()
.into_iter())
Expand All @@ -18,15 +32,34 @@ async fn get_referenced_assets(
/// Returns a completion that changes when any content of any asset in the whole
/// asset graph changes.
#[turbo_tasks::function]
pub async fn any_content_changed(root: Vc<Box<dyn Asset>>) -> Result<Vc<Completion>> {
pub async fn any_content_changed_of_module(root: Vc<Box<dyn Module>>) -> Result<Vc<Completion>> {
let completions = NonDeterministic::new()
.skip_duplicates()
.visit([root], get_referenced_modules)
.await
.completed()?
.into_inner()
.into_iter()
.map(|m| content_changed(Vc::upcast(m)))
.collect();

Ok(Vc::<Completions>::cell(completions).completed())
}

/// Returns a completion that changes when any content of any asset in the whole
/// asset graph changes.
#[turbo_tasks::function]
pub async fn any_content_changed_of_output_asset(
root: Vc<Box<dyn OutputAsset>>,
) -> Result<Vc<Completion>> {
let completions = NonDeterministic::new()
.skip_duplicates()
.visit([root], get_referenced_assets)
.visit([root], get_referenced_output_assets)
.await
.completed()?
.into_inner()
.into_iter()
.map(content_changed)
.map(|m| content_changed(Vc::upcast(m)))
.collect();

Ok(Vc::<Completions>::cell(completions).completed())
Expand All @@ -42,7 +75,7 @@ pub async fn any_content_changed_of_output_assets(
roots
.await?
.iter()
.map(|&a| any_content_changed(Vc::upcast(a)))
.map(|&a| any_content_changed_of_output_asset(a))
.collect(),
)
.completed())
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-core/src/chunk/available_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use turbo_tasks_hash::Xxh3Hash64Hasher;

use super::{ChunkableModuleReference, ChunkingType};
use crate::{
asset::Asset,
module::{Module, ModulesSet},
reference::AssetReference,
};
Expand Down
7 changes: 7 additions & 0 deletions crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,18 @@ pub trait Chunk: Asset {
fn path(self: Vc<Self>) -> Vc<FileSystemPath> {
self.ident().path()
}

/// Returns a list of chunks that should be loaded in parallel to this
/// chunk.
fn parallel_chunks(self: Vc<Self>) -> Vc<Chunks> {
Chunks::empty()
}

/// Other things (most likely [Asset]s) referenced from this [Chunk].
// TODO refactor this to ensure that only [OutputAsset]s can be referenced
fn references(self: Vc<Self>) -> Vc<AssetReferences> {
AssetReferences::empty()
}
}

/// Aggregated information about a chunk content that can be used by the runtime
Expand Down
8 changes: 0 additions & 8 deletions crates/turbopack-core/src/file_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use turbo_tasks_fs::{FileContent, FileSystemEntryType, FileSystemPath, LinkConte
use crate::{
asset::{Asset, AssetContent},
ident::AssetIdent,
reference::AssetReferences,
source::Source,
};

Expand Down Expand Up @@ -53,11 +52,4 @@ impl Asset for FileSource {
_ => Err(anyhow::anyhow!("Invalid file type {:?}", file_type)),
}
}

#[turbo_tasks::function]
fn references(&self) -> Vc<AssetReferences> {
// TODO: build input sourcemaps via language specific sourceMappingURL comment
// or parse.
AssetReferences::empty()
}
}
22 changes: 19 additions & 3 deletions crates/turbopack-core/src/introspect/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{bail, Result};
use indexmap::IndexSet;
use turbo_tasks::{ValueToString, Vc};
use turbo_tasks_fs::FileContent;
Expand Down Expand Up @@ -93,8 +93,24 @@ impl Introspectable for IntrospectableAsset {
}

#[turbo_tasks::function]
fn children(&self) -> Vc<IntrospectableChildren> {
children_from_asset_references(self.0.references())
async fn children(&self) -> Result<Vc<IntrospectableChildren>> {
let asset = self.0.resolve().await?;
Ok(
if Vc::try_resolve_downcast::<Box<dyn Source>>(asset)
.await?
.is_some()
{
Vc::cell(Default::default())
} else if let Some(module) = Vc::try_resolve_downcast::<Box<dyn Module>>(asset).await? {
children_from_asset_references(module.references())
} else if let Some(output_asset) =
Vc::try_resolve_downcast::<Box<dyn OutputAsset>>(asset).await?
{
children_from_asset_references(output_asset.references())
} else {
bail!("unknown type")
},
)
}
}

Expand Down
11 changes: 10 additions & 1 deletion crates/turbopack-core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use anyhow::{bail, Result};
use indexmap::IndexSet;
use turbo_tasks::Vc;

use crate::{asset::Asset, ident::AssetIdent, raw_module::RawModule, source::Source};
use crate::{
asset::Asset, ident::AssetIdent, raw_module::RawModule, reference::AssetReferences,
source::Source,
};

/// A module. This usually represents parsed source code, which has references
/// to other modules.
Expand All @@ -11,6 +14,12 @@ pub trait Module: Asset {
/// The identifier of the [Module]. It's expected to be unique and capture
/// all properties of the [Module].
fn ident(&self) -> Vc<AssetIdent>;

/// Other things (most likely [Asset]s) referenced from this [Module].
// TODO refactor this to ensure that only [Module]s can be referenced
fn references(self: Vc<Self>) -> Vc<AssetReferences> {
AssetReferences::empty()
}
}

#[turbo_tasks::value(transparent)]
Expand Down
8 changes: 7 additions & 1 deletion crates/turbopack-core/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{Context, Result};
use indexmap::IndexSet;
use turbo_tasks::Vc;

use crate::{asset::Asset, ident::AssetIdent};
use crate::{asset::Asset, ident::AssetIdent, reference::AssetReferences};

/// An asset that should be outputted, e. g. written to disk or served from a
/// server.
Expand All @@ -12,6 +12,12 @@ pub trait OutputAsset: Asset {
/// The identifier of the [OutputAsset]. It's expected to be unique and
/// capture all properties of the [OutputAsset]. Only path must be used.
fn ident(&self) -> Vc<AssetIdent>;

/// Other things (most likely [Asset]s) referenced from this [OutputAsset].
// TODO refactor this to ensure that only [OutputAsset]s can be referenced
fn references(self: Vc<Self>) -> Vc<AssetReferences> {
AssetReferences::empty()
}
}

#[turbo_tasks::value(transparent)]
Expand Down
14 changes: 7 additions & 7 deletions crates/turbopack-core/src/proxied_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ use crate::{
/// Next.js apps.
#[turbo_tasks::value]
pub struct ProxiedAsset {
asset: Vc<Box<dyn Asset>>,
asset: Vc<Box<dyn OutputAsset>>,
path: Vc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl ProxiedAsset {
/// Creates a new [`ProxiedAsset`] from an [`Asset`] and a path.
#[turbo_tasks::function]
pub fn new(asset: Vc<Box<dyn Asset>>, path: Vc<FileSystemPath>) -> Vc<Self> {
pub fn new(asset: Vc<Box<dyn OutputAsset>>, path: Vc<FileSystemPath>) -> Vc<Self> {
ProxiedAsset { asset, path }.cell()
}
}
Expand All @@ -34,6 +34,11 @@ impl OutputAsset for ProxiedAsset {
fn ident(&self) -> Vc<AssetIdent> {
AssetIdent::from_path(self.path)
}

#[turbo_tasks::function]
fn references(&self) -> Vc<AssetReferences> {
self.asset.references()
}
}

#[turbo_tasks::value_impl]
Expand All @@ -43,11 +48,6 @@ impl Asset for ProxiedAsset {
self.asset.content()
}

#[turbo_tasks::function]
fn references(&self) -> Vc<AssetReferences> {
self.asset.references()
}

#[turbo_tasks::function]
fn versioned_content(&self) -> Vc<Box<dyn VersionedContent>> {
self.asset.versioned_content()
Expand Down
Loading

0 comments on commit 46bb9b7

Please sign in to comment.