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;
|
2021-06-22 10:30:16 -04:00
|
|
|
use crate::web_worker::WorkerControlEvent;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::op_async;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
2021-06-22 10:30:16 -04:00
|
|
|
use deno_core::CancelFuture;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::OpState;
|
2021-06-22 10:30:16 -04:00
|
|
|
use deno_web::JsMessageData;
|
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)),
|
2021-06-22 10:30:16 -04:00
|
|
|
("op_worker_recv_message", op_async(op_worker_recv_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,
|
2021-06-22 10:30:16 -04:00
|
|
|
data: JsMessageData,
|
2021-05-11 15:09:09 -04:00
|
|
|
_: (),
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
2021-06-22 10:30:16 -04:00
|
|
|
handle.port.send(state, data)?;
|
2021-05-11 15:09:09 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
async fn op_worker_recv_message(
|
2021-05-11 15:09:09 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
_: (),
|
|
|
|
_: (),
|
2021-06-22 10:30:16 -04:00
|
|
|
) -> Result<Option<JsMessageData>, AnyError> {
|
|
|
|
let handle = {
|
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<WebWorkerInternalHandle>().clone()
|
2021-05-11 15:09:09 -04:00
|
|
|
};
|
2021-06-22 10:30:16 -04:00
|
|
|
handle
|
|
|
|
.port
|
|
|
|
.recv(state.clone())
|
|
|
|
.or_cancel(handle.cancel)
|
|
|
|
.await?
|
2021-05-11 15:09:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
fn op_worker_unhandled_error(
|
|
|
|
state: &mut OpState,
|
|
|
|
message: String,
|
|
|
|
_: (),
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let sender = state.borrow::<WebWorkerInternalHandle>().clone();
|
|
|
|
sender
|
2021-06-22 10:30:16 -04:00
|
|
|
.post_event(WorkerControlEvent::Error(generic_error(message)))
|
2021-05-11 15:09:09 -04:00
|
|
|
.expect("Failed to propagate error event to parent worker");
|
|
|
|
Ok(())
|
|
|
|
}
|