Abstractify the Listener abstractions further

This commit is contained in:
Manish Goregaokar 2016-06-01 14:59:55 +05:30
parent 2cbc8dee25
commit d4f428ad95
7 changed files with 47 additions and 17 deletions

View file

@ -227,7 +227,7 @@ impl HTMLLinkElement {
sender: action_sender,
};
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
listener.notify(message.to().unwrap());
listener.notify_action(message.to().unwrap());
});
if self.parser_inserted.get() {

View file

@ -484,7 +484,7 @@ impl HTMLMediaElement {
sender: action_sender,
};
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
listener.notify(message.to().unwrap());
listener.notify_action(message.to().unwrap());
});
// FIXME: we're supposed to block the load event much earlier than now

View file

@ -314,7 +314,7 @@ impl HTMLScriptElement {
sender: action_sender,
};
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
listener.notify(message.to().unwrap());
listener.notify_action(message.to().unwrap());
});
doc.load_async(LoadType::Script(url), response_target);

View file

@ -302,7 +302,7 @@ impl XMLHttpRequest {
sender: action_sender,
};
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
listener.notify(message.to().unwrap());
listener.notify_action(message.to().unwrap());
});
core_resource_thread.send(Load(load_data, LoadConsumer::Listener(response_target), None)).unwrap();
}

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use net_traits::{AsyncResponseListener, ResponseAction};
use net_traits::{Action, AsyncResponseListener, ResponseAction};
use script_runtime::ScriptThreadEventCategory::NetworkEvent;
use script_runtime::{CommonScriptMsg, ScriptChan};
use script_thread::Runnable;
@ -10,13 +10,13 @@ use std::sync::{Arc, Mutex};
/// An off-thread sink for async network event runnables. All such events are forwarded to
/// a target thread, where they are invoked on the provided context object.
pub struct NetworkListener<T: AsyncResponseListener + PreInvoke + Send + 'static> {
pub context: Arc<Mutex<T>>,
pub struct NetworkListener<Listener: PreInvoke + Send + 'static> {
pub context: Arc<Mutex<Listener>>,
pub script_chan: Box<ScriptChan + Send>,
}
impl<T: AsyncResponseListener + PreInvoke + Send + 'static> NetworkListener<T> {
pub fn notify(&self, action: ResponseAction) {
impl<Listener: PreInvoke + Send + 'static> NetworkListener<Listener> {
pub fn notify<A: Action<Listener>+Send+'static>(&self, action: A) {
if let Err(err) = self.script_chan.send(CommonScriptMsg::RunnableMsg(NetworkEvent, box ListenerRunnable {
context: self.context.clone(),
action: action,
@ -26,6 +26,13 @@ impl<T: AsyncResponseListener + PreInvoke + Send + 'static> NetworkListener<T> {
}
}
// helps type inference
impl<Listener: AsyncResponseListener + PreInvoke + Send + 'static> NetworkListener<Listener> {
pub fn notify_action(&self, action: ResponseAction) {
self.notify(action);
}
}
/// A gating mechanism that runs before invoking the runnable on the target thread.
/// If the `should_invoke` method returns false, the runnable is discarded without
/// being invoked.
@ -36,13 +43,13 @@ pub trait PreInvoke {
}
/// A runnable for moving the async network events between threads.
struct ListenerRunnable<T: AsyncResponseListener + PreInvoke + Send> {
context: Arc<Mutex<T>>,
action: ResponseAction,
struct ListenerRunnable<A: Action<Listener>+Send+'static, Listener: PreInvoke + Send> {
context: Arc<Mutex<Listener>>,
action: A,
}
impl<T: AsyncResponseListener + PreInvoke + Send> Runnable for ListenerRunnable<T> {
fn handler(self: Box<ListenerRunnable<T>>) {
impl<A: Action<Listener>+Send+'static, Listener: PreInvoke + Send> Runnable for ListenerRunnable<A, Listener> {
fn handler(self: Box<ListenerRunnable<A, Listener>>) {
let this = *self;
let mut context = this.context.lock().unwrap();
if context.should_invoke() {

View file

@ -1947,7 +1947,7 @@ impl ScriptThread {
script_chan: self.chan.clone(),
};
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
listener.notify(message.to().unwrap());
listener.notify_action(message.to().unwrap());
});
let response_target = AsyncResponseTarget {
sender: action_sender,