Skip to content

Commit 9870675

Browse files
committed
feat: Show discovered peer count
1 parent bc1f03d commit 9870675

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/tray.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,19 @@ module.exports = function (ctx) {
277277
})
278278
}
279279

280-
const pollPeers = () => {
280+
const fetchPeers = () => {
281281
// If the daemon is running, send a request to retrieve the number
282-
// of connected peers. Emit 'peersPolled' event upon retrieval.
282+
// of connected peers. Emit 'peerCountFetched' event upon retrieval.
283283
if (state.status === STATUS.STARTING_FINISHED && ctx.getIpfsd) {
284284
ctx.getIpfsd().then((daemon) => {
285285
daemon.api.swarm.peers().then((value) => {
286286
if (value.length) {
287-
ipcMain.emit('peersPolled', value.length)
287+
ipcMain.emit('peerCountFetched', value.length)
288288
}
289289
})
290290
})
291291
} else {
292-
ipcMain.emit('peersPolled', 0)
292+
ipcMain.emit('peerCountFetched', 0)
293293
}
294294
}
295295

@@ -362,6 +362,12 @@ module.exports = function (ctx) {
362362
}
363363
}
364364

365+
ipcMain.on('menubar-will-open', () => {
366+
fetchPeers()
367+
setupMenu()
368+
updateMenu()
369+
})
370+
365371
ipcMain.on('ipfsd', status => {
366372
state.status = status
367373
updateMenu()
@@ -387,26 +393,21 @@ module.exports = function (ctx) {
387393
updateMenu()
388394
})
389395

390-
ipcMain.on('peersPolled', peerCount => {
396+
ipcMain.on('peerCountFetched', peerCount => {
391397
// When a new peer count is retrieved, rebuild the menu and update
392398
// the tray tooltip with the new number if necessary.
393399
if (peerCount !== state.peerCount) {
394400
state.peerCount = peerCount
395-
menu = buildMenu(ctx, state.peerCount)
396-
menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') })
397-
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })
398-
tray.setContextMenu(menu)
399401
tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount'))
400-
updateMenu()
401402
}
402403
})
403404

404405
ipcMain.on('configUpdated', () => { updateMenu() })
405406
ipcMain.on('languageUpdated', () => { setupMenu() })
406407

407408
setupMenu()
408-
setInterval(pollPeers, 60000)
409409

410+
tray.on('mouse-move', () => { fetchPeers() })
410411
ctx.tray = tray
411412
logger.info('[tray] started')
412413
}

0 commit comments

Comments
 (0)