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