2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-11-26 09:17:45 -05:00
|
|
|
use crate::web_worker::WebWorkerHandle;
|
|
|
|
use crate::web_worker::WorkerEvent;
|
2021-04-02 09:47:57 -04:00
|
|
|
use deno_core::error::null_opbuf;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::futures::channel::mpsc;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::op_sync;
|
|
|
|
use deno_core::Extension;
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
|
|
|
(
|
|
|
|
"op_worker_post_message",
|
|
|
|
op_sync(move |state, _args: (), buf| {
|
|
|
|
let buf = buf.ok_or_else(null_opbuf)?;
|
|
|
|
let msg_buf: Box<[u8]> = (*buf).into();
|
|
|
|
let mut sender = state.borrow::<mpsc::Sender<WorkerEvent>>().clone();
|
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::Message(msg_buf))
|
|
|
|
.expect("Failed to post message to host");
|
|
|
|
Ok(())
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
// Notify host that guest worker closes.
|
|
|
|
(
|
|
|
|
"op_worker_close",
|
|
|
|
op_sync(move |state, _args: (), _bufs| {
|
|
|
|
// Notify parent that we're finished
|
|
|
|
let mut sender = state.borrow::<mpsc::Sender<WorkerEvent>>().clone();
|
|
|
|
sender.close_channel();
|
|
|
|
// Terminate execution of current worker
|
|
|
|
let handle = state.borrow::<WebWorkerHandle>();
|
|
|
|
handle.terminate();
|
|
|
|
Ok(())
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
.build()
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|