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

feat: add contents.ipc (EventEmitter), which allows handling messages for given webContents #16420

Closed
wants to merge 1 commit into from

Conversation

miniak
Copy link
Contributor

@miniak miniak commented Jan 16, 2019

Description of Change

  • The motivation is to simplify application code, when messages only from a particular webContents instance are being handled.
  • This is especially handy when using once, where doing the event.sender === someWebContents check can't be done in the listener, as it would be unregistered even if we don't handle the message.
  • We cannot just emit the IPC events on the webContents directly as it could clash with existing events.
  • It allows to use webContents.ipc.removeAllListeners(), which is safe(r) unlike ipcMain.removeAllListeners()

example

const win = new BrowserWindow()
...

// this does not work properly
ipcMain.once('ready', (event) => {
  if (event.sender === win.webContents) {
    console.log('ready')
  }
})

// this is annoying
ipcMain.on('ready', function handler (event) => {
  if (event.sender === win.webContents) {
    console.log('ready')
    ipcMain.removeListener('ready', handler)
  }
})

// this is much better
win.webContents.ipc.once('ready', () => {
  console.log('ready')
})

Checklist

Release Notes

Notes: Added contents.ipc (EventEmitter), which allows handling messages for given webContents

@miniak miniak requested review from a team January 16, 2019 16:53
@miniak miniak changed the title feat: add contents.ipc: EventEmitter, which allows handling messages for given instance feat: add contents.ipc (EventEmitter), which allows handling messages for given webContents Jan 16, 2019
@miniak miniak self-assigned this Jan 16, 2019
Copy link
Contributor

@alexeykuzmin alexeykuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Tests maybe?

Copy link
Member

@MarshallOfSound MarshallOfSound left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this just adds another route to do the same stuff you can already do. In particular something I think was overlooked here was the ipc-message event.

webContents.on('ipc-message', handler)

I don't think adding another source of events is a good design choice here. Should discuss the advantage of this comparing to the above ipc-message event.

@miniak
Copy link
Contributor Author

miniak commented Jan 16, 2019

@MarshallOfSound ipc-message on webContents is not documented, it's an implementation detail basically. And it's not as convenient to use as you cannot directly listen for individual messages.

@miniak
Copy link
Contributor Author

miniak commented Jan 16, 2019

@MarshallOfSound also you would have to handle ipc-message-sync for sync messages separately. In this case we are also adding the returnValue property on the event in the internal code before doing ipcMain.emit

@miniak
Copy link
Contributor Author

miniak commented Jan 16, 2019

@MarshallOfSound it actually seems like the code around ipc-message should be refactored to make it more robust as you would currently break the forwarding to ipcMain by calling webContents.removeAllListeners()

@miniak
Copy link
Contributor Author

miniak commented Jan 16, 2019

@nornagon, @zcbenz, @codebytere what do you think?

@nornagon
Copy link
Member

I agree with @MarshallOfSound that this seems like what the ipc-message event is for. I'd be interested to see a PR to clean up & refactor & document that event. I don't think it's a problem that ipc-message-sync is a separate event—synchronous messages are substantially different to asynchronous ones and you should know which kind you're dealing with.

@miniak
Copy link
Contributor Author

miniak commented Jan 20, 2019

@nornagon, @MarshallOfSound fair enough. I will create a new PR to make the ipc-message and ipc-message-sync events public + necessary refactoring

@miniak
Copy link
Contributor Author

miniak commented Jan 20, 2019

Replaced by #16468

@miniak miniak closed this Jan 20, 2019
@miniak miniak deleted the miniak/webcontents-ipc branch January 20, 2019 10:44
@miniak miniak mentioned this pull request Jul 20, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants