diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-11-17 01:17:47 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-11-16 19:17:47 -0500 |
commit | 8f9a942cb911ed017eb128e9fbeb6f9a48e69601 (patch) | |
tree | 4f56623262f84becac18546d9da3d5d9ce9c8735 /cli/compilers/ts.rs | |
parent | cb00fd6e988184420f842b1e77ca4cf627d32773 (diff) |
Use futures 0.3 API (#3358)
Diffstat (limited to 'cli/compilers/ts.rs')
-rw-r--r-- | cli/compilers/ts.rs | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index cac382659..34bf74ab1 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -15,13 +15,15 @@ use crate::worker::Worker; use deno::Buf; use deno::ErrBox; use deno::ModuleSpecifier; +use futures::future::FutureExt; +use futures::future::TryFutureExt; use futures::Future; -use futures::IntoFuture; use regex::Regex; use std::collections::HashSet; use std::fs; use std::io; use std::path::PathBuf; +use std::pin::Pin; use std::str; use std::sync::atomic::Ordering; use std::sync::Mutex; @@ -254,7 +256,7 @@ impl TsCompiler { global_state: ThreadSafeGlobalState, module_name: String, out_file: Option<String>, - ) -> impl Future<Item = (), Error = ErrBox> { + ) -> impl Future<Output = Result<(), ErrBox>> { debug!( "Invoking the compiler to bundle. module_name: {}", module_name @@ -270,19 +272,17 @@ impl TsCompiler { let worker = TsCompiler::setup_worker(global_state.clone()); let worker_ = worker.clone(); - let first_msg_fut = worker - .post_message(req_msg) - .into_future() - .then(move |_| worker) - .then(move |result| { - 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() - }); + 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>| { @@ -292,11 +292,11 @@ impl TsCompiler { 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)); + return futures::future::err(ErrBox::from(diagnostics)); } } - Ok(()) + futures::future::ok(()) }, ) } @@ -325,11 +325,11 @@ impl TsCompiler { self: &Self, global_state: ThreadSafeGlobalState, source_file: &SourceFile, - ) -> Box<CompiledModuleFuture> { + ) -> Pin<Box<CompiledModuleFuture>> { if self.has_compiled(&source_file.url) { return match self.get_compiled_module(&source_file.url) { - Ok(compiled) => Box::new(futures::future::ok(compiled)), - Err(err) => Box::new(futures::future::err(err)), + Ok(compiled) => futures::future::ok(compiled).boxed(), + Err(err) => futures::future::err(err).boxed(), }; } @@ -351,7 +351,7 @@ impl TsCompiler { self.get_compiled_module(&source_file.url) { self.mark_compiled(&source_file.url); - return Box::new(futures::future::ok(compiled_module)); + return futures::future::ok(compiled_module).boxed(); } } } @@ -382,19 +382,17 @@ impl TsCompiler { .add("Compile", &module_url.to_string()); let global_state_ = global_state.clone(); - let first_msg_fut = worker - .post_message(req_msg) - .into_future() - .then(move |_| worker) - .then(move |result| { - 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() - }); + 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 + }; let fut = first_msg_fut .map_err(|_| panic!("not handled")) @@ -405,37 +403,42 @@ impl TsCompiler { 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)); + return futures::future::err(ErrBox::from(diagnostics)); } } - Ok(()) + futures::future::ok(()) }) .and_then(move |_| { // if we are this far it means compilation was successful and we can // load compiled filed from disk - 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) - }) + 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); - Ok(compiled_module) + futures::future::ok(compiled_module) }) .then(move |r| { debug!(">>>>> compile_sync END"); // TODO(ry) do this in worker's destructor. // resource.close(); - r + futures::future::ready(r) }); - Box::new(fut) + fut.boxed() } /// Get associated `CompiledFileMetadata` for given module if it exists. @@ -656,7 +659,6 @@ mod tests { use crate::fs as deno_fs; use crate::tokio_util; use deno::ModuleSpecifier; - use futures::future::lazy; use std::path::PathBuf; use tempfile::TempDir; @@ -682,7 +684,7 @@ mod tests { String::from("hello.js"), ]); - tokio_util::run(lazy(move || { + tokio_util::run( mock_state .ts_compiler .compile_async(mock_state.clone(), &out) @@ -693,9 +695,9 @@ mod tests { .code .as_bytes() .starts_with("console.log(\"Hello World\");".as_bytes())); - Ok(()) - }) - })) + futures::future::ok(()) + }), + ) } #[test] @@ -716,7 +718,7 @@ mod tests { String::from("$deno$/bundle.js"), ]); - tokio_util::run(lazy(move || { + tokio_util::run( state .ts_compiler .bundle_async( @@ -726,9 +728,9 @@ mod tests { ) .then(|result| { assert!(result.is_ok()); - Ok(()) - }) - })) + futures::future::ok(()) + }), + ) } #[test] |