From 07451eadf2a04060b2719f8d2fcc748d37579134 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Sun, 2 Oct 2022 18:48:28 +0200 Subject: [PATCH] fixed unlimited creation of `openFile` listeners --- contextAPI.js | 13 +++++++++++-- src/ui/src/components/Ation.js | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/contextAPI.js b/contextAPI.js index f371b6e..3d0ceeb 100644 --- a/contextAPI.js +++ b/contextAPI.js @@ -2,8 +2,17 @@ const {contextBridge, ipcRenderer} = require("electron"); +let fileOpenListener = null; + contextBridge.exposeInMainWorld("api", { openFileDialog : () => ipcRenderer.send("WindowManager::openFileDialog"), - onFileOpen : callback => ipcRenderer.on("Ation::openFile", (_, presentation) => callback(presentation)), - openFile : filePath => ipcRenderer.send("WindowManager::openFile", filePath) + onFileOpen : callback => { + if (fileOpenListener) + ipcRenderer.off("Ation::openFile", fileOpenListener); + fileOpenListener = (_, presentation) => callback(presentation); + ipcRenderer.on("Ation::openFile", fileOpenListener); + }, + openFile : filePath => ipcRenderer.send("WindowManager::openFile", filePath), + + removeOnFileOpenListener : callback => ipcRenderer.off("Ation::openFile", callback) }); \ No newline at end of file diff --git a/src/ui/src/components/Ation.js b/src/ui/src/components/Ation.js index c9ee4fd..1979e61 100644 --- a/src/ui/src/components/Ation.js +++ b/src/ui/src/components/Ation.js @@ -23,12 +23,12 @@ const Ation = () => { const [newBasePath, slideDeck] = presentation; if (!slideDeck) return; - if (newBasePath !== basePath) + if (slide >= slideDeck.length) setSlide(0); setBasePath(newBasePath); setDeck(slideDeck); }); - }, [basePath]); + }, [basePath, slide]); const openFile = () => { window.api.openFileDialog();