2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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;
|
2023-09-14 17:05:18 -04:00
|
|
|
use deno_core::op2;
|
2021-06-22 10:30:16 -04:00
|
|
|
use deno_core::CancelFuture;
|
2021-05-11 15:09:09 -04:00
|
|
|
use deno_core::OpState;
|
2021-06-22 10:30:16 -04:00
|
|
|
use deno_web::JsMessageData;
|
2024-10-22 04:41:08 -04:00
|
|
|
use deno_web::MessagePortError;
|
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;
|
2024-10-22 04:41:08 -04:00
|
|
|
pub use sync_fetch::SyncFetchError;
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_web_worker,
|
|
|
|
ops = [
|
|
|
|
op_worker_post_message,
|
|
|
|
op_worker_recv_message,
|
|
|
|
// Notify host that guest worker closes.
|
|
|
|
op_worker_close,
|
|
|
|
op_worker_get_type,
|
|
|
|
op_worker_sync_fetch,
|
2023-03-18 18:30:04 -04:00
|
|
|
],
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2021-05-11 15:09:09 -04:00
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2]
|
2021-05-11 15:09:09 -04:00
|
|
|
fn op_worker_post_message(
|
|
|
|
state: &mut OpState,
|
2023-09-14 17:05:18 -04:00
|
|
|
#[serde] data: JsMessageData,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<(), MessagePortError> {
|
2021-05-11 15:09:09 -04:00
|
|
|
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
|
2024-10-22 04:41:08 -04:00
|
|
|
handle.port.send(state, data)
|
2021-05-11 15:09:09 -04:00
|
|
|
}
|
|
|
|
|
2023-10-25 07:43:38 -04:00
|
|
|
#[op2(async(lazy), fast)]
|
2023-10-07 11:34:03 -04:00
|
|
|
#[serde]
|
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>>,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<Option<JsMessageData>, MessagePortError> {
|
2021-06-22 10:30:16 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2(fast)]
|
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();
|
|
|
|
}
|
|
|
|
|
2023-09-14 17:05:18 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
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
|
|
|
}
|