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 _stream;
let _mediaRecorder; let _mediaRecorder;
const audioChunks = [];
async function init() { async function init() {
if (_mediaRecorder) return; if (_mediaRecorder) return;
@ -23,18 +22,11 @@ async function init() {
timer = new Timer(); timer = new Timer();
_stream = null; _stream = null;
_mediaRecorder = null; _mediaRecorder = null;
audioChunks.length = 0;
_stream = await navigator.mediaDevices.getUserMedia({ audio: true }); _stream = await navigator.mediaDevices.getUserMedia({ audio: true });
_mediaRecorder = new MediaRecorder(_stream); _mediaRecorder = new MediaRecorder(_stream);
// Remove previous recording
audioChunks.length = 0;
_mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
_mediaRecorder.onerror = (error) => { _mediaRecorder.onerror = (error) => {
console.log(error); console.log(error);
_mediaRecorder.stop(); _mediaRecorder.stop();
@ -54,16 +46,12 @@ function startOrResumeRec() {
if (_mediaRecorder.state === 'paused') { if (_mediaRecorder.state === 'paused') {
_mediaRecorder.resume(); _mediaRecorder.resume();
} else if (_mediaRecorder.state === 'inactive') { } else if (_mediaRecorder.state === 'inactive') {
audioChunks.length = 0;
_mediaRecorder.start(); _mediaRecorder.start();
} }
timer.resume(); timer.resume();
} }
async function restartRec() { 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 // Needed, otherwise the browser indicator would remain after closing UI
if (_mediaRecorder.state !== 'inactive') _mediaRecorder.stop(); if (_mediaRecorder.state !== 'inactive') _mediaRecorder.stop();
_stream.getTracks().forEach((track) => track.stop()); _stream.getTracks().forEach((track) => track.stop());
@ -95,13 +83,11 @@ function VoiceMailRecorder({ fnHowToSubmit }) {
_mediaRecorder.onresume = () => setState('Recording...'); _mediaRecorder.onresume = () => setState('Recording...');
} }
function stopAndSubmit(skipSubmission) { function stopAndSubmit(skipSubmission = false) {
if (_mediaRecorder && _mediaRecorder.state !== 'inactive') _mediaRecorder.stop();
_stream.getTracks().forEach((track) => track.stop());
if (!skipSubmission) { if (!skipSubmission) {
setTimeout(() => { _mediaRecorder.ondataavailable = (event) => {
const audioChunks = [];
audioChunks.push(event.data);
_mediaRecorder = null; _mediaRecorder = null;
_stream = null; _stream = null;
@ -110,8 +96,12 @@ function VoiceMailRecorder({ fnHowToSubmit }) {
const audioFile = new File([audioBlob], 'voicemail.webm', opts); const audioFile = new File([audioBlob], 'voicemail.webm', opts);
fnHowToSubmit(audioFile); 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(() => { useEffect(() => {

View file

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

View file

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