Avoid excessive numbers of IPC event listeners in ApplicationDelegate

This commit is contained in:
Max Brunsfeld
2018-07-26 10:20:01 -07:00
parent c52dc23242
commit 1e2e461e5e
2 changed files with 20 additions and 58 deletions

View File

@@ -1,12 +1,16 @@
const {ipcRenderer, remote, shell} = require('electron')
const ipcHelpers = require('./ipc-helpers')
const {Disposable} = require('event-kit')
const {Emitter, Disposable} = require('event-kit')
const getWindowLoadSettings = require('./get-window-load-settings')
module.exports =
class ApplicationDelegate {
constructor () {
this.pendingSettingsUpdateCount = 0
this.ipcMessageEmitter = new Emitter()
ipcRenderer.on('message', (event, message, detail) => {
this.ipcMessageEmitter.emit(message, detail)
})
}
getWindowLoadSettings () { return getWindowLoadSettings() }
@@ -189,21 +193,13 @@ class ApplicationDelegate {
}
onDidChangeUserSettings (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'did-change-user-settings') {
if (this.pendingSettingsUpdateCount === 0) callback(detail)
}
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('did-change-user-settings', detail => {
if (this.pendingSettingsUpdateCount === 0) callback(detail)
})
}
onDidFailToReadUserSettings (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'did-fail-to-read-user-settings') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('did-fail-to-read-user-setting', callback)
}
confirm (options, callback) {
@@ -261,24 +257,14 @@ class ApplicationDelegate {
}
onDidOpenLocations (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'open-locations') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('open-locations', callback)
}
onUpdateAvailable (callback) {
const outerCallback = (event, message, detail) => {
// TODO: Yes, this is strange that `onUpdateAvailable` is listening for
// `did-begin-downloading-update`. We currently have no mechanism to know
// if there is an update, so begin of downloading is a good proxy.
if (message === 'did-begin-downloading-update') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
// TODO: Yes, this is strange that `onUpdateAvailable` is listening for
// `did-begin-downloading-update`. We currently have no mechanism to know
// if there is an update, so begin of downloading is a good proxy.
return this.ipcMessageEmitter.on('did-begin-downloading-update', callback)
}
onDidBeginDownloadingUpdate (callback) {
@@ -286,40 +272,19 @@ class ApplicationDelegate {
}
onDidBeginCheckingForUpdate (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'checking-for-update') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('checking-for-update', callback)
}
onDidCompleteDownloadingUpdate (callback) {
const outerCallback = (event, message, detail) => {
// TODO: We could rename this event to `did-complete-downloading-update`
if (message === 'update-available') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('update-available', callback)
}
onUpdateNotAvailable (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'update-not-available') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('update-not-available', callback)
}
onUpdateError (callback) {
const outerCallback = (event, message, detail) => {
if (message === 'update-error') callback(detail)
}
ipcRenderer.on('message', outerCallback)
return new Disposable(() => ipcRenderer.removeListener('message', outerCallback))
return this.ipcMessageEmitter.on('update-error', callback)
}
onApplicationMenuCommand (handler) {