summaryrefslogtreecommitdiff
path: root/runtime/worker.rs
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/worker.rs')
-rw-r--r--runtime/worker.rs50
1 files changed, 39 insertions, 11 deletions
diff --git a/runtime/worker.rs b/runtime/worker.rs
index 9ffd0b5ab..1ffd32b48 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -1,8 +1,8 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-use crate::inspector::DenoInspector;
use crate::inspector::InspectorServer;
-use crate::inspector::InspectorSession;
+use crate::inspector::JsRuntimeInspector;
+use crate::inspector::LocalInspectorSession;
use crate::js;
use crate::metrics;
use crate::ops;
@@ -13,6 +13,7 @@ use deno_core::error::Context as ErrorContext;
use deno_core::futures::future::poll_fn;
use deno_core::futures::future::FutureExt;
use deno_core::futures::stream::StreamExt;
+use deno_core::futures::Future;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::url::Url;
@@ -27,6 +28,7 @@ use deno_core::RuntimeOptions;
use deno_file::BlobUrlStore;
use log::debug;
use std::env;
+use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use std::task::Context;
@@ -40,7 +42,7 @@ use std::task::Poll;
/// All `WebWorker`s created during program execution
/// are descendants of this worker.
pub struct MainWorker {
- inspector: Option<Box<DenoInspector>>,
+ inspector: Option<Box<JsRuntimeInspector>>,
pub js_runtime: JsRuntime,
should_break_on_first_statement: bool,
}
@@ -147,10 +149,15 @@ impl MainWorker {
});
let inspector = if options.attach_inspector {
- Some(DenoInspector::new(
- &mut js_runtime,
- options.maybe_inspector_server.clone(),
- ))
+ let mut inspector = JsRuntimeInspector::new(&mut js_runtime);
+
+ if let Some(server) = options.maybe_inspector_server.clone() {
+ let session_sender = inspector.get_session_sender();
+ let deregister_rx = inspector.add_deregister_handler();
+ server.register_inspector(session_sender, deregister_rx);
+ }
+
+ Some(inspector)
} else {
None
};
@@ -243,10 +250,9 @@ impl MainWorker {
/// Create new inspector session. This function panics if Worker
/// was not configured to create inspector.
- pub fn create_inspector_session(&mut self) -> Box<InspectorSession> {
- let inspector = self.inspector.as_mut().unwrap();
-
- InspectorSession::new(&mut **inspector)
+ pub async fn create_inspector_session(&mut self) -> LocalInspectorSession {
+ let inspector = self.inspector.as_ref().unwrap();
+ inspector.create_local_session()
}
pub fn poll_event_loop(
@@ -261,6 +267,28 @@ impl MainWorker {
pub async fn run_event_loop(&mut self) -> Result<(), AnyError> {
poll_fn(|cx| self.poll_event_loop(cx)).await
}
+
+ /// A utility function that runs provided future concurrently with the event loop.
+ ///
+ /// Useful when using a local inspector session.
+ pub async fn with_event_loop<'a, T>(
+ &mut self,
+ mut fut: Pin<Box<dyn Future<Output = T> + 'a>>,
+ ) -> T {
+ loop {
+ tokio::select! {
+ result = &mut fut => {
+ return result;
+ }
+ _ = self.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.
+ tokio::time::sleep(tokio::time::Duration::from_millis(0)).await;
+ }
+ };
+ }
+ }
}
impl Drop for MainWorker {