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

@ -187,6 +187,13 @@ pub trait FetchTaskTarget {
fn process_response_eof(&mut self, response: &response::Response);
}
pub trait FetchResponseListener {
fn process_request_body(&mut self);
fn process_request_eof(&mut self);
fn process_response(&mut self, metadata: Result<Metadata, NetworkError>);
fn process_response_eof(&mut self, response: Result<Vec<u8>, NetworkError>);
}
impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
fn process_request_body(&mut self, _: &request::Request) {
let _ = self.send(FetchResponseMsg::ProcessRequestBody);
@ -217,6 +224,10 @@ impl FetchTaskTarget for IpcSender<FetchResponseMsg> {
}
}
pub trait Action<Listener> {
fn process(self, listener: &mut Listener);
}
/// A listener for asynchronous network events. Cancelling the underlying request is unsupported.
pub trait AsyncResponseListener {
/// The response headers for a request have been received.
@ -241,9 +252,9 @@ pub enum ResponseAction {
ResponseComplete(Result<(), NetworkError>)
}
impl ResponseAction {
impl<T: AsyncResponseListener> Action<T> for ResponseAction {
/// Execute the default action on a provided listener.
pub fn process(self, listener: &mut AsyncResponseListener) {
fn process(self, listener: &mut T) {
match self {
ResponseAction::HeadersAvailable(m) => listener.headers_available(m),
ResponseAction::DataAvailable(d) => listener.data_available(d),
@ -252,6 +263,18 @@ impl ResponseAction {
}
}
impl<T: FetchResponseListener> Action<T> for FetchResponseMsg {
/// Execute the default action on a provided listener.
fn process(self, listener: &mut T) {
match self {
FetchResponseMsg::ProcessRequestBody => listener.process_request_body(),
FetchResponseMsg::ProcessRequestEOF => listener.process_request_eof(),
FetchResponseMsg::ProcessResponse(meta) => listener.process_response(meta),
FetchResponseMsg::ProcessResponseEOF(data) => listener.process_response_eof(data),
}
}
}
/// A target for async networking events. Commonly used to dispatch a runnable event to another
/// thread storing the wrapped closure for later execution.
#[derive(Deserialize, Serialize)]