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

This commit is contained in:
Esteban Abaroa
2023-12-20 15:56:30 +00:00
parent 84e836a6dd
commit 795d39098a
2 changed files with 37 additions and 17 deletions

View File

@@ -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);
}
});

View File

@@ -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);