1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/runtime/ops/web_worker.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2021-01-10 21:59:07 -05:00
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
2020-09-05 20:34:02 -04:00
use crate::web_worker::WebWorkerHandle;
use crate::web_worker::WorkerEvent;
use deno_core::error::null_opbuf;
use deno_core::futures::channel::mpsc;
pub fn init(
rt: &mut deno_core::JsRuntime,
sender: mpsc::Sender<WorkerEvent>,
handle: WebWorkerHandle,
) {
2020-09-05 20:34:02 -04:00
// Post message to host as guest worker.
let sender_ = sender.clone();
super::reg_json_sync(
rt,
2020-02-25 09:14:27 -05:00
"op_worker_post_message",
move |_state, _args: (), buf| {
let buf = buf.ok_or_else(null_opbuf)?;
let msg_buf: Box<[u8]> = (*buf).into();
2020-09-05 20:34:02 -04:00
sender_
.clone()
.try_send(WorkerEvent::Message(msg_buf))
.expect("Failed to post message to host");
Ok(())
2020-09-05 20:34:02 -04:00
},
);
2020-09-05 20:34:02 -04:00
// 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(())
},
);
}