diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-12-13 19:45:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-13 19:45:53 +0100 |
commit | 2e74f164b6dcf0ecbf8dd38fba9fae550d784bd0 (patch) | |
tree | 61abe8e09d5331ace5d9de529f0e2737a8e05dbb /runtime/ops/web_worker.rs | |
parent | 84ef9bd21fb48fb6b5fbc8dafc3de9f361bade3b (diff) |
refactor: deno_runtime crate (#8640)
This commit moves Deno JS runtime, ops, permissions and
inspector implementation to new "deno_runtime" crate located
in "runtime/" directory.
Details in "runtime/README.md".
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'runtime/ops/web_worker.rs')
-rw-r--r-- | runtime/ops/web_worker.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/runtime/ops/web_worker.rs b/runtime/ops/web_worker.rs new file mode 100644 index 000000000..d88330a04 --- /dev/null +++ b/runtime/ops/web_worker.rs @@ -0,0 +1,37 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +use crate::web_worker::WebWorkerHandle; +use crate::web_worker::WorkerEvent; +use deno_core::futures::channel::mpsc; +use deno_core::serde_json::json; + +pub fn init( + rt: &mut deno_core::JsRuntime, + sender: mpsc::Sender<WorkerEvent>, + handle: WebWorkerHandle, +) { + // Post message to host as guest worker. + let sender_ = sender.clone(); + super::reg_json_sync( + rt, + "op_worker_post_message", + move |_state, _args, bufs| { + assert_eq!(bufs.len(), 1, "Invalid number of arguments"); + let msg_buf: Box<[u8]> = (*bufs[0]).into(); + sender_ + .clone() + .try_send(WorkerEvent::Message(msg_buf)) + .expect("Failed to post message to host"); + Ok(json!({})) + }, + ); + + // Notify host that guest worker closes. + super::reg_json_sync(rt, "op_worker_close", move |_state, _args, _bufs| { + // Notify parent that we're finished + sender.clone().close_channel(); + // Terminate execution of current worker + handle.terminate(); + Ok(json!({})) + }); +} |