1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 08:33:43 -05:00

refactor: don't create new runtime for fs ops (#3730)

This commit is contained in:
Bartek Iwańczuk 2020-01-21 14:26:11 +01:00 committed by GitHub
parent 21cc9cb7a7
commit 0cd605515c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,12 +1,12 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::*;
use futures::future::FutureExt;
use futures::task::SpawnExt;
pub use serde_derive::Deserialize;
use serde_json::json;
pub use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
use tokio::task;
pub type AsyncJsonOp =
Pin<Box<dyn Future<Output = Result<Value, ErrBox>> + Send>>;
@ -96,11 +96,12 @@ where
if is_sync {
Ok(JsonOp::Sync(f()?))
} else {
//TODO(afinch7) replace this with something more efficent.
let pool = futures::executor::ThreadPool::new().unwrap();
let handle = pool
.spawn_with_handle(futures::future::lazy(move |_cx| f()))
.unwrap();
Ok(JsonOp::Async(handle.boxed()))
let fut = async move {
task::spawn_blocking(move || f())
.await
.map_err(ErrBox::from)?
}
.boxed();
Ok(JsonOp::Async(fut.boxed()))
}
}