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
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
use crate::web_worker::WebWorkerInternalHandle;
|
2020-11-26 09:17:45 -05:00
|
|
|
use crate::web_worker::WorkerEvent;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::error::generic_error;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::error::null_opbuf;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::op_async;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::OpState;
|
2021-05-06 13:32:03 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2021-05-11 15:09:09 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2021-05-11 15:09:09 -04:00
|
|
|
("op_worker_post_message", op_sync(op_worker_post_message)),
|
|
|
|
("op_worker_get_message", op_async(op_worker_get_message)),
|
2021-05-02 19:22:57 -04:00
|
|
|
// Notify host that guest worker closes.
|
2021-05-11 15:09:09 -04:00
|
|
|
("op_worker_close", op_sync(op_worker_close)),
|
|
|
|
// Notify host that guest worker has unhandled error.
|
2021-05-02 19:22:57 -04:00
|
|
|
(
|
2021-05-11 15:09:09 -04:00
|
|
|
"op_worker_unhandled_error",
|
|
|
|
op_sync(op_worker_unhandled_error),
|
2021-05-02 19:22:57 -04:00
|
|
|
),
|
|
|
|
])
|
|
|
|
.build()
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2021-05-11 15:09:09 -04:00
|
|
|
|
|
|
|
fn op_worker_post_message(
|
|
|
|
state: &mut OpState,
|
|
|
|
_: (),
|
|
|
|
buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let buf = buf.ok_or_else(null_opbuf)?;
|
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
|
|
|
handle
|
|
|
|
.post_event(WorkerEvent::Message(buf))
|
|
|
|
.expect("Failed to post message to host");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_worker_get_message(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
_: (),
|
|
|
|
_: (),
|
|
|
|
) -> Result<ZeroCopyBuf, AnyError> {
|
|
|
|
let temp = {
|
|
|
|
let a = state.borrow();
|
|
|
|
a.borrow::<WebWorkerInternalHandle>().clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let maybe_data = temp.get_message().await;
|
|
|
|
|
|
|
|
Ok(maybe_data.unwrap_or_else(ZeroCopyBuf::empty))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
|
|
|
fn op_worker_close(state: &mut OpState, _: (), _: ()) -> Result<(), AnyError> {
|
|
|
|
// Notify parent that we're finished
|
|
|
|
let mut handle = state.borrow_mut::<WebWorkerInternalHandle>().clone();
|
|
|
|
|
|
|
|
handle.terminate();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A worker that encounters an uncaught error will pass this error
|
|
|
|
/// to its parent worker using this op. The parent worker will use
|
|
|
|
/// this same op to pass the error to its own parent (in case
|
|
|
|
/// `e.preventDefault()` was not called in `worker.onerror`). This
|
|
|
|
/// is done until the error reaches the root/ main worker.
|
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
|
|
|
fn op_worker_unhandled_error(
|
|
|
|
state: &mut OpState,
|
|
|
|
message: String,
|
|
|
|
_: (),
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let sender = state.borrow::<WebWorkerInternalHandle>().clone();
|
|
|
|
sender
|
|
|
|
.post_event(WorkerEvent::Error(generic_error(message)))
|
|
|
|
.expect("Failed to propagate error event to parent worker");
|
|
|
|
Ok(())
|
|
|
|
}
|