Workers in Servo

As a result of the above, Servo ended-up with several worker “global scopes”, each with their own “run the event-loop” algorithm. While working on a related issue, I realized that I was often making the same changes in both of those worker event-loops, which was error prone. So I set to out to turn that code into one “generic worker event-loop”.

The ‘run_event_loop’ of the ServiceWorkerGlobalScope at https://github.com/servo/servo/blob/029715aba6f4a4c62b5a2759d47dda57954cc433/components/script/dom/serviceworkerglobalscope.rs

The ‘run_event_loop’ of the DedicatedWorkerGlobalScope at https://github.com/servo/servo/blob/029715aba6f4a4c62b5a2759d47dda57954cc433/components/script/dom/dedicatedworkerglobalscope.rs

This proved harder then I though, because what I needed was a run_worker_event_loop<T> sort of algorithm, where T was a type of “Worker Globalscope” and a replacement for &self. But since &self was previously used to access some capabilities of the “Worker Globalscope”, I would need T to implement a trait giving run_worker_event_loop access to these capabilities of T .

Yet those capabilities themselves required to be generic because each event-loop consisted of handling different messages on different channels, and then handling those messages as different type of event-loop specific events.

Furthermore, those run_event_loop differed in some ways, for example because the “dedicated worker global scope” takes this worker argument, and handles it as part of each iteration of the event-loop, whereas the “service worker global scope” lacks that concept.

The different stuff inside each type of “worker globalscope”

At first, I tried to have some sort of “double generic” setup looking like run_worker_event_loop<T<TimerMsg, WorkerMsg, Event>> , where I wanted to express something like run_worker_event_loop is generic over T , which itself is generic over <TimerMsg, WorkerMsg, Event> , and the Rust compiler didn’t seem to appreciate that approach a lot.