Skip to content

Commit

Permalink
fix(Desktop): Fix issues when trying to register duplicate IPC handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Jun 10, 2021
1 parent c9dbb47 commit 1504b37
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 110 deletions.
56 changes: 30 additions & 26 deletions desktop/src/main/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { showSettings } from './window'
export const getConfig = async () => {
return {
config: config.read(),
schema: config.schema(),
schema: config.schema()
}
}

Expand All @@ -22,45 +22,49 @@ export const getPlugins = () => {
(pluginObject: NormalizedPlugins, plugin) => {
return {
entities: { ...pluginObject.entities, [plugin.name]: plugin },
ids: [...pluginObject.ids, plugin.name],
ids: [...pluginObject.ids, plugin.name]
}
},
{ entities: {}, ids: [] }
)
}

export const registerConfigHandlers = () => {
ipcMain.handle(CHANNEL.OPEN_CONFIG_WINDOW, async () => {
return showSettings()
})
try {
ipcMain.handle(CHANNEL.OPEN_CONFIG_WINDOW, async () => {
return showSettings()
})

ipcMain.handle(CHANNEL.READ_CONFIG, async () => {
return getConfig()
})
ipcMain.handle(CHANNEL.READ_CONFIG, async () => {
return getConfig()
})

ipcMain.handle(CHANNEL.READ_PLUGINS, async () => {
return getPlugins()
})
ipcMain.handle(CHANNEL.READ_PLUGINS, async () => {
return getPlugins()
})

ipcMain.handle(CHANNEL.LIST_AVAILABLE_PLUGINS, async () => {
return getPlugins()
})
ipcMain.handle(CHANNEL.LIST_AVAILABLE_PLUGINS, async () => {
return getPlugins()
})

ipcMain.handle(CHANNEL.INSTALL_PLUGIN, async (_event, name) => {
return plugins.install(name)
})
ipcMain.handle(CHANNEL.INSTALL_PLUGIN, async (_event, name) => {
return plugins.install(name)
})

ipcMain.handle(CHANNEL.UNINSTALL_PLUGIN, async (_event, name) => {
return plugins.uninstall(name)
})
ipcMain.handle(CHANNEL.UNINSTALL_PLUGIN, async (_event, name) => {
return plugins.uninstall(name)
})

ipcMain.handle(CHANNEL.UPGRADE_PLUGIN, async (_event, name) => {
return plugins.upgrade(name)
})
ipcMain.handle(CHANNEL.UPGRADE_PLUGIN, async (_event, name) => {
return plugins.upgrade(name)
})

ipcMain.handle(CHANNEL.REFRESH_PLUGINS, async () => {
return plugins.refresh(plugins.list().map((plugin) => plugin.name))
})
ipcMain.handle(CHANNEL.REFRESH_PLUGINS, async () => {
return plugins.refresh(plugins.list().map(plugin => plugin.name))
})
} catch {
// Handlers likely already registered
}
}

