Skip to content

Commit

Permalink
Cancel audio recording on room switch (#22880)
Browse files Browse the repository at this point in the history
 * Cancel audio recordings on message bar destroy event.

Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com>
  • Loading branch information
matheusbsilva137 and gabriellsh authored Aug 12, 2021
1 parent b2024dd commit 353dcd6
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions app/ui-message/client/messageBox/messageBoxAudioMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ const stopRecording = () => new Promise((resolve) => AudioRecorder.stop(resolve)
const recordingInterval = new ReactiveVar(null);
const recordingRoomId = new ReactiveVar(null);

const cancelRecording = (instance) => new Promise(async () => {
if (recordingInterval.get()) {
clearInterval(recordingInterval.get());
recordingInterval.set(null);
recordingRoomId.set(null);
}

instance.time.set('00:00');

await stopRecording();

instance.state.set(null);
});

Template.messageBoxAudioMessage.onCreated(async function() {
this.state = new ReactiveVar(null);
this.time = new ReactiveVar('00:00');
Expand Down Expand Up @@ -47,6 +61,12 @@ Template.messageBoxAudioMessage.onCreated(async function() {
}
});

Template.messageBoxAudioMessage.onDestroyed(async function() {
if (this.state.get() === 'recording') {
await cancelRecording(this);
}
});

Template.messageBoxAudioMessage.helpers({
isAllowed() {
return AudioRecorder.isSupported()
Expand Down Expand Up @@ -105,17 +125,7 @@ Template.messageBoxAudioMessage.events({
async 'click .js-audio-message-cancel'(event, instance) {
event.preventDefault();

if (recordingInterval.get()) {
clearInterval(recordingInterval.get());
recordingInterval.set(null);
recordingRoomId.set(null);
}

instance.time.set('00:00');

await stopRecording();

instance.state.set(null);
await cancelRecording(instance);
},

async 'click .js-audio-message-done'(event, instance) {
Expand Down

0 comments on commit 353dcd6

Please sign in to comment.