1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00
denoland-deno/runtime/ops/web_worker.rs
Matt Mastracci 0a3d329dd8
Reland refactor(cli): use new sanitizer for resources (#22226)
Originally in #22125
Reverted in #22153 because of #22148

Fixed in deno_core https://github.com/denoland/deno_core/pull/538

Test plan: 

1. Check out: https://github.com/poolifier/poolifier-deno.git

2. `PATH=.../deno/target/release/:$PATH deno task test`

3. `ok | 13 passed (188 steps) | 0 failed (18s)`
2024-02-05 12:21:29 -07:00

68 lines
1.5 KiB
Rust

// Copyright 2018-2024 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::op2;
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,
],
);
#[op2]
fn op_worker_post_message(
state: &mut OpState,
#[serde] data: JsMessageData,
) -> Result<(), AnyError> {
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
handle.port.send(state, data)?;
Ok(())
}
#[op2(async(lazy), fast)]
#[serde]
async fn op_worker_recv_message(
state: Rc<RefCell<OpState>>,
) -> Result<Option<JsMessageData>, AnyError> {
let handle = {
let state = state.borrow();
state.borrow::<WebWorkerInternalHandle>().clone()
};
handle
.port
.recv(state.clone())
.or_cancel(handle.cancel)
.await?
}
#[op2(fast)]
fn op_worker_close(state: &mut OpState) {
// Notify parent that we're finished
let mut handle = state.borrow_mut::<WebWorkerInternalHandle>().clone();
handle.terminate();
}
#[op2]
#[serde]
fn op_worker_get_type(state: &mut OpState) -> WebWorkerType {
let handle = state.borrow::<WebWorkerInternalHandle>().clone();
handle.worker_type
}