Improve code quality

This commit is contained in:
C0ffeeCode 2022-01-06 15:58:15 +01:00
parent 8cd2372f53
commit c9d2c33677
3 changed files with 11 additions and 22 deletions

View file

@ -15,7 +15,6 @@ let timer;
let _stream;
let _mediaRecorder;
const audioChunks = [];
async function init() {
if (_mediaRecorder) return;
@ -23,18 +22,11 @@ async function init() {
timer = new Timer();
_stream = null;
_mediaRecorder = null;
audioChunks.length = 0;
_stream = await navigator.mediaDevices.getUserMedia({ audio: true });
_mediaRecorder = new MediaRecorder(_stream);
// Remove previous recording
audioChunks.length = 0;
_mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
_mediaRecorder.onerror = (error) => {
console.log(error);
_mediaRecorder.stop();
@ -54,16 +46,12 @@ function startOrResumeRec() {
if (_mediaRecorder.state === 'paused') {
_mediaRecorder.resume();
} else if (_mediaRecorder.state === 'inactive') {
audioChunks.length = 0;
_mediaRecorder.start();
}
timer.resume();
}
async function restartRec() {
// TODO: BUG: If recording is paused before restarting
// it will cause the "Recording paused" not to be edited
// Needed, otherwise the browser indicator would remain after closing UI
if (_mediaRecorder.state !== 'inactive') _mediaRecorder.stop();
_stream.getTracks().forEach((track) => track.stop());
@ -95,13 +83,11 @@ function VoiceMailRecorder({ fnHowToSubmit }) {
_mediaRecorder.onresume = () => setState('Recording...');
}
function stopAndSubmit(skipSubmission) {
if (_mediaRecorder && _mediaRecorder.state !== 'inactive') _mediaRecorder.stop();
_stream.getTracks().forEach((track) => track.stop());
function stopAndSubmit(skipSubmission = false) {
if (!skipSubmission) {
setTimeout(() => {
_mediaRecorder.ondataavailable = (event) => {
const audioChunks = [];
audioChunks.push(event.data);
_mediaRecorder = null;
_stream = null;
@ -110,8 +96,12 @@ function VoiceMailRecorder({ fnHowToSubmit }) {
const audioFile = new File([audioBlob], 'voicemail.webm', opts);
fnHowToSubmit(audioFile);
}, 300); // TODO: Fix this hack, stop does not mean dataavailable but its going to happen soon
};
}
// Stop recording, remove browser indicator
if (_mediaRecorder && _mediaRecorder.state !== 'inactive') _mediaRecorder.stop();
_stream.getTracks().forEach((track) => track.stop());
}
useEffect(() => {

View file

@ -5,7 +5,6 @@
div {
width: 150px;
max-width: 100%;
// max-width: ;
}
}

View file

@ -297,14 +297,14 @@ function RoomViewInput({
const handleAttachmentTypeSelectorReturn = (ret) => {
switch (ret) {
case attachmentUiFrameTypes.none:
setAttachmentOrUi(attachmentUiFrameTypes.none);
setAttachmentOrUi(ret);
roomsInput.cancelAttachment(roomId);
break;
case attachmentUiFrameTypes.file:
uploadInputRef.current.click();
break;
case attachmentUiFrameTypes.voiceMailRecorder:
setAttachmentOrUi(attachmentUiFrameTypes.voiceMailRecorder);
setAttachmentOrUi(ret);
break;
default:
console.log('unhandled attachment type selector return');