From 75eb94afcaae2f868ecccba5b5dcea4066998d7a Mon Sep 17 00:00:00 2001 From: Agustin Chiappe Berrini Date: Mon, 24 Sep 2018 19:31:59 -0400 Subject: Unify the task source and task canceller API I moved away from the `Window` struct all the logic to handle task sources, into a new struct called `TaskManager`. In a happy world, I'd be able to just have there two functions, of the types: ```rust fn task_source(&self, name: TaskSourceName) -> Box fn task_source_with_canceller(&self, name: TaskSourceName) -> (Box, TaskSourceCanceller) ``` And not so much duplicated code. However, because TaskSource can't be a trait object (because it has generic type parameters), that's not possible. Instead, I decided to reduce duplicated logic through macros. For reasons[1], I have to pass both the name of the function with canceller and the name of the function without, as I'm not able to concatenate them in the macro itself. I could probably use `concat_idents` to create both types already defined and reduce the amount of arguments by one, but that macro is nightly only. At the same time, not being able to declare macros inside `impl` forces me to pass `self` as an argument. All this makes this solution more verbose than it would be ideally. It does reduce duplication, but it doesn't reduce the size of the file. [1](https://github.com/rust-lang/rust/issues/29599) --- components/script/layout_image.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'components/script/layout_image.rs') diff --git a/components/script/layout_image.rs b/components/script/layout_image.rs index 68a3e788cf4..39670eefabb 100644 --- a/components/script/layout_image.rs +++ b/components/script/layout_image.rs @@ -10,7 +10,6 @@ use crate::dom::bindings::reflector::DomObject; use crate::dom::node::{document_from_node, Node}; use crate::network_listener::{NetworkListener, PreInvoke}; -use crate::task_source::TaskSourceName; use ipc_channel::ipc; use ipc_channel::router::ROUTER; use net_traits::image_cache::{ImageCache, PendingImageId}; @@ -57,13 +56,16 @@ pub fn fetch_image_for_layout( })); let document = document_from_node(node); - let window = document.window(); let (action_sender, action_receiver) = ipc::channel().unwrap(); + let (task_source, canceller) = document + .window() + .task_manager() + .networking_task_source_with_canceller(); let listener = NetworkListener { - context: context, - task_source: window.networking_task_source(), - canceller: Some(window.task_canceller(TaskSourceName::Networking)), + context, + task_source, + canceller: Some(canceller), }; ROUTER.add_route( action_receiver.to_opaque(), -- cgit v1.2.3