@@ -97,15 +97,10 @@ export default class VoiceChannelStore extends EventEmitter {
9797 messaging . on ( `action:${ ElementWidgetActions . UnmuteVideo } ` , this . onUnmuteVideo ) ;
9898
9999 // Actually perform the join
100- const waitForJoin = new Promise < void > ( resolve =>
101- messaging . once ( `action:${ ElementWidgetActions . JoinCall } ` , ( ev : CustomEvent < IWidgetApiRequest > ) => {
102- resolve ( ) ;
103- this . ack ( ev ) ;
104- } ) ,
105- ) ;
100+ const waitForJoin = this . waitForAction ( ElementWidgetActions . JoinCall ) ;
106101 messaging . transport . send ( ElementWidgetActions . JoinCall , { } ) ;
107102 try {
108- await this . timeout ( waitForJoin ) ;
103+ await waitForJoin ;
109104 } catch ( e ) {
110105 // If it timed out, clean up our advance preparations
111106 this . activeChannel = null ;
@@ -128,60 +123,56 @@ export default class VoiceChannelStore extends EventEmitter {
128123 public disconnect = async ( ) => {
129124 this . assertConnected ( ) ;
130125
131- const waitForHangup = new Promise < void > ( resolve =>
132- this . activeChannel . once ( `action:${ ElementWidgetActions . HangupCall } ` , ( ) => resolve ( ) ) ,
133- ) ;
126+ const waitForHangup = this . waitForAction ( ElementWidgetActions . HangupCall ) ;
134127 this . activeChannel . transport . send ( ElementWidgetActions . HangupCall , { } ) ;
135- await this . timeout ( waitForHangup ) ;
128+ await waitForHangup ;
136129
137130 // onHangup cleans up for us
138131 } ;
139132
140133 public muteAudio = async ( ) => {
141134 this . assertConnected ( ) ;
142135
143- const waitForMute = new Promise < void > ( resolve =>
144- this . activeChannel . once ( `action:${ ElementWidgetActions . MuteAudio } ` , ( ) => resolve ( ) ) ,
145- ) ;
136+ const waitForMute = this . waitForAction ( ElementWidgetActions . MuteAudio ) ;
146137 this . activeChannel . transport . send ( ElementWidgetActions . MuteAudio , { } ) ;
147- await this . timeout ( waitForMute ) ;
138+ await waitForMute ;
148139 } ;
149140
150141 public unmuteAudio = async ( ) => {
151142 this . assertConnected ( ) ;
152143
153- const waitForUnmute = new Promise < void > ( resolve =>
154- this . activeChannel . once ( `action:${ ElementWidgetActions . UnmuteAudio } ` , ( ) => resolve ( ) ) ,
155- ) ;
144+ const waitForUnmute = this . waitForAction ( ElementWidgetActions . UnmuteAudio ) ;
156145 this . activeChannel . transport . send ( ElementWidgetActions . UnmuteAudio , { } ) ;
157- await this . timeout ( waitForUnmute ) ;
146+ await waitForUnmute ;
158147 } ;
159148
160149 public muteVideo = async ( ) => {
161150 this . assertConnected ( ) ;
162151
163- const waitForMute = new Promise < void > ( resolve =>
164- this . activeChannel . once ( `action:${ ElementWidgetActions . MuteVideo } ` , ( ) => resolve ( ) ) ,
165- ) ;
152+ const waitForMute = this . waitForAction ( ElementWidgetActions . MuteVideo ) ;
166153 this . activeChannel . transport . send ( ElementWidgetActions . MuteVideo , { } ) ;
167- await this . timeout ( waitForMute ) ;
154+ await waitForMute ;
168155 } ;
169156
170157 public unmuteVideo = async ( ) => {
171158 this . assertConnected ( ) ;
172159
173- const waitForUnmute = new Promise < void > ( resolve =>
174- this . activeChannel . once ( `action:${ ElementWidgetActions . UnmuteVideo } ` , ( ) => resolve ( ) ) ,
175- ) ;
160+ const waitForUnmute = this . waitForAction ( ElementWidgetActions . UnmuteVideo ) ;
176161 this . activeChannel . transport . send ( ElementWidgetActions . UnmuteVideo , { } ) ;
177- await this . timeout ( waitForUnmute ) ;
162+ await waitForUnmute ;
178163 } ;
179164
180165 private assertConnected = ( ) => {
181166 if ( ! this . activeChannel ) throw new Error ( "Not connected to any voice channel" ) ;
182167 } ;
183168
184- private timeout = async ( wait : Promise < void > ) => {
169+ private waitForAction = async ( action : ElementWidgetActions ) => {
170+ const wait = new Promise < void > ( resolve =>
171+ this . activeChannel . once ( `action:${ action } ` , ( ev : CustomEvent < IWidgetApiRequest > ) => {
172+ resolve ( ) ;
173+ this . ack ( ev ) ;
174+ } ) ,
175+ ) ;
185176 if ( await timeout ( wait , false , VoiceChannelStore . TIMEOUT ) === false ) {
186177 throw new Error ( "Communication with voice channel timed out" ) ;
187178 }
0 commit comments