Skip to content

Commit

Permalink
move backend impl into type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 14, 2024
1 parent 08d2cf9 commit a071389
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
7 changes: 3 additions & 4 deletions crates/napi/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ use turbo_tasks::{
ValueToString, Vc,
};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath};
use turbo_tasks_memory::MemoryBackend;
use turbopack_core::PROJECT_FILESYSTEM_NAME;

use crate::register;
use crate::{next_api::utils::NextBackend, register};

#[turbo_tasks::function]
async fn project_fs(project_dir: RcStr, watching: bool) -> Result<Vc<Box<dyn FileSystem>>> {
Expand Down Expand Up @@ -353,7 +352,7 @@ async fn get_value(

#[napi]
pub fn stream_entrypoints(
turbo_tasks: External<Arc<TurboTasks<MemoryBackend>>>,
turbo_tasks: External<Arc<TurboTasks<NextBackend>>>,
root_dir: String,
project_dir: String,
page_extensions: Vec<String>,
Expand Down Expand Up @@ -399,7 +398,7 @@ pub fn stream_entrypoints(

#[napi]
pub async fn get_entrypoints(
turbo_tasks: External<Arc<TurboTasks<MemoryBackend>>>,
turbo_tasks: External<Arc<TurboTasks<NextBackend>>>,
root_dir: String,
project_dir: String,
page_extensions: Vec<String>,
Expand Down
13 changes: 6 additions & 7 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use tracing::Instrument;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use turbo_tasks::{Completion, RcStr, ReadRef, TransientInstance, TurboTasks, UpdateInfo, Vc};
use turbo_tasks_fs::{DiskFileSystem, FileContent, FileSystem, FileSystemPath};
use turbo_tasks_memory::MemoryBackend;
use turbopack_core::{
diagnostics::PlainDiagnostic,
error::PrettyPrintError,
Expand All @@ -44,7 +43,7 @@ use url::Url;
use super::{
endpoint::ExternalEndpoint,
utils::{
get_diagnostics, get_issues, subscribe, NapiDiagnostic, NapiIssue, RootTask,
get_diagnostics, get_issues, subscribe, NapiDiagnostic, NapiIssue, NextBackend, RootTask,
TurbopackResult, VcArc,
},
};
Expand Down Expand Up @@ -244,7 +243,7 @@ impl From<NapiDefineEnv> for DefineEnv {
}

pub struct ProjectInstance {
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: Arc<TurboTasks<NextBackend>>,
container: Vc<ProjectContainer>,
exit_receiver: tokio::sync::Mutex<Option<ExitReceiver>>,
}
Expand Down Expand Up @@ -309,7 +308,7 @@ pub async fn project_new(
subscriber.init();
}

let turbo_tasks = TurboTasks::new(MemoryBackend::new(
let turbo_tasks = TurboTasks::new(NextBackend::new(
turbo_engine_options
.memory_limit
.map(|m| m as usize)
Expand Down Expand Up @@ -463,7 +462,7 @@ impl NapiRoute {
fn from_route(
pathname: String,
value: Route,
turbo_tasks: &Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: &Arc<TurboTasks<NextBackend>>,
) -> Self {
let convert_endpoint = |endpoint: Vc<Box<dyn Endpoint>>| {
Some(External::new(ExternalEndpoint(VcArc::new(
Expand Down Expand Up @@ -530,7 +529,7 @@ struct NapiMiddleware {
impl NapiMiddleware {
fn from_middleware(
value: &Middleware,
turbo_tasks: &Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: &Arc<TurboTasks<NextBackend>>,
) -> Result<Self> {
Ok(NapiMiddleware {
endpoint: External::new(ExternalEndpoint(VcArc::new(
Expand All @@ -550,7 +549,7 @@ struct NapiInstrumentation {
impl NapiInstrumentation {
fn from_instrumentation(
value: &Instrumentation,
turbo_tasks: &Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: &Arc<TurboTasks<NextBackend>>,
) -> Result<Self> {
Ok(NapiInstrumentation {
node_js: External::new(ExternalEndpoint(VcArc::new(
Expand Down
12 changes: 7 additions & 5 deletions crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ use turbopack_core::{
source_pos::SourcePos,
};

pub type NextBackend = MemoryBackend;

/// A helper type to hold both a Vc operation and the TurboTasks root process.
/// Without this, we'd need to pass both individually all over the place
#[derive(Clone)]
pub struct VcArc<T> {
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: Arc<TurboTasks<NextBackend>>,
/// The Vc. Must be resolved, otherwise you are referencing an inactive
/// operation.
vc: T,
}

impl<T> VcArc<T> {
pub fn new(turbo_tasks: Arc<TurboTasks<MemoryBackend>>, vc: T) -> Self {
pub fn new(turbo_tasks: Arc<TurboTasks<NextBackend>>, vc: T) -> Self {
Self { turbo_tasks, vc }
}

pub fn turbo_tasks(&self) -> &Arc<TurboTasks<MemoryBackend>> {
pub fn turbo_tasks(&self) -> &Arc<TurboTasks<NextBackend>> {
&self.turbo_tasks
}
}
Expand All @@ -55,7 +57,7 @@ pub fn serde_enum_to_string<T: Serialize>(value: &T) -> Result<String> {
/// The root of our turbopack computation.
pub struct RootTask {
#[allow(dead_code)]
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: Arc<TurboTasks<NextBackend>>,
#[allow(dead_code)]
task_id: Option<TaskId>,
}
Expand Down Expand Up @@ -299,7 +301,7 @@ impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
}

pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>(
turbo_tasks: Arc<TurboTasks<MemoryBackend>>,
turbo_tasks: Arc<TurboTasks<NextBackend>>,
func: JsFunction,
handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>,
Expand Down
9 changes: 5 additions & 4 deletions crates/napi/src/turbotrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use std::sync::Arc;
use napi::bindgen_prelude::*;
use node_file_trace::{start, Args};
use turbo_tasks::TurboTasks;
use turbo_tasks_memory::MemoryBackend;
use turbopack::{
module_options::{EcmascriptOptionsContext, ModuleOptionsContext},
resolve_options_context::ResolveOptionsContext,
};

use crate::next_api::utils::NextBackend;

#[napi]
pub fn create_turbo_tasks(memory_limit: Option<i64>) -> External<Arc<TurboTasks<MemoryBackend>>> {
let turbo_tasks = TurboTasks::new(MemoryBackend::new(
pub fn create_turbo_tasks(memory_limit: Option<i64>) -> External<Arc<TurboTasks<NextBackend>>> {
let turbo_tasks = TurboTasks::new(NextBackend::new(
memory_limit.map(|m| m as usize).unwrap_or(usize::MAX),
));
External::new_with_size_hint(
Expand All @@ -23,7 +24,7 @@ pub fn create_turbo_tasks(memory_limit: Option<i64>) -> External<Arc<TurboTasks<
#[napi]
pub async fn run_turbo_tracing(
options: Buffer,
turbo_tasks: Option<External<Arc<TurboTasks<MemoryBackend>>>>,
turbo_tasks: Option<External<Arc<TurboTasks<NextBackend>>>>,
) -> napi::Result<Vec<String>> {
let args: Args = serde_json::from_slice(options.as_ref())?;
let turbo_tasks = turbo_tasks.map(|t| t.clone());
Expand Down

0 comments on commit a071389

Please sign in to comment.