diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 5e0ad7403..1c612d81f 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -238,6 +238,12 @@ impl Driver { self.send(CoreMessage::AddEvent(EventData::new(event, action))); } + /// Removes all global event handlers from an audio context. + #[instrument(skip(self))] + pub fn remove_all_global_events(&mut self) { + self.send(CoreMessage::RemoveGlobalEvents); + } + /// Sends a message to the inner tasks, restarting it if necessary. fn send(&mut self, status: CoreMessage) { // Restart thread if it errored. diff --git a/src/driver/tasks/events.rs b/src/driver/tasks/events.rs index 95ec677b7..44468250d 100644 --- a/src/driver/tasks/events.rs +++ b/src/driver/tasks/events.rs @@ -43,6 +43,9 @@ pub(crate) async fn runner(_interconnect: Interconnect, evt_rx: Receiver { + global.remove_handlers(); + }, Ok(AddTrack(store, state, handle)) => { events.push(store); states.push(state); diff --git a/src/driver/tasks/message/core.rs b/src/driver/tasks/message/core.rs index dc02f541b..76bf4dec9 100644 --- a/src/driver/tasks/message/core.rs +++ b/src/driver/tasks/message/core.rs @@ -18,6 +18,7 @@ pub enum CoreMessage { AddTrack(Track), SetBitrate(Bitrate), AddEvent(EventData), + RemoveGlobalEvents, SetConfig(Config), Mute(bool), Reconnect, diff --git a/src/driver/tasks/message/events.rs b/src/driver/tasks/message/events.rs index c7989eae8..2125fd8a9 100644 --- a/src/driver/tasks/message/events.rs +++ b/src/driver/tasks/message/events.rs @@ -12,6 +12,7 @@ pub enum EventMessage { AddGlobalEvent(EventData), AddTrackEvent(usize, EventData), FireCoreEvent(CoreContext), + RemoveGlobalEvents, AddTrack(EventStore, TrackState, TrackHandle), ChangeState(usize, TrackStateChange), diff --git a/src/driver/tasks/mod.rs b/src/driver/tasks/mod.rs index 7dd8960e3..838d501b2 100644 --- a/src/driver/tasks/mod.rs +++ b/src/driver/tasks/mod.rs @@ -109,6 +109,9 @@ async fn runner(mut config: Config, rx: Receiver, tx: Sender { let _ = interconnect.events.send(EventMessage::AddGlobalEvent(evt)); }, + Ok(CoreMessage::RemoveGlobalEvents) => { + let _ = interconnect.events.send(EventMessage::RemoveGlobalEvents); + }, Ok(CoreMessage::Mute(m)) => { let _ = interconnect.mixer.send(MixerMessage::SetMute(m)); }, diff --git a/src/events/store.rs b/src/events/store.rs index 17152bee3..67057df26 100644 --- a/src/events/store.rs +++ b/src/events/store.rs @@ -164,6 +164,10 @@ impl GlobalEvents { holder.push(index); } + pub(crate) fn remove_handlers(&mut self) { + self.store = EventStore::new(); + } + pub(crate) async fn tick( &mut self, events: &mut Vec,