fix(electron): don't spam user with ipfs errors

This commit is contained in:
Esteban Abaroa
2023-12-20 16:05:50 +00:00
parent be2121272c
commit bc83a75b04
2 changed files with 37 additions and 17 deletions
+8 -16
View File
@@ -5,24 +5,16 @@ const path = require('path');
const startIpfs = require('./start-ipfs'); const startIpfs = require('./start-ipfs');
const startPlebbitRpcServer = require('./start-plebbit-rpc'); const startPlebbitRpcServer = require('./start-plebbit-rpc');
const { URL } = require('node:url'); 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; let startIpfsError;
setInterval(async () => { startIpfs.onError = (error) => {
try { // only show error once or it spams the user
const started = await tcpPortUsed.check(5001, '127.0.0.1'); const alreadyShownIpfsError = !!startIpfsError;
if (started) { startIpfsError = error;
return; if (!alreadyShownIpfsError) {
} dialog.showErrorBox('IPFS warning', error.message);
await startIpfs();
} catch (e) {
console.log(e);
startIpfsError = e;
dialog.showErrorBox('IPFS error', startIpfsError.message);
} }
}, 10000); };
// use common user agent instead of electron so img, video, audio, iframe elements don't get blocked // 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 // https://www.whatismybrowser.com/guides/the-latest-version/chrome
@@ -138,7 +130,7 @@ const createMainWindow = () => {
} }
if (startIpfsError) { if (startIpfsError) {
dialog.showErrorBox('IPFS error', startIpfsError.message); dialog.showErrorBox('IPFS warning', startIpfsError.message);
} }
}); });
+29 -1
View File
@@ -5,6 +5,7 @@ const fs = require('fs-extra');
const envPaths = require('env-paths').default('plebbit', { suffix: false }); const envPaths = require('env-paths').default('plebbit', { suffix: false });
const ps = require('node:process'); const ps = require('node:process');
const proxyServer = require('./proxy-server'); const proxyServer = require('./proxy-server');
const tcpPortUsed = require('tcp-port-used');
// use this custom function instead of spawnSync for better logging // use this custom function instead of spawnSync for better logging
// also spawnSync might have been causing crash on start on windows // 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);