-
Notifications
You must be signed in to change notification settings - Fork 865
/
enable-namesys-pubsub.js
54 lines (45 loc) · 1.4 KB
/
enable-namesys-pubsub.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 = 'experiments.pubsubNamesys'
const namesysPubsubFlag = '--enable-namesys-pubsub'
const isEnabled = flags => flags.some(f => f === namesysPubsubFlag)
function enable () {
const flags = store.get('ipfsConfig.flags', [])
if (!isEnabled(flags)) {
flags.push(namesysPubsubFlag)
applyConfig(flags)
}
}
function disable () {
let flags = store.get('ipfsConfig.flags', [])
if (isEnabled(flags)) {
flags = flags.filter(item => item !== namesysPubsubFlag) // 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(`[ipns over pubsub] ${err.toString()}`)
return false
}
}
activate({ newValue: store.get(CONFIG_KEY, false) })
createToggler(CONFIG_KEY, activate)
logger.info(`[ipns over pubsub] ${store.get(CONFIG_KEY, false) ? 'enabled' : 'disabled'}`)
}
module.exports.CONFIG_KEY = CONFIG_KEY