// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{JsonOp, Value}; use crate::op_error::OpError; use crate::ops::json_op; use crate::state::State; use crate::web_worker::WebWorkerHandle; use crate::worker::WorkerEvent; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use futures::channel::mpsc; use std::convert::From; pub fn web_worker_op( sender: mpsc::Sender, dispatcher: D, ) -> impl Fn(&mut CoreIsolate, Value, Option) -> Result where D: Fn( &mpsc::Sender, Value, Option, ) -> Result, { move |_isolate: &mut CoreIsolate, args: Value, zero_copy: Option| -> Result { dispatcher(&sender, args, zero_copy) } } pub fn web_worker_op2( handle: WebWorkerHandle, sender: mpsc::Sender, dispatcher: D, ) -> impl Fn(&mut CoreIsolate, Value, Option) -> Result where D: Fn( WebWorkerHandle, &mpsc::Sender, Value, Option, ) -> Result, { move |_isolate: &mut CoreIsolate, args: Value, zero_copy: Option| -> Result { dispatcher(handle.clone(), &sender, args, zero_copy) } } pub fn init( i: &mut CoreIsolate, s: &State, sender: &mpsc::Sender, handle: WebWorkerHandle, ) { i.register_op( "op_worker_post_message", s.core_op(json_op(web_worker_op( sender.clone(), op_worker_post_message, ))), ); i.register_op( "op_worker_close", s.core_op(json_op(web_worker_op2( handle, sender.clone(), op_worker_close, ))), ); } /// Post message to host as guest worker fn op_worker_post_message( sender: &mpsc::Sender, _args: Value, data: Option, ) -> Result { let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice(); let mut sender = sender.clone(); sender .try_send(WorkerEvent::Message(d)) .expect("Failed to post message to host"); Ok(JsonOp::Sync(json!({}))) } /// Notify host that guest worker closes fn op_worker_close( handle: WebWorkerHandle, sender: &mpsc::Sender, _args: Value, _data: Option, ) -> Result { let mut sender = sender.clone(); // Notify parent that we're finished sender.close_channel(); // Terminate execution of current worker handle.terminate(); Ok(JsonOp::Sync(json!({}))) }