Skip to content

Commit

Permalink
feat: disable/enable gc via settings menu
Browse files Browse the repository at this point in the history
Part of ipfs#1647

Very similar approach to ipfs#1735
  • Loading branch information
andrew committed Jan 20, 2021
1 parent 6dbfa13 commit e9a10dc
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"launchOnStartup": "Launch at Login",
"openWebUIAtLaunch": "Open Web UI at Launch",
"pubsub": "Enable PubSub",
"automaticGc": "Automatic Garbage Collection",
"ipfsCommandLineTools": "Command Line Tools",
"takeScreenshotShortcut": "Global Screenshot Shortcut",
"downloadHashShortcut": "Global Download Shortcut",
Expand Down
54 changes: 54 additions & 0 deletions src/automatic-gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const createToggler = require('./utils/create-toggler')
const logger = require('./common/logger')
const store = require('./common/store')
const { ipcMain } = require('electron')

const CONFIG_KEY = 'automaticGc'
const gcFlag = '--enable-gc'
const isEnabled = flags => flags.some(f => f === gcFlag)

function enable () {
const flags = store.get('ipfsConfig.flags', [])
if (!isEnabled(flags)) {
flags.push(gcFlag)
applyConfig(flags)
}
}

function disable () {
let flags = store.get('ipfsConfig.flags', [])
if (isEnabled(flags)) {
flags = flags.filter(item => item !== gcFlag) // remove flag
applyConfig(flags)
}
}

function applyConfig (newFlags) {
store.set('ipfsConfig.flags', newFlags)
ipcMain.emit('ipfsConfigChanged') // trigger node restart
}

module.exports = async function () {
const activate = ({ newValue, oldValue }) => {
if (newValue === oldValue) return

try {
if (newValue === true) {
enable()
} else {
disable()
}

return true
} catch (err) {
logger.error(`[automatic gc] ${err.toString()}`)

return false
}
}
activate({ newValue: store.get(CONFIG_KEY, true) })
createToggler(CONFIG_KEY, activate)
logger.info(`[automatic gc] ${store.get(CONFIG_KEY, true) ? 'enabled' : 'disabled'}`)
}

module.exports.CONFIG_KEY = CONFIG_KEY
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const setupNpmOnIpfs = require('./npm-on-ipfs')
const setupDaemon = require('./daemon')
const setupWebUI = require('./webui')
const setupAutoLaunch = require('./auto-launch')
const setupAutoGc = require('./automatic-gc')
const setupPubsub = require('./enable-pubsub')
const setupDownloadCid = require('./download-cid')
const setupTakeScreenshot = require('./take-screenshot')
Expand Down Expand Up @@ -75,6 +76,7 @@ async function run () {
await Promise.all([
setupArgvFilesHandler(ctx),
setupAutoLaunch(ctx),
setupAutoGc(ctx),
setupPubsub(ctx),
setupSecondInstance(ctx),
// Setup global shortcuts
Expand Down
3 changes: 3 additions & 0 deletions src/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const { CONFIG_KEY: SCREENSHOT_KEY, SHORTCUT: SCREENSHOT_SHORTCUT, takeScreensho
const { CONFIG_KEY: DOWNLOAD_KEY, SHORTCUT: DOWNLOAD_SHORTCUT, downloadCid } = require('./download-cid')
const { CONFIG_KEY: AUTO_LAUNCH_KEY, isSupported: supportsLaunchAtLogin } = require('./auto-launch')
const { CONFIG_KEY: PUBSUB_KEY } = require('./enable-pubsub')
const { CONFIG_KEY: AUTO_GC_KEY } = require('./automatic-gc')
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
const { CONFIG_KEY: AUTO_LAUNCH_WEBUI_KEY } = require('./webui')

const CONFIG_KEYS = [
AUTO_LAUNCH_KEY,
AUTO_LAUNCH_WEBUI_KEY,
AUTO_GC_KEY,
IPFS_PATH_KEY,
NPM_IPFS_KEY,
SCREENSHOT_KEY,
Expand Down Expand Up @@ -124,6 +126,7 @@ function buildMenu (ctx) {
},
buildCheckbox(AUTO_LAUNCH_KEY, 'settings.launchOnStartup'),
buildCheckbox(AUTO_LAUNCH_WEBUI_KEY, 'settings.openWebUIAtLaunch'),
buildCheckbox(AUTO_GC_KEY, 'settings.automaticGc'),
buildCheckbox(IPFS_PATH_KEY, 'settings.ipfsCommandLineTools'),
buildCheckbox(SCREENSHOT_KEY, 'settings.takeScreenshotShortcut'),
buildCheckbox(DOWNLOAD_KEY, 'settings.downloadHashShortcut'),
Expand Down

0 comments on commit e9a10dc

Please sign in to comment.