export const removeConfigHandlers = () => {
Expand Down
105 changes: 58 additions & 47 deletions desktop/src/main/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,71 @@ import { removeChannelHandlers } from '../utils/handler'
import { DOCUMENT_CHANNEL } from './channel'

export const registerDocumentHandlers = () => {
ipcMain.handle(CHANNEL.OPEN_DOCUMENT, async (_event, filePath: string) => {
return documents.open(filePath)
})
try {
ipcMain.handle(CHANNEL.OPEN_DOCUMENT, async (_event, filePath: string) => {
return documents.open(filePath)
})

ipcMain.handle(CHANNEL.CLOSE_DOCUMENT, async (_event, documentId: string) => {
try {
documents.close(documentId)
} catch (e) {}
})
ipcMain.handle(
CHANNEL.CLOSE_DOCUMENT,
async (_event, documentId: string) => {
try {
documents.close(documentId)
} catch (e) {}
}
)

ipcMain.handle(
CHANNEL.UNSUBSCRIBE_DOCUMENT,
async (
_event,
{ documentId, topics }: { documentId: string; topics: string[] }
) => {
documents.unsubscribe(documentId, topics)
}
)
ipcMain.handle(
CHANNEL.UNSUBSCRIBE_DOCUMENT,
async (
_event,
{ documentId, topics }: { documentId: string; topics: string[] }
) => {
documents.unsubscribe(documentId, topics)
}
)

ipcMain.handle(
CHANNEL.GET_DOCUMENT_CONTENTS,
async (ipcEvent, documentId: string) => {
documents.subscribe(documentId, ['modified'], (_topic, docEvent) => {
ipcEvent.sender.send(CHANNEL.GET_DOCUMENT_CONTENTS, docEvent)
})
ipcMain.handle(
CHANNEL.GET_DOCUMENT_CONTENTS,
async (ipcEvent, documentId: string) => {
documents.subscribe(documentId, ['modified'], (_topic, docEvent) => {
ipcEvent.sender.send(CHANNEL.GET_DOCUMENT_CONTENTS, docEvent)
})

// Use `dump` to get document content, rather than `read`, to avoid
// (a) a re-read of the file (that is done on open) (b) re-encoding for
// each subscriber.
return documents.dump(documentId)
}
)
// Use `dump` to get document content, rather than `read`, to avoid
// (a) a re-read of the file (that is done on open) (b) re-encoding for
// each subscriber.
return documents.dump(documentId)
}
)

ipcMain.handle(
CHANNEL.DOCUMENT_GET_PREVIEW,
async (ipcEvent, documentId: string) => {
documents.subscribe(documentId, ['encoded:html'], (_topic, docEvent) => {
ipcEvent.sender.send(CHANNEL.DOCUMENT_GET_PREVIEW, docEvent)
})
ipcMain.handle(
CHANNEL.DOCUMENT_GET_PREVIEW,
async (ipcEvent, documentId: string) => {
documents.subscribe(
documentId,
['encoded:html'],
(_topic, docEvent) => {
ipcEvent.sender.send(CHANNEL.DOCUMENT_GET_PREVIEW, docEvent)
}
)

return documents.dump(documentId, 'html')
}
)
return documents.dump(documentId, 'html')
}
)

ipcMain.handle(
CHANNEL.SAVE_DOCUMENT,
async (
_event,
{ documentId, content }: { documentId: string; content: string }
) => {
documents.write(documentId, content)
}
)
ipcMain.handle(
CHANNEL.SAVE_DOCUMENT,
async (
_event,
{ documentId, content }: { documentId: string; content: string }
) => {
documents.write(documentId, content)
}
)
} catch {
// Handlers likely already registered
}
}

export const removeDocoumentHandlers = () => {
Expand Down
16 changes: 10 additions & 6 deletions desktop/src/main/launcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { LAUNCHER_CHANNEL } from './channels'
import { closeLauncherWindow, openLauncherWindow } from './window'

export const registerLauncherHandlers = () => {
ipcMain.handle(CHANNEL.OPEN_LAUNCHER_WINDOW, async () => {
openLauncherWindow()
})
try {
ipcMain.handle(CHANNEL.OPEN_LAUNCHER_WINDOW, async () => {
openLauncherWindow()
})

ipcMain.handle(CHANNEL.CLOSE_LAUNCHER_WINDOW, async () => {
closeLauncherWindow()
})
ipcMain.handle(CHANNEL.CLOSE_LAUNCHER_WINDOW, async () => {
closeLauncherWindow()
})
} catch {
// Handlers likely already registered
}
}

export const removeLauncherHandlers = () => {
Expand Down
16 changes: 10 additions & 6 deletions desktop/src/main/onboarding/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { ONBOARDING_CHANNEL } from './channels'
import { closeOnboardingWindow, openOnboardingWindow } from './window'

export const registerOnboardingHandlers = () => {
ipcMain.handle(CHANNEL.OPEN_ONBOARDING_WINDOW, async () => {
return openOnboardingWindow()
})
try {
ipcMain.handle(CHANNEL.OPEN_ONBOARDING_WINDOW, async () => {
return openOnboardingWindow()
})

ipcMain.handle(CHANNEL.CLOSE_ONBOARDING_WINDOW, async () => {
return closeOnboardingWindow()
})
ipcMain.handle(CHANNEL.CLOSE_ONBOARDING_WINDOW, async () => {
return closeOnboardingWindow()
})
} catch {
// Handlers likely already registered
}
}

export const removeOnboaringHandlers = () => {
Expand Down
12 changes: 10 additions & 2 deletions desktop/src/main/onboarding/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BrowserWindow } from 'electron'
import { registerOnboardingHandlers, removeOnboaringHandlers } from '.'
import { createWindow } from '../../app/window'
import { i18n } from '../../i18n'
import { registerConfigHandlers, removeConfigHandlers } from '../config'
import { registerLauncherHandlers, removeLauncherHandlers } from '../launcher'

let onboardingWindow: BrowserWindow | null

Expand All @@ -13,18 +15,24 @@ export const openOnboardingWindow = () => {
height: 600,
maxWidth: 1000,
minWidth: 600,
minHeight: 600,
maxHeight: 800,
minHeight: 500,
show: false,
title: i18n.t('onboarding.title'),
center: true,
fullscreenable: false,
center: true
})

onboardingWindow.on('closed', () => {
removeLauncherHandlers()
removeConfigHandlers()
removeOnboaringHandlers()
onboardingWindow = null
})

onboardingWindow.webContents.on('did-finish-load', () => {
registerLauncherHandlers()
registerConfigHandlers()
registerOnboardingHandlers()
onboardingWindow?.show()
})
Expand Down
50 changes: 27 additions & 23 deletions desktop/src/main/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ import { openProject } from './handlers'
import { openProjectWindow } from './window'

export const registerProjectHandlers = () => {
ipcMain.handle(
CHANNEL.SHOW_PROJECT_WINDOW,
async (_event, directoryPath: string) => {
openProjectWindow(directoryPath)
}
)
try {
ipcMain.handle(
CHANNEL.SHOW_PROJECT_WINDOW,
async (_event, directoryPath: string) => {
openProjectWindow(directoryPath)
}
)

ipcMain.handle(CHANNEL.SELECT_PROJECT_DIR, async () => {
openProject()
})
ipcMain.handle(CHANNEL.SELECT_PROJECT_DIR, async () => {
openProject()
})

ipcMain.handle(
CHANNEL.OPEN_PROJECT,
async (_event, directoryPath: string) => {
openProjectWindow(directoryPath)
}
)
ipcMain.handle(
CHANNEL.OPEN_PROJECT,
async (_event, directoryPath: string) => {
openProjectWindow(directoryPath)
}
)

ipcMain.handle(
CHANNEL.GET_PROJECT_FILES,
async (ipcEvent, directoryPath: string) => {
return projects.open(directoryPath, (_topic, projectEvent) => {
ipcEvent.sender.send(CHANNEL.GET_PROJECT_FILES, projectEvent)
})
}
)
ipcMain.handle(
CHANNEL.GET_PROJECT_FILES,
async (ipcEvent, directoryPath: string) => {
return projects.open(directoryPath, (_topic, projectEvent) => {
ipcEvent.sender.send(CHANNEL.GET_PROJECT_FILES, projectEvent)
})
}
)
} catch {
// Handlers likely already registered
}
}

export const removeProjectHandlers = () => {
Expand Down

0 comments on commit 1504b37

Please sign in to comment.