diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-11-22 18:14:34 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-22 12:14:34 -0500 |
commit | 363b968bfcef26c30f84e485beec6194e5b1dd98 (patch) | |
tree | c37319273fc99f0dbd9195cd3e467938563743c0 /cli/compilers | |
parent | 8d977d0117c2b61e6714cec9e4238f4ba0a56195 (diff) |
minor clean ups in TS compiler (#3394)
Diffstat (limited to 'cli/compilers')
-rw-r--r-- | cli/compilers/js.rs | 2 | ||||
-rw-r--r-- | cli/compilers/ts.rs | 155 |
2 files changed, 56 insertions, 101 deletions
diff --git a/cli/compilers/js.rs b/cli/compilers/js.rs index b6277659f..02a7cc5b1 100644 --- a/cli/compilers/js.rs +++ b/cli/compilers/js.rs @@ -2,7 +2,7 @@ use crate::compilers::CompiledModule; use crate::compilers::CompiledModuleFuture; use crate::file_fetcher::SourceFile; -use crate::futures::future::FutureExt; +use futures::future::FutureExt; use std::pin::Pin; use std::str; diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index 34bf74ab1..c255e18be 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -16,7 +16,6 @@ use deno::Buf; use deno::ErrBox; use deno::ModuleSpecifier; use futures::future::FutureExt; -use futures::future::TryFutureExt; use futures::Future; use regex::Regex; use std::collections::HashSet; @@ -272,33 +271,22 @@ impl TsCompiler { let worker = TsCompiler::setup_worker(global_state.clone()); let worker_ = worker.clone(); - let first_msg_fut = async move { - worker.post_message(req_msg).await.unwrap(); - let result = worker.await; - if let Err(err) = result { - // TODO(ry) Need to forward the error instead of exiting. - eprintln!("{}", err.to_string()); - std::process::exit(1); - } - debug!("Sent message to worker"); - worker_.get_message().await - }; - - first_msg_fut.map_err(|_| panic!("not handled")).and_then( - move |maybe_msg: Option<Buf>| { - debug!("Received message from worker"); - if let Some(msg) = maybe_msg { - let json_str = std::str::from_utf8(&msg).unwrap(); - debug!("Message: {}", json_str); - if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) { - return futures::future::err(ErrBox::from(diagnostics)); - } + async move { + worker.post_message(req_msg).await?; + worker.await?; + debug!("Sent message to worker"); + let maybe_msg = worker_.get_message().await?; + debug!("Received message from worker"); + if let Some(msg) = maybe_msg { + let json_str = std::str::from_utf8(&msg).unwrap(); + debug!("Message: {}", json_str); + if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) { + return Err(ErrBox::from(diagnostics)); } - - futures::future::ok(()) - }, - ) + } + Ok(()) + } } /// Mark given module URL as compiled to avoid multiple compilations of same module @@ -382,63 +370,27 @@ impl TsCompiler { .add("Compile", &module_url.to_string()); let global_state_ = global_state.clone(); - let first_msg_fut = async move { - worker.post_message(req_msg).await.unwrap(); - let result = worker.await; - if let Err(err) = result { - // TODO(ry) Need to forward the error instead of exiting. - eprintln!("{}", err.to_string()); - std::process::exit(1); - } + async move { + worker.post_message(req_msg).await?; + worker.await?; debug!("Sent message to worker"); - worker_.get_message().await - }; - - let fut = first_msg_fut - .map_err(|_| panic!("not handled")) - .and_then(move |maybe_msg: Option<Buf>| { - debug!("Received message from worker"); - - if let Some(msg) = maybe_msg { - let json_str = std::str::from_utf8(&msg).unwrap(); - debug!("Message: {}", json_str); - if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) { - return futures::future::err(ErrBox::from(diagnostics)); - } + let maybe_msg = worker_.get_message().await?; + if let Some(msg) = maybe_msg { + let json_str = std::str::from_utf8(&msg).unwrap(); + debug!("Message: {}", json_str); + if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) { + return Err(ErrBox::from(diagnostics)); } - - futures::future::ok(()) - }) - .and_then(move |_| { - // if we are this far it means compilation was successful and we can - // load compiled filed from disk - futures::future::ready( - global_state_ - .ts_compiler - .get_compiled_module(&source_file_.url) - .map_err(|e| { - // TODO: this situation shouldn't happen - panic!( - "Expected to find compiled file: {} {}", - e, source_file_.url - ) - }), - ) - }) - .and_then(move |compiled_module| { - // Explicit drop to keep reference alive until future completes. - drop(compiling_job); - - futures::future::ok(compiled_module) - }) - .then(move |r| { - debug!(">>>>> compile_sync END"); - // TODO(ry) do this in worker's destructor. - // resource.close(); - futures::future::ready(r) - }); - - fut.boxed() + } + let compiled_module = global_state_ + .ts_compiler + .get_compiled_module(&source_file_.url) + .expect("Expected to find compiled file"); + drop(compiling_job); + debug!(">>>>> compile_sync END"); + Ok(compiled_module) + } + .boxed() } /// Get associated `CompiledFileMetadata` for given module if it exists. @@ -684,20 +636,22 @@ mod tests { String::from("hello.js"), ]); - tokio_util::run( - mock_state + let fut = async move { + let result = mock_state .ts_compiler .compile_async(mock_state.clone(), &out) - .then(|result| { - assert!(result.is_ok()); - assert!(result - .unwrap() - .code - .as_bytes() - .starts_with("console.log(\"Hello World\");".as_bytes())); - futures::future::ok(()) - }), - ) + .await; + + assert!(result.is_ok()); + assert!(result + .unwrap() + .code + .as_bytes() + .starts_with("console.log(\"Hello World\");".as_bytes())); + Ok(()) + }; + + tokio_util::run(fut.boxed()) } #[test] @@ -718,19 +672,20 @@ mod tests { String::from("$deno$/bundle.js"), ]); - tokio_util::run( - state + let fut = async move { + let result = state .ts_compiler .bundle_async( state.clone(), module_name, Some(String::from("$deno$/bundle.js")), ) - .then(|result| { - assert!(result.is_ok()); - futures::future::ok(()) - }), - ) + .await; + + assert!(result.is_ok()); + Ok(()) + }; + tokio_util::run(fut.boxed()) } #[test] |