// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. mod sync_fetch; use crate::web_worker::WebWorkerInternalHandle; use crate::web_worker::WebWorkerType; use deno_core::error::AnyError; use deno_core::op; use deno_core::CancelFuture; use deno_core::OpState; use deno_web::JsMessageData; use std::cell::RefCell; use std::rc::Rc; use self::sync_fetch::op_worker_sync_fetch; 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, ], customizer = |ext: &mut deno_core::ExtensionBuilder| { ext.force_op_registration(); }, ); #[op] fn op_worker_post_message( state: &mut OpState, data: JsMessageData, ) -> Result<(), AnyError> { let handle = state.borrow::().clone(); handle.port.send(state, data)?; Ok(()) } #[op(deferred)] async fn op_worker_recv_message( state: Rc>, ) -> Result, AnyError> { let handle = { let state = state.borrow(); state.borrow::().clone() }; handle .port .recv(state.clone()) .or_cancel(handle.cancel) .await? } #[op] fn op_worker_close(state: &mut OpState) { // Notify parent that we're finished let mut handle = state.borrow_mut::().clone(); handle.terminate(); } #[op] fn op_worker_get_type(state: &mut OpState) -> WebWorkerType { let handle = state.borrow::().clone(); handle.worker_type }