-
Notifications
You must be signed in to change notification settings - Fork 865
/
automatic-gc.js
54 lines (45 loc) · 1.32 KB
/
automatic-gc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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