mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
4e1abb4f3a
To better reflect changes in error types in JS from #3662 this PR changes default error type used in ops from "ErrBox" to "OpError". "OpError" is a type that can be sent over to JSON; it has all information needed to construct error in JavaScript. That made "GetErrorKind" trait useless and so it was removed altogether. To provide compatibility with previous use of "ErrBox" an implementation of "From<ErrBox> for OpError" was added, however, it is an escape hatch and ops implementors should strive to use "OpError" directly.
66 lines
1.8 KiB
Rust
66 lines
1.8 KiB
Rust
// 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::worker::WorkerEvent;
|
|
use deno_core::*;
|
|
use futures;
|
|
use futures::channel::mpsc;
|
|
use futures::sink::SinkExt;
|
|
use std;
|
|
use std::convert::From;
|
|
|
|
pub fn web_worker_op<D>(
|
|
sender: mpsc::Sender<WorkerEvent>,
|
|
dispatcher: D,
|
|
) -> impl Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, OpError>
|
|
where
|
|
D: Fn(
|
|
&mpsc::Sender<WorkerEvent>,
|
|
Value,
|
|
Option<ZeroCopyBuf>,
|
|
) -> Result<JsonOp, OpError>,
|
|
{
|
|
move |args: Value,
|
|
zero_copy: Option<ZeroCopyBuf>|
|
|
-> Result<JsonOp, OpError> { dispatcher(&sender, args, zero_copy) }
|
|
}
|
|
|
|
pub fn init(i: &mut Isolate, s: &State, sender: &mpsc::Sender<WorkerEvent>) {
|
|
i.register_op(
|
|
"worker_post_message",
|
|
s.core_op(json_op(web_worker_op(
|
|
sender.clone(),
|
|
op_worker_post_message,
|
|
))),
|
|
);
|
|
i.register_op(
|
|
"worker_close",
|
|
s.core_op(json_op(web_worker_op(sender.clone(), op_worker_close))),
|
|
);
|
|
}
|
|
|
|
/// Post message to host as guest worker
|
|
fn op_worker_post_message(
|
|
sender: &mpsc::Sender<WorkerEvent>,
|
|
_args: Value,
|
|
data: Option<ZeroCopyBuf>,
|
|
) -> Result<JsonOp, OpError> {
|
|
let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice();
|
|
let mut sender = sender.clone();
|
|
let fut = sender.send(WorkerEvent::Message(d));
|
|
futures::executor::block_on(fut).expect("Failed to post message to host");
|
|
Ok(JsonOp::Sync(json!({})))
|
|
}
|
|
|
|
/// Notify host that guest worker closes
|
|
fn op_worker_close(
|
|
sender: &mpsc::Sender<WorkerEvent>,
|
|
_args: Value,
|
|
_data: Option<ZeroCopyBuf>,
|
|
) -> Result<JsonOp, OpError> {
|
|
let mut sender = sender.clone();
|
|
sender.close_channel();
|
|
Ok(JsonOp::Sync(json!({})))
|
|
}
|