@@ -31,6 +31,7 @@ class Volume: StoreSubscriber {
3131
3232 var newLeftGain : Double = gain
3333 var newRightGain : Double = gain
34+
3435 if ( gain > 1 ) {
3536 if ( volumeSupported) {
3637 device. setVirtualMasterVolume ( 1.0 , direction: . playback)
@@ -92,9 +93,10 @@ class Volume: StoreSubscriber {
9293 Driver . device!. mute = shouldMute
9394 device. mute = shouldMute
9495
95-
9696 gainChanged. emit ( gain)
9797
98+ Application . ignoreNextVolumeEvent = false
99+ Application . ignoreNextDriverMuteEvent = false
98100 }
99101 }
100102
@@ -143,29 +145,43 @@ class Volume: StoreSubscriber {
143145 // MARK: - State
144146 typealias StoreSubscriberStateType = VolumeState
145147
148+ private let changeGainThread = DispatchQueue ( label: " change-volume " , qos: . userInteractive)
149+ private var latestChangeGainTask : DispatchWorkItem ?
150+ private func performOnChangeGainThread ( _ code: @escaping ( ) -> Void ) {
151+ latestChangeGainTask? . cancel ( )
152+ latestChangeGainTask = DispatchWorkItem ( block: code)
153+ changeGainThread. async ( execute: latestChangeGainTask!)
154+ }
155+
146156 func newState( state: VolumeState ) {
147157 if ( state. balance != balance) {
148- if ( state. transition) {
149- Transition . perform ( from: balance, to: state. balance) { balance in
150- self . balance = balance
158+ performOnChangeGainThread {
159+ if ( state. transition) {
160+ Transition . perform ( from: self . balance, to: state. balance) { balance in
161+ self . balance = balance
162+ }
163+ } else {
164+ self . balance = state. balance
151165 }
152- } else {
153- balance = state. balance
154166 }
155167 }
156168
157169 if ( state. gain != gain) {
158- if ( state. transition) {
159- Transition . perform ( from: gain, to: state. gain) { gain in
160- self . gain = gain
170+ performOnChangeGainThread {
171+ if ( state. transition) {
172+ Transition . perform ( from: self . gain, to: state. gain) { gain in
173+ self . gain = gain
174+ }
175+ } else {
176+ self . gain = state. gain
161177 }
162- } else {
163- gain = state. gain
164178 }
165179 }
166180
167181 if ( state. muted != muted) {
168- muted = state. muted
182+ performOnChangeGainThread {
183+ self . muted = state. muted
184+ }
169185 }
170186 }
171187
0 commit comments