script: Move TaskManager to GlobalScope (#34827)

This is a simplification of the internal `TaskQueue` API that moves the
`TaskManager` to the `GlobalScope` itself. In addition, the handling of
cancellers is moved to the `TaskManager` as well. This means that no
arguments other than the `task` are necessary for queueing tasks, which
makes the API a lot easier to use and cleaner.

`TaskSource` now also keeps a copy of the canceller with it, so that
they always know the proper way to cancel any tasks queued on them.

There is one complication here. The event loop `sender` for dedicated
workers is constantly changing as it is set to `None` when not handling
messages. This is because this sender keeps a handle to the main
thread's `Worker` object, preventing garbage collection while any
messages are still in flight or being handled. This change allows
setting the `sender` on the `TaskManager` to `None` to allow proper
garbabge collection.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-04 09:41:50 +01:00 committed by GitHub
parent 75a22cfe2e
commit b2eda71952
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
54 changed files with 1060 additions and 1516 deletions

View file

@ -63,38 +63,30 @@ impl TextTrackList {
self.dom_tracks.borrow_mut().push(Dom::from_ref(track));
let this = Trusted::new(self);
let (source, canceller) = &self
.global()
.as_window()
.task_manager()
.media_element_task_source_with_canceller();
let task_source = self.global().task_manager().media_element_task_source();
let idx = match self.find(track) {
Some(t) => t,
None => return,
let Some(idx) = self.find(track) else {
return;
};
let _ = source.queue_with_canceller(
task!(track_event_queue: move || {
let this = this.root();
let _ = task_source.queue(task!(track_event_queue: move || {
let this = this.root();
if let Some(track) = this.item(idx) {
let event = TrackEvent::new(
&this.global(),
atom!("addtrack"),
false,
false,
&Some(VideoTrackOrAudioTrackOrTextTrack::TextTrack(
DomRoot::from_ref(&track)
)),
CanGc::note()
);
if let Some(track) = this.item(idx) {
let event = TrackEvent::new(
&this.global(),
atom!("addtrack"),
false,
false,
&Some(VideoTrackOrAudioTrackOrTextTrack::TextTrack(
DomRoot::from_ref(&track)
)),
CanGc::note()
);
event.upcast::<Event>().fire(this.upcast::<EventTarget>(), CanGc::note());
}
}),
canceller,
);
event.upcast::<Event>().fire(this.upcast::<EventTarget>(), CanGc::note());
}
}));
track.add_track_list(self);
}
}