2020-01-21 03:49:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use crate::ops;
|
|
|
|
use crate::state::ThreadSafeState;
|
2020-01-21 11:50:06 -05:00
|
|
|
use crate::worker::Worker;
|
2020-01-21 03:49:47 -05:00
|
|
|
use crate::worker::WorkerChannels;
|
|
|
|
use deno_core;
|
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::StartupData;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use std::future::Future;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2020-01-21 03:49:47 -05:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
/// This worker is implementation of `Worker` Web API
|
|
|
|
///
|
|
|
|
/// At the moment this type of worker supports only
|
|
|
|
/// communication with parent and creating new workers.
|
|
|
|
///
|
|
|
|
/// Each `WebWorker` is either a child of `MainWorker` or other
|
|
|
|
/// `WebWorker`.
|
2020-01-21 03:49:47 -05:00
|
|
|
#[derive(Clone)]
|
2020-01-21 11:50:06 -05:00
|
|
|
pub struct WebWorker(Worker);
|
2020-01-21 03:49:47 -05:00
|
|
|
|
|
|
|
impl WebWorker {
|
|
|
|
pub fn new(
|
|
|
|
name: String,
|
|
|
|
startup_data: StartupData,
|
|
|
|
state: ThreadSafeState,
|
|
|
|
external_channels: WorkerChannels,
|
|
|
|
) -> Self {
|
2020-01-21 11:50:06 -05:00
|
|
|
let state_ = state.clone();
|
|
|
|
let worker = Worker::new(name, startup_data, state_, external_channels);
|
|
|
|
{
|
|
|
|
let mut isolate = worker.isolate.try_lock().unwrap();
|
|
|
|
ops::web_worker::init(&mut isolate, &state);
|
|
|
|
ops::worker_host::init(&mut isolate, &state);
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
Self(worker)
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl Deref for WebWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
2020-01-21 03:49:47 -05:00
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl DerefMut for WebWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for WebWorker {
|
|
|
|
type Output = Result<(), ErrBox>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
2020-01-21 11:50:06 -05:00
|
|
|
inner.0.poll_unpin(cx)
|
2020-01-21 03:49:47 -05:00
|
|
|
}
|
|
|
|
}
|