Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create enum for ipcMain eventNames #2189

Merged
merged 7 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/auto-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { ipcMain } = require('electron')
const logger = require('../common/logger')
const { showDialog } = require('../dialogs')
const { IS_MAC, IS_WIN, IS_APPIMAGE } = require('../common/consts')
const ipcMainEvents = require('../utils/ipcMainEvents')

function isAutoUpdateSupported () {
// atm only macOS, windows and AppImage builds support autoupdate mechanism,
Expand Down Expand Up @@ -140,13 +141,13 @@ function setup (ctx) {
}

async function checkForUpdates () {
ipcMain.emit('updating')
ipcMain.emit(ipcMainEvents.UPDATING)
try {
await autoUpdater.checkForUpdates()
} catch (_) {
// Ignore. The errors are already handled on 'error' event.
}
ipcMain.emit('updatingEnded')
ipcMain.emit(ipcMainEvents.UPDATING_ENDED)
}

module.exports = async function (ctx) {
Expand Down
3 changes: 2 additions & 1 deletion src/automatic-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const logger = require('./common/logger')
const store = require('./common/store')
const { AUTO_GARBAGE_COLLECTOR: CONFIG_KEY } = require('./common/config-keys')
const { ipcMain } = require('electron')
const ipcMainEvents = require('./utils/ipcMainEvents')

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

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

module.exports = async function () {
Expand Down
3 changes: 2 additions & 1 deletion src/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const store = require('../common/store')
const logger = require('../common/logger')
const { STATUS } = require('./consts')
const createDaemon = require('./daemon')
const ipcMainEvents = require('../utils/ipcMainEvents')

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

const updateStatus = (stat, id = null) => {
status = stat
ipcMain.emit('ipfsd', status, id)
ipcMain.emit(ipcMainEvents.IPFSD, status, id)
}

const getIpfsd = async (optional = false) => {
Expand Down
3 changes: 2 additions & 1 deletion src/enable-namesys-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const logger = require('./common/logger')
const store = require('./common/store')
const { EXPERIMENT_PUBSUB_NAMESYS: CONFIG_KEY } = require('./common/config-keys')
const { ipcMain } = require('electron')
const ipcMainEvents = require('./utils/ipcMainEvents')

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

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

module.exports = async function () {
Expand Down
3 changes: 2 additions & 1 deletion src/enable-pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const logger = require('./common/logger')
const store = require('./common/store')
const { EXPERIMENT_PUBSUB: CONFIG_KEY } = require('./common/config-keys')
const { ipcMain } = require('electron')
const ipcMainEvents = require('./utils/ipcMainEvents')

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

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

module.exports = async function () {
Expand Down
3 changes: 2 additions & 1 deletion src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const i18n = require('i18next')
const ICU = require('i18next-icu')
const Backend = require('i18next-fs-backend')
const store = require('./common/store')
const ipcMainEvents = require('./utils/ipcMainEvents')

module.exports = async function () {
await i18n
Expand All @@ -30,6 +31,6 @@ module.exports = async function () {
store.set('language', lang)

await i18n.changeLanguage(lang)
ipcMain.emit('languageUpdated', lang)
ipcMain.emit(ipcMainEvents.LANG_UPDATED, lang)
})
}
5 changes: 3 additions & 2 deletions src/run-gc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ipcMain } = require('electron')
const logger = require('./common/logger')
const { showDialog, recoverableErrorDialog } = require('./dialogs')
const dock = require('./utils/dock')
const ipcMainEvents = require('./utils/ipcMainEvents')

module.exports = function runGarbageCollector ({ getIpfsd }) {
dock.run(async () => {
Expand Down Expand Up @@ -30,7 +31,7 @@ module.exports = function runGarbageCollector ({ getIpfsd }) {
return
}

ipcMain.emit('gcRunning')
ipcMain.emit(ipcMainEvents.GC_RUNNING)

try {
const errors = []
Expand Down Expand Up @@ -63,6 +64,6 @@ module.exports = function runGarbageCollector ({ getIpfsd }) {
})
}

ipcMain.emit('gcEnded')
ipcMain.emit(ipcMainEvents.GC_ENDED)
})
}
7 changes: 4 additions & 3 deletions src/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const logger = require('./common/logger')
const store = require('./common/store')
const moveRepositoryLocation = require('./move-repository-location')
const runGarbageCollector = require('./run-gc')
const ipcMainEvents = require('./utils/ipcMainEvents')
const { setCustomBinary, clearCustomBinary, hasCustomBinary } = require('./custom-ipfs-binary')
const { STATUS } = require('./daemon')
const { IS_MAC, IS_WIN, VERSION, GO_IPFS_VERSION } = require('./common/consts')
Expand All @@ -19,7 +20,7 @@ function buildCheckbox (key, label) {
return {
id: key,
label: i18n.t(label),
click: () => { ipcMain.emit(`toggle_${key}`) },
click: () => { ipcMainEvents.TOGGLE(key) },
type: 'checkbox',
checked: false
}
Expand Down Expand Up @@ -261,8 +262,8 @@ module.exports = function (ctx) {
tray.setContextMenu(menu)
tray.setToolTip('IPFS Desktop')

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

updateMenu()
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/create-toggler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { ipcMain } = require('electron')
const store = require('../common/store')
const logger = require('../common/logger')
const ipcMainEvents = require('./ipcMainEvents')

module.exports = function (settingsOption, activate) {
ipcMain.on(`toggle_${settingsOption}`, async () => {
ipcMain.on(ipcMainEvents.TOGGLE(settingsOption), async () => {
const oldValue = store.get(settingsOption, null)
const newValue = !oldValue

Expand All @@ -17,6 +18,6 @@ module.exports = function (settingsOption, activate) {
// We always emit the event so any handlers for it can act upon
// the current configuration, whether it was successfully
// updated or not.
ipcMain.emit('configUpdated')
ipcMain.emit(ipcMainEvents.CONFIG_UPDATED)
})
}
19 changes: 19 additions & 0 deletions src/utils/ipcMainEvents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { ipcMain } = require('electron')

const ipcMainEvents = Object.freeze({
CONFIG_UPDATED: 'configUpdated',
GC_ENDED: 'gcEnded',
GC_RUNNING: 'gcRunning',
IPFSD: 'ipfsd',
IPFS_CONFIG_CHANGED: 'ipfsConfigChanged',
LANG_UPDATED: 'languageUpdated',
MENUBAR_CLOSE: 'menubar-will-close',
MENUBAR_OPEN: 'menubar-will-open',
UPDATING: 'updating',
UPDATING_ENDED: 'updatingEnded',
TOGGLE: function (key) {
ipcMain.emit(`toggle_${key}`)
}
})

module.exports = ipcMainEvents