Skip to content

Commit

Permalink
refactor: Worker is not a Future (denoland#7895)
Browse files Browse the repository at this point in the history
This commit rewrites deno::Worker to not implement Future
trait.

Instead there are two separate methods:
- Worker::poll_event_loop() - does single tick of event loop
- Worker::run_event_loop() - runs event loop to completion

Additionally some cleanup to Worker's field visibility was done.
  • Loading branch information
bartlomieju authored Oct 9, 2020
1 parent 9731cbc commit f4357f0
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 144 deletions.
4 changes: 1 addition & 3 deletions cli/coverage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::colors;
use crate::inspector::DenoInspector;
use crate::inspector::InspectorSession;
use deno_core::error::AnyError;
use deno_core::serde_json;
Expand All @@ -14,8 +13,7 @@ pub struct CoverageCollector {
}

impl CoverageCollector {
pub fn new(inspector_ptr: *mut DenoInspector) -> Self {
let session = InspectorSession::new(inspector_ptr);
pub fn new(session: Box<InspectorSession>) -> Self {
Self { session }
}

Expand Down
22 changes: 9 additions & 13 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async fn eval_command(
debug!("main_module {}", &main_module);
worker.execute_module(&main_module).await?;
worker.execute("window.dispatchEvent(new Event('load'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;
worker.execute("window.dispatchEvent(new Event('unload'))")?;
Ok(())
}
Expand Down Expand Up @@ -423,7 +423,7 @@ async fn run_repl(flags: Flags) -> Result<(), AnyError> {
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
let global_state = GlobalState::new(flags)?;
let mut worker = MainWorker::new(&global_state, main_module.clone());
(&mut *worker).await?;
worker.run_event_loop().await?;

repl::run(&global_state, worker).await
}
Expand Down Expand Up @@ -454,7 +454,7 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
debug!("main_module {}", main_module);
worker.execute_module(&main_module).await?;
worker.execute("window.dispatchEvent(new Event('load'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;
worker.execute("window.dispatchEvent(new Event('unload'))")?;
Ok(())
}
Expand Down Expand Up @@ -500,7 +500,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
debug!("main_module {}", main_module);
worker.execute_module(&main_module).await?;
worker.execute("window.dispatchEvent(new Event('load'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;
worker.execute("window.dispatchEvent(new Event('unload'))")?;
Ok(())
}
Expand All @@ -525,7 +525,7 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
debug!("main_module {}", main_module);
worker.execute_module(&main_module).await?;
worker.execute("window.dispatchEvent(new Event('load'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;
worker.execute("window.dispatchEvent(new Event('unload'))")?;
Ok(())
}
Expand Down Expand Up @@ -578,12 +578,8 @@ async fn test_command(
.save_source_file_in_cache(&main_module, source_file);

let mut maybe_coverage_collector = if flags.coverage {
let inspector = worker
.inspector
.as_mut()
.expect("Inspector is not created.");

let mut coverage_collector = CoverageCollector::new(&mut **inspector);
let session = worker.create_inspector_session();
let mut coverage_collector = CoverageCollector::new(session);
coverage_collector.start_collecting().await?;

Some(coverage_collector)
Expand All @@ -594,9 +590,9 @@ async fn test_command(
let execute_result = worker.execute_module(&main_module).await;
execute_result?;
worker.execute("window.dispatchEvent(new Event('load'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;
worker.execute("window.dispatchEvent(new Event('unload'))")?;
(&mut *worker).await?;
worker.run_event_loop().await?;

if let Some(coverage_collector) = maybe_coverage_collector.as_mut() {
let coverages = coverage_collector.collect().await?;
Expand Down
3 changes: 2 additions & 1 deletion cli/ops/worker_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ fn run_worker_thread(
// TODO(bartlomieju): this thread should return result of event loop
// that means that we should store JoinHandle to thread to ensure
// that it actually terminates.
rt.block_on(worker).expect("Panic in event loop");
rt.block_on(worker.run_event_loop())
.expect("Panic in event loop");
debug!("Worker thread shuts down {}", &name);
})?;

Expand Down
11 changes: 3 additions & 8 deletions cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn post_message_and_poll(
return result
}

_ = &mut *worker => {
_ = worker.run_event_loop() => {
// A zero delay is long enough to yield the thread in order to prevent the loop from
// running hot for messages that are taking longer to resolve like for example an
// evaluation of top level await.
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn read_line_and_poll(
result = &mut line => {
return result.unwrap();
}
_ = &mut *worker, if poll_worker => {
_ = worker.run_event_loop(), if poll_worker => {
poll_worker = false;
}
_ = &mut timeout => {
Expand All @@ -92,12 +92,7 @@ pub async fn run(
// Our inspector is unable to default to the default context id so we have to specify it here.
let context_id: u32 = 1;

let inspector = worker
.inspector
.as_mut()
.expect("Inspector is not created.");

let mut session = InspectorSession::new(&mut **inspector);
let mut session = worker.create_inspector_session();

let history_file = global_state.dir.root.join("deno_history.txt");

Expand Down
Loading

0 comments on commit f4357f0

Please sign in to comment.