mirror of
https://github.com/servo/servo.git
synced 2025-07-30 02:30:21 +01:00
Split out a receive_event method from the DedicatedWorkerGlobalScope event loop.
This commit is contained in:
parent
0565df8596
commit
17985e5296
1 changed files with 35 additions and 34 deletions
|
@ -44,7 +44,7 @@ use url::Url;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use std::mem::replace;
|
use std::mem::replace;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::mpsc::{Sender, Receiver, channel, Select};
|
use std::sync::mpsc::{Sender, Receiver, channel, Select, RecvError};
|
||||||
|
|
||||||
/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with
|
/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with
|
||||||
/// every message. While this SendableWorkerScriptChan is alive, the associated Worker object
|
/// every message. While this SendableWorkerScriptChan is alive, the associated Worker object
|
||||||
|
@ -92,6 +92,11 @@ impl<'a> Drop for AutoWorkerReset<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum MixedMessage {
|
||||||
|
FromWorker((TrustedWorkerAddress, ScriptMsg)),
|
||||||
|
FromDevtools(DevtoolScriptControlMsg),
|
||||||
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dedicatedworkerglobalscope
|
// https://html.spec.whatwg.org/multipage/#dedicatedworkerglobalscope
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct DedicatedWorkerGlobalScope {
|
pub struct DedicatedWorkerGlobalScope {
|
||||||
|
@ -143,7 +148,6 @@ impl DedicatedWorkerGlobalScope {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DedicatedWorkerGlobalScope {
|
impl DedicatedWorkerGlobalScope {
|
||||||
#[allow(unsafe_code)]
|
|
||||||
pub fn run_worker_scope(init: WorkerGlobalScopeInit,
|
pub fn run_worker_scope(init: WorkerGlobalScopeInit,
|
||||||
worker_url: Url,
|
worker_url: Url,
|
||||||
id: PipelineId,
|
id: PipelineId,
|
||||||
|
@ -215,39 +219,10 @@ impl DedicatedWorkerGlobalScope {
|
||||||
Reporter(reporter_sender)));
|
Reporter(reporter_sender)));
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MixedMessage {
|
|
||||||
FromWorker((TrustedWorkerAddress, ScriptMsg)),
|
|
||||||
FromDevtools(DevtoolScriptControlMsg),
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let worker_port = &global.r().receiver;
|
let event = match global.receive_event() {
|
||||||
let devtools_port = scope.devtools_port();
|
Ok(event) => event,
|
||||||
|
|
||||||
let event = {
|
|
||||||
let sel = Select::new();
|
|
||||||
let mut worker_handle = sel.handle(worker_port);
|
|
||||||
let mut devtools_handle = sel.handle(devtools_port);
|
|
||||||
unsafe {
|
|
||||||
worker_handle.add();
|
|
||||||
if scope.devtools_sender().is_some() {
|
|
||||||
devtools_handle.add();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let ret = sel.wait();
|
|
||||||
if ret == worker_handle.id() {
|
|
||||||
match worker_port.recv() {
|
|
||||||
Ok(stuff) => MixedMessage::FromWorker(stuff),
|
|
||||||
Err(_) => break,
|
Err(_) => break,
|
||||||
}
|
|
||||||
} else if ret == devtools_handle.id() {
|
|
||||||
match devtools_port.recv() {
|
|
||||||
Ok(stuff) => MixedMessage::FromDevtools(stuff),
|
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("unexpected select result!")
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
|
@ -313,9 +288,35 @@ impl<'a> DedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalScope {
|
||||||
trait PrivateDedicatedWorkerGlobalScopeHelpers {
|
trait PrivateDedicatedWorkerGlobalScopeHelpers {
|
||||||
fn handle_event(self, msg: ScriptMsg);
|
fn handle_event(self, msg: ScriptMsg);
|
||||||
fn dispatch_error_to_worker(self, &ErrorEvent);
|
fn dispatch_error_to_worker(self, &ErrorEvent);
|
||||||
|
fn receive_event(self) -> Result<MixedMessage, RecvError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalScope {
|
impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for &'a DedicatedWorkerGlobalScope {
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
fn receive_event(self) -> Result<MixedMessage, RecvError> {
|
||||||
|
let scope = WorkerGlobalScopeCast::from_ref(self);
|
||||||
|
let worker_port = &self.receiver;
|
||||||
|
let devtools_port = scope.devtools_port();
|
||||||
|
|
||||||
|
let sel = Select::new();
|
||||||
|
let mut worker_handle = sel.handle(worker_port);
|
||||||
|
let mut devtools_handle = sel.handle(devtools_port);
|
||||||
|
unsafe {
|
||||||
|
worker_handle.add();
|
||||||
|
if scope.devtools_sender().is_some() {
|
||||||
|
devtools_handle.add();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let ret = sel.wait();
|
||||||
|
if ret == worker_handle.id() {
|
||||||
|
Ok(MixedMessage::FromWorker(try!(worker_port.recv())))
|
||||||
|
} else if ret == devtools_handle.id() {
|
||||||
|
Ok(MixedMessage::FromDevtools(try!(devtools_port.recv())))
|
||||||
|
} else {
|
||||||
|
panic!("unexpected select result!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_event(self, msg: ScriptMsg) {
|
fn handle_event(self, msg: ScriptMsg) {
|
||||||
match msg {
|
match msg {
|
||||||
ScriptMsg::DOMMessage(data) => {
|
ScriptMsg::DOMMessage(data) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue