Skip to content

Commit 57525dd

Browse files
noliveira95SgtPookihacdias
authored
chore: create enum for ipcMain eventNames (#2189)
Co-authored-by: Russell Dempsey <[email protected]> Co-authored-by: Henrique Dias <[email protected]>
1 parent cc70bc5 commit 57525dd

File tree

10 files changed

+38
-14
lines changed

10 files changed

+38
-14
lines changed

src/auto-updater/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { ipcMain } = require('electron')
55
const logger = require('../common/logger')
66
const { showDialog } = require('../dialogs')
77
const { IS_MAC, IS_WIN, IS_APPIMAGE } = require('../common/consts')
8+
const ipcMainEvents = require('../common/ipc-main-events')
89

910
function isAutoUpdateSupported () {
1011
// atm only macOS, windows and AppImage builds support autoupdate mechanism,
@@ -140,13 +141,13 @@ function setup (ctx) {
140141
}
141142

142143
async function checkForUpdates () {
143-
ipcMain.emit('updating')
144+
ipcMain.emit(ipcMainEvents.UPDATING)
144145
try {
145146
await autoUpdater.checkForUpdates()
146147
} catch (_) {
147148
// Ignore. The errors are already handled on 'error' event.
148149
}
149-
ipcMain.emit('updatingEnded')
150+
ipcMain.emit(ipcMainEvents.UPDATING_ENDED)
150151
}
151152

152153
module.exports = async function (ctx) {

src/automatic-gc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const logger = require('./common/logger')
33
const store = require('./common/store')
44
const { AUTO_GARBAGE_COLLECTOR: CONFIG_KEY } = require('./common/config-keys')
55
const { ipcMain } = require('electron')
6+
const ipcMainEvents = require('./common/ipc-main-events')
67

78
const gcFlag = '--enable-gc'
89
const isEnabled = flags => flags.some(f => f === gcFlag)
@@ -25,7 +26,7 @@ function disable () {
2526

2627
function applyConfig (newFlags) {
2728
store.set('ipfsConfig.flags', newFlags)
28-
ipcMain.emit('ipfsConfigChanged') // trigger node restart
29+
ipcMain.emit(ipcMainEvents.IPFS_CONFIG_CHANGED) // trigger node restart
2930
}
3031

3132
module.exports = async function () {

src/common/ipc-main-events.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const ipcMainEvents = Object.freeze({
2+
CONFIG_UPDATED: 'configUpdated',
3+
GC_ENDED: 'gcEnded',
4+
GC_RUNNING: 'gcRunning',
5+
IPFSD: 'ipfsd',
6+
IPFS_CONFIG_CHANGED: 'ipfsConfigChanged',
7+
LANG_UPDATED: 'languageUpdated',
8+
MENUBAR_CLOSE: 'menubar-will-close',
9+
MENUBAR_OPEN: 'menubar-will-open',
10+
UPDATING: 'updating',
11+
UPDATING_ENDED: 'updatingEnded',
12+
TOGGLE: (key) => `toggle_${key}`
13+
})
14+
15+
module.exports = ipcMainEvents

src/daemon/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const store = require('../common/store')
66
const logger = require('../common/logger')
77
const { STATUS } = require('./consts')
88
const createDaemon = require('./daemon')
9+
const ipcMainEvents = require('../common/ipc-main-events')
910

1011
module.exports = async function (ctx) {
1112
let ipfsd = null
@@ -14,7 +15,7 @@ module.exports = async function (ctx) {
1415

1516
const updateStatus = (stat, id = null) => {
1617
status = stat
17-
ipcMain.emit('ipfsd', status, id)
18+
ipcMain.emit(ipcMainEvents.IPFSD, status, id)
1819
}
1920

2021
const getIpfsd = async (optional = false) => {

src/enable-namesys-pubsub.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const logger = require('./common/logger')
33
const store = require('./common/store')
44
const { EXPERIMENT_PUBSUB_NAMESYS: CONFIG_KEY } = require('./common/config-keys')
55
const { ipcMain } = require('electron')
6+
const ipcMainEvents = require('./common/ipc-main-events')
67

78
const namesysPubsubFlag = '--enable-namesys-pubsub'
89
const isEnabled = flags => flags.some(f => f === namesysPubsubFlag)
@@ -25,7 +26,7 @@ function disable () {
2526

2627
function applyConfig (newFlags) {
2728
store.set('ipfsConfig.flags', newFlags)
28-
ipcMain.emit('ipfsConfigChanged') // trigger node restart
29+
ipcMain.emit(ipcMainEvents.IPFS_CONFIG_CHANGED) // trigger node restart
2930
}
3031

3132
module.exports = async function () {

src/enable-pubsub.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const logger = require('./common/logger')
33
const store = require('./common/store')
44
const { EXPERIMENT_PUBSUB: CONFIG_KEY } = require('./common/config-keys')
55
const { ipcMain } = require('electron')
6+
const ipcMainEvents = require('./common/ipc-main-events')
67

78
const pubsubFlag = '--enable-pubsub-experiment'
89
const isEnabled = flags => flags.some(f => f === pubsubFlag)
@@ -25,7 +26,7 @@ function disable () {
2526

2627
function applyConfig (newFlags) {
2728
store.set('ipfsConfig.flags', newFlags)
28-
ipcMain.emit('ipfsConfigChanged') // trigger node restart
29+
ipcMain.emit(ipcMainEvents.IPFS_CONFIG_CHANGED) // trigger node restart
2930
}
3031

3132
module.exports = async function () {

src/i18n.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const i18n = require('i18next')
44
const ICU = require('i18next-icu')
55
const Backend = require('i18next-fs-backend')
66
const store = require('./common/store')
7+
const ipcMainEvents = require('./common/ipc-main-events')
78

89
module.exports = async function () {
910
await i18n
@@ -30,6 +31,6 @@ module.exports = async function () {
3031
store.set('language', lang)
3132

3233
await i18n.changeLanguage(lang)
33-
ipcMain.emit('languageUpdated', lang)
34+
ipcMain.emit(ipcMainEvents.LANG_UPDATED, lang)
3435
})
3536
}

src/run-gc.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { ipcMain } = require('electron')
33
const logger = require('./common/logger')
44
const { showDialog, recoverableErrorDialog } = require('./dialogs')
55
const dock = require('./utils/dock')
6+
const ipcMainEvents = require('./common/ipc-main-events')
67

78
module.exports = function runGarbageCollector ({ getIpfsd }) {
89
dock.run(async () => {
@@ -30,7 +31,7 @@ module.exports = function runGarbageCollector ({ getIpfsd }) {
3031
return
3132
}
3233

33-
ipcMain.emit('gcRunning')
34+
ipcMain.emit(ipcMainEvents.GC_RUNNING)
3435

3536
try {
3637
const errors = []
@@ -63,6 +64,6 @@ module.exports = function runGarbageCollector ({ getIpfsd }) {
6364
})
6465
}
6566

66-
ipcMain.emit('gcEnded')
67+
ipcMain.emit(ipcMainEvents.GC_ENDED)
6768
})
6869
}

src/tray.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const logger = require('./common/logger')
66
const store = require('./common/store')
77
const moveRepositoryLocation = require('./move-repository-location')
88
const runGarbageCollector = require('./run-gc')
9+
const ipcMainEvents = require('./common/ipc-main-events')
910
const { setCustomBinary, clearCustomBinary, hasCustomBinary } = require('./custom-ipfs-binary')
1011
const { STATUS } = require('./daemon')
1112
const { IS_MAC, IS_WIN, VERSION, GO_IPFS_VERSION } = require('./common/consts')
@@ -19,7 +20,7 @@ function buildCheckbox (key, label) {
1920
return {
2021
id: key,
2122
label: i18n.t(label),
22-
click: () => { ipcMain.emit(`toggle_${key}`) },
23+
click: () => { ipcMain.emit(ipcMainEvents.TOGGLE(key)) },
2324
type: 'checkbox',
2425
checked: false
2526
}
@@ -261,8 +262,8 @@ module.exports = function (ctx) {
261262
tray.setContextMenu(menu)
262263
tray.setToolTip('IPFS Desktop')
263264

264-
menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') })
265-
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })
265+
menu.on('menu-will-show', () => { ipcMain.emit(ipcMainEvents.MENUBAR_OPEN) })
266+
menu.on('menu-will-close', () => { ipcMain.emit(ipcMainEvents.MENUBAR_CLOSE) })
266267

267268
updateMenu()
268269
}

src/utils/create-toggler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { ipcMain } = require('electron')
22
const store = require('../common/store')
33
const logger = require('../common/logger')
4+
const ipcMainEvents = require('../common/ipc-main-events')
45

56
module.exports = function (settingsOption, activate) {
6-
ipcMain.on(`toggle_${settingsOption}`, async () => {
7+
ipcMain.on(ipcMainEvents.TOGGLE(settingsOption), async () => {
78
const oldValue = store.get(settingsOption, null)
89
const newValue = !oldValue
910

@@ -17,6 +18,6 @@ module.exports = function (settingsOption, activate) {
1718
// We always emit the event so any handlers for it can act upon
1819
// the current configuration, whether it was successfully
1920
// updated or not.
20-
ipcMain.emit('configUpdated')
21+
ipcMain.emit(ipcMainEvents.CONFIG_UPDATED)
2122
})
2223
}

0 commit comments

Comments
 (0)