Skip to content

Commit

Permalink
Driver: Add ability to clear all global event handlers.
Browse files Browse the repository at this point in the history
Closes #32. This should make event handler cleanup considerably easier depending on use case.
  • Loading branch information
FelixMcFelix committed Jan 17, 2021
1 parent dcb6ad9 commit 55b8e7f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/driver/tasks/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub(crate) async fn runner(_interconnect: Interconnect, evt_rx: Receiver<EventMe

global.fire_core_event(evt, ctx).await;
},
Ok(RemoveGlobalEvents) => {
global.remove_handlers();
},
Ok(AddTrack(store, state, handle)) => {
events.push(store);
states.push(state);
Expand Down
1 change: 1 addition & 0 deletions src/driver/tasks/message/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum CoreMessage {
AddTrack(Track),
SetBitrate(Bitrate),
AddEvent(EventData),
RemoveGlobalEvents,
SetConfig(Config),
Mute(bool),
Reconnect,
Expand Down
1 change: 1 addition & 0 deletions src/driver/tasks/message/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum EventMessage {
AddGlobalEvent(EventData),
AddTrackEvent(usize, EventData),
FireCoreEvent(CoreContext),
RemoveGlobalEvents,

AddTrack(EventStore, TrackState, TrackHandle),
ChangeState(usize, TrackStateChange),
Expand Down
3 changes: 3 additions & 0 deletions src/driver/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ async fn runner(mut config: Config, rx: Receiver<CoreMessage>, tx: Sender<CoreMe
Ok(CoreMessage::AddEvent(evt)) => {
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));
},
Expand Down
4 changes: 4 additions & 0 deletions src/events/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventStore>,
Expand Down

0 comments on commit 55b8e7f

Please sign in to comment.