]> Repos - mime-chat/commitdiff
recording timer, bux fixes, simplified emitter
authorAndrew Gundersen <gundersena@xavier.edu>
Mon, 22 Nov 2021 21:59:33 +0000 (15:59 -0600)
committerAndrew Gundersen <gundersena@xavier.edu>
Mon, 22 Nov 2021 21:59:33 +0000 (15:59 -0600)
src/audio.js
src/render/components/context.js
src/session.js
src/utils/emitter.js
src/window.js

index b87940908b7b062f629e39cba4bf5206ce97467c..a10c1621564adb7cb5771e4dbe9e658b19c41d11 100644 (file)
@@ -12,6 +12,8 @@ let outputDevice;
 let setWriteId;
 let setStreamsId;
 
+let autoStopId; /* keep track of recording time */
+
 let playbackId; /* UID of the message being played */
 
 const streamState = { rec: false, pb: false };
@@ -24,7 +26,7 @@ backgroundMitt.on("data", (int16Arr) => {
 
 backgroundMitt.on("write", (int16Arr) => {
     clearTimeout(setWriteId);
-    setWriteId = setTimeout(() => setPlaybackStatus(false), 500);
+    setWriteId = setTimeout(setPlaybackStatus.prototype.bind(null, false), 500);
 });
 
 function setStreams() 
@@ -47,8 +49,21 @@ function setStreams()
     }
 }
 
+function startRecording()
+{
+    setRecordingStatus(true);
+
+    /* 15s recording time limit */
+    autoStopId = setTimeout(stopRecording, 15000);
+}
+
 function stopRecording()
 {
+    if (autoStopId)
+    {
+        clearTimeout(autoStopId);
+    }
+
     setRecordingStatus(false);
 
     sendMessage({
@@ -65,7 +80,7 @@ function initAudio()
     setStreamsId = setInterval(setStreams, 2000);
 
     const res = globalShortcut.register('CommandOrControl+Return', () => {
-        streamState.rec ? stopRecording() : setRecordingStatus(true);
+        streamState.rec ? stopRecording() : startRecording();
     });
 
     if (!res) throw new Error("Failed to register recording shortcut");
@@ -103,6 +118,8 @@ function setRecordingStatus(status)
 
 function setPlaybackStatus(status)
 {
+    console.log("Setting playback status: ", status);
+
     streamState.pb = status;
     ipcEmit("playback", playbackId, status);
     updateTray("playback", status);
index 859fe4db3c5d1ff6ca442bdbb04412c800306fb3..fbfb2c702379e3ac8a963f1a55812cb2356ec61b 100644 (file)
@@ -9,6 +9,7 @@ const Context =
         Vue.onMounted(() => {
             window.mainApi.on("playback", (id, status) => {
                 if (id === props.id) {
+                    console.log(id, status);
                     playback.value = status;
                 }
             });
index b131d30432284effffe4e0a713888441ce06b4ca..9fcbf114ac4b7dfc41e6301bbe3917753f015b46 100644 (file)
@@ -52,9 +52,9 @@ function handleMessage()
                 new Notification({title: message.context, body: message.content[message.content.length - 1]}).show();
             }
 
-            if (message.audio) playback(message.audio, message.id);
-
             ipcEmit("ui-update", message);
+
+            if (message.audio) playback(message.audio, message.id);
         }
     }
 }
index a621d24e3ba6c8e193307e673447bf41b496c0b9..d3d533bff87a40d7eb0e0b52d67b108d53fc4a8d 100644 (file)
@@ -1,14 +1,10 @@
-const EventEmitter = require('events');
+const EventEmitter = require("events");
 
-class BackgroundMitt extends EventEmitter { }
-const backgroundMitt = new BackgroundMitt();
+const backgroundMitt = new EventEmitter();
 
-const ipcEmit = (channel, payload) => 
+const ipcEmit = (channel, ...args) => 
 {
-    backgroundMitt.emit('ipc-renderer', {
-        channel,
-        payload
-    });
+    backgroundMitt.emit("ipc-renderer", channel, ...args);
 };
 
 module.exports.backgroundMitt = backgroundMitt;
index ce42df82d6dbf73ac227fb3ad51ad423470cac93..0f9887dc1bb2ad4e046bd8ba1afbf2a88dd038cf 100644 (file)
@@ -52,7 +52,6 @@ function createWin()
 
     .on("closed", () => {
         store.set("focus", false);
-        backgroundMitt.removeAllListeners("ipc-renderer");
         win = null;
     })
 
@@ -63,9 +62,10 @@ function createWin()
 
     win.webContents.on("did-finish-load", () => {
 
-        /* Gateway to the frontend */
-        backgroundMitt.on("ipc-renderer", (e) => {
-            win.webContents.send(e.channel, e.payload);    
+        backgroundMitt.removeAllListeners("ipc-renderer");
+
+        backgroundMitt.on("ipc-renderer", (channel, ...args) => {
+            win.webContents.send(channel, ...args);    
         });
 
         win.webContents.send("set-account", store.get("account"));