diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-02-18 14:47:11 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-18 14:47:11 -0500 |
commit | 3d5bed35e032ee20e4fe34cad925202c6f0c0d3e (patch) | |
tree | 2f9ad905c9e55bd80832055ac7ef41c94bf419bc /cli/compilers | |
parent | 08dcf6bff73bbe579769ccd0f135ed4af919ea48 (diff) |
refactor: remove run_worker_loop (#4028)
* remove run_worker_loop, impl poll for WebWorker
* store JoinHandle to worker thread
Diffstat (limited to 'cli/compilers')
-rw-r--r-- | cli/compilers/compiler_worker.rs | 30 | ||||
-rw-r--r-- | cli/compilers/ts.rs | 8 | ||||
-rw-r--r-- | cli/compilers/wasm.rs | 8 |
3 files changed, 25 insertions, 21 deletions
diff --git a/cli/compilers/compiler_worker.rs b/cli/compilers/compiler_worker.rs index 123c29abb..f0489e641 100644 --- a/cli/compilers/compiler_worker.rs +++ b/cli/compilers/compiler_worker.rs @@ -1,11 +1,17 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use crate::ops; use crate::state::State; -use crate::worker::Worker; +use crate::web_worker::WebWorker; +use core::task::Context; use deno_core; +use deno_core::ErrBox; use deno_core::StartupData; +use futures::future::Future; +use futures::future::FutureExt; use std::ops::Deref; use std::ops::DerefMut; +use std::pin::Pin; +use std::task::Poll; /// This worker is used to host TypeScript and WASM compilers. /// @@ -20,22 +26,15 @@ use std::ops::DerefMut; /// /// TODO(bartlomieju): add support to reuse the worker - or in other /// words support stateful TS compiler -pub struct CompilerWorker(Worker); +pub struct CompilerWorker(WebWorker); impl CompilerWorker { pub fn new(name: String, startup_data: StartupData, state: State) -> Self { let state_ = state.clone(); - let mut worker = Worker::new(name, startup_data, state_); + let mut worker = WebWorker::new(name, startup_data, state_); { let isolate = &mut worker.isolate; - ops::runtime::init(isolate, &state); ops::compiler::init(isolate, &state); - ops::web_worker::init(isolate, &state, &worker.internal_channels.sender); - ops::errors::init(isolate, &state); - // for compatibility with Worker scope, though unused at - // the moment - ops::timers::init(isolate, &state); - ops::fetch::init(isolate, &state); // TODO(bartlomieju): CompilerWorker should not // depend on those ops ops::os::init(isolate, &state); @@ -48,7 +47,7 @@ impl CompilerWorker { } impl Deref for CompilerWorker { - type Target = Worker; + type Target = WebWorker; fn deref(&self) -> &Self::Target { &self.0 } @@ -59,3 +58,12 @@ impl DerefMut for CompilerWorker { &mut self.0 } } + +impl Future for CompilerWorker { + type Output = Result<(), ErrBox>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { + let inner = self.get_mut(); + inner.0.poll_unpin(cx) + } +} diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index 9bfd93eeb..dec74c256 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -9,12 +9,11 @@ use crate::file_fetcher::SourceFile; use crate::file_fetcher::SourceFileFetcher; use crate::global_state::GlobalState; use crate::msg; -use crate::ops::worker_host::run_worker_loop; use crate::ops::JsonResult; use crate::source_maps::SourceMapGetter; use crate::startup_data; use crate::state::*; -use crate::tokio_util::create_basic_runtime; +use crate::tokio_util; use crate::version; use crate::worker::WorkerEvent; use crate::worker::WorkerHandle; @@ -611,11 +610,10 @@ async fn execute_in_thread( let builder = std::thread::Builder::new().name("deno-ts-compiler".to_string()); let join_handle = builder.spawn(move || { - let mut worker = TsCompiler::setup_worker(global_state.clone()); + let worker = TsCompiler::setup_worker(global_state.clone()); handle_sender.send(Ok(worker.thread_safe_handle())).unwrap(); drop(handle_sender); - let mut rt = create_basic_runtime(); - run_worker_loop(&mut rt, &mut worker).expect("Panic in event loop"); + tokio_util::run_basic(worker).expect("Panic in event loop"); })?; let mut handle = handle_receiver.recv().unwrap()?; handle.post_message(req).await?; diff --git a/cli/compilers/wasm.rs b/cli/compilers/wasm.rs index d6f0d2f4a..bc056d4f9 100644 --- a/cli/compilers/wasm.rs +++ b/cli/compilers/wasm.rs @@ -3,10 +3,9 @@ use super::compiler_worker::CompilerWorker; use crate::compilers::CompiledModule; use crate::file_fetcher::SourceFile; use crate::global_state::GlobalState; -use crate::ops::worker_host::run_worker_loop; use crate::startup_data; use crate::state::*; -use crate::tokio_util::create_basic_runtime; +use crate::tokio_util; use crate::worker::WorkerEvent; use crate::worker::WorkerHandle; use deno_core::Buf; @@ -123,11 +122,10 @@ async fn execute_in_thread( let builder = std::thread::Builder::new().name("deno-wasm-compiler".to_string()); let join_handle = builder.spawn(move || { - let mut worker = WasmCompiler::setup_worker(global_state); + let worker = WasmCompiler::setup_worker(global_state); handle_sender.send(Ok(worker.thread_safe_handle())).unwrap(); drop(handle_sender); - let mut rt = create_basic_runtime(); - run_worker_loop(&mut rt, &mut worker).expect("Panic in event loop"); + tokio_util::run_basic(worker).expect("Panic in event loop"); })?; let mut handle = handle_receiver.recv().unwrap()?; handle.post_message(req).await?; |