From: Andrew Gundersen Date: Mon, 22 Nov 2021 21:59:33 +0000 (-0600) Subject: recording timer, bux fixes, simplified emitter X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=7de055585674fc643a69a663d5c5644c6f0d3f95;p=mime-chat recording timer, bux fixes, simplified emitter --- diff --git a/src/audio.js b/src/audio.js index b879409..a10c162 100644 --- a/src/audio.js +++ b/src/audio.js @@ -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); diff --git a/src/render/components/context.js b/src/render/components/context.js index 859fe4d..fbfb2c7 100644 --- a/src/render/components/context.js +++ b/src/render/components/context.js @@ -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; } }); diff --git a/src/session.js b/src/session.js index b131d30..9fcbf11 100644 --- a/src/session.js +++ b/src/session.js @@ -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); } } } diff --git a/src/utils/emitter.js b/src/utils/emitter.js index a621d24..d3d533b 100644 --- a/src/utils/emitter.js +++ b/src/utils/emitter.js @@ -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; diff --git a/src/window.js b/src/window.js index ce42df8..0f9887d 100644 --- a/src/window.js +++ b/src/window.js @@ -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"));