1
- use crate :: context:: AudioNodeId ;
1
+ use crate :: context:: { AudioContextState , AudioNodeId } ;
2
2
use crate :: { AudioBuffer , AudioRenderCapacityEvent } ;
3
3
4
4
use std:: any:: Any ;
@@ -55,6 +55,7 @@ pub(crate) enum EventPayload {
55
55
ProcessorError ( ErrorEvent ) ,
56
56
Diagnostics ( Vec < u8 > ) ,
57
57
Message ( Box < dyn Any + Send + ' static > ) ,
58
+ AudioContextState ( AudioContextState ) ,
58
59
}
59
60
60
61
#[ derive( Debug ) ]
@@ -78,10 +79,10 @@ impl EventDispatch {
78
79
}
79
80
}
80
81
81
- pub fn state_change ( ) -> Self {
82
+ pub fn state_change ( state : AudioContextState ) -> Self {
82
83
EventDispatch {
83
84
type_ : EventType :: StateChange ,
84
- payload : EventPayload :: None ,
85
+ payload : EventPayload :: AudioContextState ( state ) ,
85
86
}
86
87
}
87
88
@@ -130,11 +131,22 @@ impl EventLoop {
130
131
}
131
132
132
133
pub fn run ( & self , event_channel : Receiver < EventDispatch > ) {
134
+ log:: debug!( "Entering event loop" ) ;
133
135
let self_clone = self . clone ( ) ;
134
136
135
- std:: thread:: spawn ( move || loop {
136
- // this thread is dedicated to event handling so we can block
137
- for event in event_channel. iter ( ) {
137
+ std:: thread:: spawn ( move || {
138
+ // This thread is dedicated to event handling so we can block
139
+ for mut event in event_channel. iter ( ) {
140
+ // Terminate the event loop when the audio context is closing
141
+ let mut terminate = false ;
142
+ if matches ! (
143
+ event. payload,
144
+ EventPayload :: AudioContextState ( AudioContextState :: Closed )
145
+ ) {
146
+ event. payload = EventPayload :: None ; // the statechange handler takes no argument
147
+ terminate = true ;
148
+ }
149
+
138
150
let mut event_handler_lock = self_clone. event_handlers . lock ( ) . unwrap ( ) ;
139
151
let callback_option = event_handler_lock. remove ( & event. type_ ) ;
140
152
drop ( event_handler_lock) ; // release Mutex while running callback
@@ -152,7 +164,13 @@ impl EventLoop {
152
164
}
153
165
} ;
154
166
}
167
+
168
+ if terminate {
169
+ break ;
170
+ }
155
171
}
172
+
173
+ log:: debug!( "Event loop has terminated" ) ;
156
174
} ) ;
157
175
}
158
176
0 commit comments