mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
Use futures 0.3 API (#3358)
This commit is contained in:
parent
cb00fd6e98
commit
8f9a942cb9
36 changed files with 1463 additions and 1018 deletions
|
@ -2,6 +2,8 @@
|
|||
use crate::compilers::CompiledModule;
|
||||
use crate::compilers::CompiledModuleFuture;
|
||||
use crate::file_fetcher::SourceFile;
|
||||
use crate::futures::future::FutureExt;
|
||||
use std::pin::Pin;
|
||||
use std::str;
|
||||
|
||||
pub struct JsCompiler {}
|
||||
|
@ -10,7 +12,7 @@ impl JsCompiler {
|
|||
pub fn compile_async(
|
||||
self: &Self,
|
||||
source_file: &SourceFile,
|
||||
) -> Box<CompiledModuleFuture> {
|
||||
) -> Pin<Box<CompiledModuleFuture>> {
|
||||
let module = CompiledModule {
|
||||
code: str::from_utf8(&source_file.source_code)
|
||||
.unwrap()
|
||||
|
@ -18,6 +20,6 @@ impl JsCompiler {
|
|||
name: source_file.url.to_string(),
|
||||
};
|
||||
|
||||
Box::new(futures::future::ok(module))
|
||||
futures::future::ok(module).boxed()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
use crate::compilers::CompiledModule;
|
||||
use crate::compilers::CompiledModuleFuture;
|
||||
use crate::file_fetcher::SourceFile;
|
||||
use crate::futures::future::FutureExt;
|
||||
use deno::ErrBox;
|
||||
use regex::Regex;
|
||||
use std::pin::Pin;
|
||||
use std::str;
|
||||
|
||||
// From https://github.com/mathiasbynens/mothereff.in/blob/master/js-variables/eff.js
|
||||
|
@ -15,11 +17,11 @@ impl JsonCompiler {
|
|||
pub fn compile_async(
|
||||
self: &Self,
|
||||
source_file: &SourceFile,
|
||||
) -> Box<CompiledModuleFuture> {
|
||||
) -> Pin<Box<CompiledModuleFuture>> {
|
||||
let maybe_json_value: serde_json::Result<serde_json::Value> =
|
||||
serde_json::from_str(&str::from_utf8(&source_file.source_code).unwrap());
|
||||
if let Err(err) = maybe_json_value {
|
||||
return Box::new(futures::future::err(ErrBox::from(err)));
|
||||
return futures::future::err(ErrBox::from(err)).boxed();
|
||||
}
|
||||
|
||||
let mut code = format!(
|
||||
|
@ -50,6 +52,6 @@ impl JsonCompiler {
|
|||
name: source_file.url.to_string(),
|
||||
};
|
||||
|
||||
Box::new(futures::future::ok(module))
|
||||
futures::future::ok(module).boxed()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,4 +19,4 @@ pub struct CompiledModule {
|
|||
}
|
||||
|
||||
pub type CompiledModuleFuture =
|
||||
dyn Future<Item = CompiledModule, Error = ErrBox> + Send;
|
||||
dyn Future<Output = Result<CompiledModule, ErrBox>> + Send;
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -7,11 +7,12 @@ use crate::startup_data;
|
|||
use crate::state::*;
|
||||
use crate::worker::Worker;
|
||||
use deno::Buf;
|
||||
use futures::Future;
|
||||
use futures::IntoFuture;
|
||||
use futures::FutureExt;
|
||||
use futures::TryFutureExt;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json;
|
||||
use std::collections::HashMap;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use url::Url;
|
||||
|
@ -71,11 +72,11 @@ impl WasmCompiler {
|
|||
self: &Self,
|
||||
global_state: ThreadSafeGlobalState,
|
||||
source_file: &SourceFile,
|
||||
) -> Box<CompiledModuleFuture> {
|
||||
) -> Pin<Box<CompiledModuleFuture>> {
|
||||
let cache = self.cache.clone();
|
||||
let maybe_cached = { cache.lock().unwrap().get(&source_file.url).cloned() };
|
||||
if let Some(m) = maybe_cached {
|
||||
return Box::new(futures::future::ok(m.clone()));
|
||||
return futures::future::ok(m.clone()).boxed();
|
||||
}
|
||||
let cache_ = self.cache.clone();
|
||||
|
||||
|
@ -92,7 +93,6 @@ impl WasmCompiler {
|
|||
.into_boxed_str()
|
||||
.into_boxed_bytes(),
|
||||
)
|
||||
.into_future()
|
||||
.then(move |_| worker)
|
||||
.then(move |result| {
|
||||
if let Err(err) = result {
|
||||
|
@ -124,9 +124,9 @@ impl WasmCompiler {
|
|||
cache_.lock().unwrap().insert(url.clone(), module.clone());
|
||||
}
|
||||
debug!("<<<<< wasm_compile_async END");
|
||||
Ok(module)
|
||||
futures::future::ok(module)
|
||||
});
|
||||
Box::new(fut)
|
||||
fut.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue