diff options
author | Agustin Chiappe Berrini <jnieve@gmail.com> | 2018-09-24 19:31:59 -0400 |
---|---|---|
committer | Agustin Chiappe Berrini <jnieve@gmail.com> | 2018-11-14 06:36:44 -0500 |
commit | 75eb94afcaae2f868ecccba5b5dcea4066998d7a (patch) | |
tree | c70e246d752eaee753ca4b59b27e27218a0cdf5e /components/script/dom/htmlscriptelement.rs | |
parent | 14bc8ab7542287a18ecdede1ca7ee2af077eb8a9 (diff) | |
download | servo-75eb94afcaae2f868ecccba5b5dcea4066998d7a.tar.gz servo-75eb94afcaae2f868ecccba5b5dcea4066998d7a.zip |
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<T: TaskSource>(&self, name: TaskSourceName) -> Box<T>
fn task_source_with_canceller<T: TaskSource>(&self, name: TaskSourceName)
-> (Box<T>, 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)
Diffstat (limited to 'components/script/dom/htmlscriptelement.rs')
-rw-r--r-- | components/script/dom/htmlscriptelement.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs index 47a4950b3d6..d93b5357e24 100644 --- a/components/script/dom/htmlscriptelement.rs +++ b/components/script/dom/htmlscriptelement.rs @@ -25,7 +25,6 @@ use crate::dom::node::{document_from_node, window_from_node}; use crate::dom::node::{ChildrenMutation, CloneChildrenFlag, Node}; use crate::dom::virtualmethods::VirtualMethods; use crate::network_listener::{NetworkListener, PreInvoke}; -use crate::task_source::TaskSourceName; use dom_struct::dom_struct; use encoding_rs::Encoding; use html5ever::{LocalName, Prefix}; @@ -294,10 +293,14 @@ fn fetch_a_classic_script( })); let (action_sender, action_receiver) = ipc::channel().unwrap(); + let (task_source, canceller) = doc + .window() + .task_manager() + .networking_task_source_with_canceller(); let listener = NetworkListener { - context: context, - task_source: doc.window().networking_task_source(), - canceller: Some(doc.window().task_canceller(TaskSourceName::Networking)), + context, + task_source, + canceller: Some(canceller), }; ROUTER.add_route( @@ -623,11 +626,10 @@ impl HTMLScriptElement { pub fn queue_error_event(&self) { let window = window_from_node(self); - window.dom_manipulation_task_source().queue_simple_event( - self.upcast(), - atom!("error"), - &window, - ); + window + .task_manager() + .dom_manipulation_task_source() + .queue_simple_event(self.upcast(), atom!("error"), &window); } pub fn dispatch_load_event(&self) { |