diff --git a/electron/main.js b/electron/main.js index 84896d4e..14832ca5 100644 --- a/electron/main.js +++ b/electron/main.js @@ -5,24 +5,16 @@ const path = require('path'); const startIpfs = require('./start-ipfs'); const startPlebbitRpcServer = require('./start-plebbit-rpc'); const { URL } = require('node:url'); -const tcpPortUsed = require('tcp-port-used'); -// retry starting ipfs every 10 second, -// in case it was started by another client that shut down and shut down ipfs with it let startIpfsError; -setInterval(async () => { - try { - const started = await tcpPortUsed.check(5001, '127.0.0.1'); - if (started) { - return; - } - await startIpfs(); - } catch (e) { - console.log(e); - startIpfsError = e; - dialog.showErrorBox('IPFS error', startIpfsError.message); +startIpfs.onError = (error) => { + // only show error once or it spams the user + const alreadyShownIpfsError = !!startIpfsError; + startIpfsError = error; + if (!alreadyShownIpfsError) { + dialog.showErrorBox('IPFS warning', error.message); } -}, 10000); +}; // use common user agent instead of electron so img, video, audio, iframe elements don't get blocked // https://www.whatismybrowser.com/guides/the-latest-version/chrome @@ -138,7 +130,7 @@ const createMainWindow = () => { } if (startIpfsError) { - dialog.showErrorBox('IPFS error', startIpfsError.message); + dialog.showErrorBox('IPFS warning', startIpfsError.message); } }); diff --git a/electron/start-ipfs.js b/electron/start-ipfs.js index 5405ccf9..97864f35 100644 --- a/electron/start-ipfs.js +++ b/electron/start-ipfs.js @@ -5,6 +5,7 @@ const fs = require('fs-extra'); const envPaths = require('env-paths').default('plebbit', { suffix: false }); const ps = require('node:process'); const proxyServer = require('./proxy-server'); +const tcpPortUsed = require('tcp-port-used'); // use this custom function instead of spawnSync for better logging // also spawnSync might have been causing crash on start on windows @@ -98,4 +99,31 @@ const startIpfs = async () => { }); }; -module.exports = startIpfs; +let pendingStart = false; +const start = async () => { + if (pendingStart) { + return; + } + pendingStart = true; + try { + const started = await tcpPortUsed.check(5001, '127.0.0.1'); + if (started) { + return; + } + await startIpfs(); + } catch (e) { + console.log('failed starting ipfs', e); + try { + // try to run exported onError callback, can be undefined + module.exports.onError(e)?.catch?.(console.log); + } catch (e) {} + } + pendingStart = false; +}; + +// retry starting ipfs every 1 second, +// in case it was started by another client that shut down and shut down ipfs with it +start(); +setInterval(() => { + start(); +}, 1000);