2020-01-21 03:49:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-09 18:15:17 -04:00
|
|
|
use crate::web_worker::WebWorkerHandle;
|
2020-02-11 04:04:59 -05:00
|
|
|
use crate::worker::WorkerEvent;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::OpRegistry;
|
2020-02-11 04:04:59 -05:00
|
|
|
use futures::channel::mpsc;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2020-04-09 18:15:17 -04:00
|
|
|
pub fn init(
|
2020-08-18 12:30:13 -04:00
|
|
|
s: &Rc<State>,
|
2020-04-09 18:15:17 -04:00
|
|
|
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();
|
|
|
|
s.register_op_json_sync(
|
2020-02-25 09:14:27 -05:00
|
|
|
"op_worker_post_message",
|
2020-09-05 20:34:02 -04:00
|
|
|
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!({}))
|
|
|
|
},
|
2020-01-21 03:49:47 -05:00
|
|
|
);
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
// Notify host that guest worker closes.
|
|
|
|
let sender_ = sender.clone();
|
|
|
|
s.register_op_json_sync("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!({}))
|
|
|
|
});
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|