2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
mod sync_fetch;
|
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
use crate::web_worker::WebWorkerInternalHandle;
|
2021-08-16 08:29:54 -04:00
|
|
|
use crate::web_worker::WebWorkerType;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
|
|
|
|
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-08-16 08:29:54 -04:00
|
|
|
use self::sync_fetch::op_worker_sync_fetch;
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_worker_post_message::decl(),
|
|
|
|
op_worker_recv_message::decl(),
|
2021-05-02 19:22:57 -04:00
|
|
|
// Notify host that guest worker closes.
|
2022-03-14 13:44:15 -04:00
|
|
|
op_worker_close::decl(),
|
|
|
|
op_worker_get_type::decl(),
|
|
|
|
op_worker_sync_fetch::decl(),
|
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
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
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(())
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
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
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_worker_close(state: &mut OpState) {
|
2021-05-11 15:09:09 -04:00
|
|
|
// Notify parent that we're finished
|
|
|
|
let mut handle = state.borrow_mut::<WebWorkerInternalHandle>().clone();
|
|
|
|
|
|
|
|
handle.terminate();
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_worker_get_type(state: &mut OpState) -> WebWorkerType {
|
2021-08-16 08:29:54 -04:00
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
2022-05-13 04:36:31 -04:00
|
|
|
handle.worker_type
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|