How to introduce external dependencies as part of the handler definition? #14
Unanswered
theogravity
asked this question in
Q&A
Replies: 1 comment 1 reply
-
|
Hi @theogravity , currently in one project I'm working in, I'm avoiding dummy implementation in favor to do something like this:
import { createInterprocess } from 'interprocess'
import { openDialog } from 'main/modules/dialog/open'
export const { ipcMain } =
createInterprocess({
main: {
openDialog,
},
})
import { OpenDialogOptions, dialog } from 'electron'
export async function openDialog(
_: Electron.IpcMainInvokeEvent,
data: OpenDialogOptions
) {
return dialog.showOpenDialog(data)
}
import { ipcMain } from 'shared/ipcs'
export function registerDialogModule() {
ipcMain.handle.openDialog()
}When I need to handle more things before returning the response to the renderer process, as mentioned in the docs, we can handle like this: export function registerGetPingModule(window: Electron.BrowserWindow) {
ipcMain.handle.getPing(async (_, { getPing, data }) => {
// call the registered handler if needed
const response = await getPing(_, data)
await ipcMain.invoke.getPong(mainWindow, 'pong')
return response
})
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say that I have a handler that requires a
browserWindowto be fed into it. Given the way the handlers are defined viacreateInterprocess, there's no way to introduce external dependencies like this as part of the initial handler definition.I've found that I have to leave it as a dummy implementation then wrap the handler as part of a closure to do that, ex:
Is there a better way to do it?
Beta Was this translation helpful? Give feedback.
All reactions