2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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-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;
|
|
|
|
|
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
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:05:06 -04:00
|
|
|
#[op(deferred)]
|
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
|
|
|
}
|