summaryrefslogtreecommitdiff
path: root/cli/ops/worker_host.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-12-11 18:49:26 +0100
committerGitHub <noreply@github.com>2020-12-11 18:49:26 +0100
commit65e72b68acf57da8462b8e7b057e7adb9393b698 (patch)
tree00e955e1186b9512b009acbb6ee80feb8a3f1733 /cli/ops/worker_host.rs
parent9414dee9e56a9f42c07ada4f8e1be864a1a1b936 (diff)
refactor(cli): decouple ops from ProgramState and Flags (#8659)
This commit does major refactor of "Worker" and "WebWorker", in order to decouple them from "ProgramState" and "Flags". The main points of interest are "create_main_worker()" and "create_web_worker_callback()" functions which are responsible for creating "Worker" and "WebWorker" in CLI context. As a result it is now possible to factor out common "runtime" functionality into a separate crate.
Diffstat (limited to 'cli/ops/worker_host.rs')
-rw-r--r--cli/ops/worker_host.rs38
1 files changed, 31 insertions, 7 deletions
diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs
index 6a2d79968..871e4b9fe 100644
--- a/cli/ops/worker_host.rs
+++ b/cli/ops/worker_host.rs
@@ -21,8 +21,27 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::convert::From;
use std::rc::Rc;
+use std::sync::Arc;
use std::thread::JoinHandle;
+pub struct CreateWebWorkerArgs {
+ pub name: String,
+ pub worker_id: u32,
+ pub permissions: Permissions,
+ pub main_module: ModuleSpecifier,
+ pub use_deno_namespace: bool,
+}
+
+pub type CreateWebWorkerCb =
+ dyn Fn(CreateWebWorkerArgs) -> WebWorker + Sync + Send;
+
+/// A holder for callback that is used to create a new
+/// WebWorker. It's a struct instead of a type alias
+/// because `GothamState` used in `OpState` overrides
+/// value if type alises have the same underlying type
+#[derive(Clone)]
+pub struct CreateWebWorkerCbHolder(Arc<CreateWebWorkerCb>);
+
#[derive(Deserialize)]
struct HostUnhandledErrorArgs {
message: String,
@@ -31,12 +50,16 @@ struct HostUnhandledErrorArgs {
pub fn init(
rt: &mut deno_core::JsRuntime,
sender: Option<mpsc::Sender<WorkerEvent>>,
+ create_web_worker_cb: Arc<CreateWebWorkerCb>,
) {
{
let op_state = rt.op_state();
let mut state = op_state.borrow_mut();
state.put::<WorkersTable>(WorkersTable::default());
state.put::<WorkerId>(WorkerId::default());
+
+ let create_module_loader = CreateWebWorkerCbHolder(create_web_worker_cb);
+ state.put::<CreateWebWorkerCbHolder>(create_module_loader);
}
super::reg_json_sync(rt, "op_create_worker", op_create_worker);
super::reg_json_sync(
@@ -102,11 +125,12 @@ fn op_create_worker(
}
let permissions = state.borrow::<Permissions>().clone();
let worker_id = state.take::<WorkerId>();
+ let create_module_loader = state.take::<CreateWebWorkerCbHolder>();
+ state.put::<CreateWebWorkerCbHolder>(create_module_loader.clone());
state.put::<WorkerId>(worker_id + 1);
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
let worker_name = args_name.unwrap_or_else(|| "".to_string());
- let program_state = super::program_state(state);
let (handle_sender, handle_receiver) =
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
@@ -121,14 +145,14 @@ fn op_create_worker(
// - JS worker is useless - meaning it throws an exception and can't do anything else,
// all action done upon it should be noops
// - newly spawned thread exits
- let worker = WebWorker::new(
- worker_name,
+
+ let worker = (create_module_loader.0)(CreateWebWorkerArgs {
+ name: worker_name,
+ worker_id,
permissions,
- module_specifier.clone(),
- program_state,
+ main_module: module_specifier.clone(),
use_deno_namespace,
- worker_id,
- );
+ });
// Send thread safe handle to newly created worker to host thread
handle_sender.send(Ok(worker.thread_safe_handle())).unwrap();