Handle port disconnecting

This commit is contained in:
Cheng Zhao 2016-05-28 12:07:08 +09:00
parent 599d3c147b
commit d8db695712
3 changed files with 42 additions and 16 deletions

View file

@ -24,8 +24,8 @@ class OnConnect extends Event {
constructor () {
super()
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, extensionId, connectInfo) => {
this.emit(new Port(webContentsId, extensionId, connectInfo.name))
ipcRenderer.on('CHROME_RUNTIME_ONCONNECT', (event, webContentsId, portId, connectInfo) => {
this.emit(new Port(webContentsId, portId, connectInfo.name))
})
}
}
@ -41,25 +41,35 @@ class MessageSender {
}
class Port {
constructor (webContentsId, extensionId, name) {
constructor (webContentsId, portId, name) {
this.webContentsId = webContentsId
this.extensionId = extensionId
this.portId = portId
this.name = name
this.onDisconnect = new Event()
this.onMessage = new Event()
this.sender = new MessageSender()
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${extensionId}`, (event, message) => {
ipcRenderer.once(`CHROME_PORT_ONDISCONNECT_${portId}`, () => {
this._onDisconnect()
})
ipcRenderer.on(`CHROME_PORT_ONMESSAGE_${portId}`, (event, message) => {
this.onMessage.emit(message, new MessageSender(), function () {})
})
}
disconnect () {
ipcRenderer.send('CHROME_PORT_DISCONNECT', this.webContentsId, this.portId)
this._onDisconnect()
}
postMessage (message) {
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.extensionId, message)
ipcRenderer.send('CHROME_PORT_POSTMESSAGE', this.webContentsId, this.portId, message)
}
_onDisconnect() {
ipcRenderer.removeAllListeners(`CHROME_PORT_ONMESSAGE_${this.portId}`)
this.onDisconnect.emit()
}
}
@ -91,7 +101,7 @@ chrome.runtime = {
[extensionId, connectInfo] = args
}
const webContentsId = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', extensionId, connectInfo)
return new Port(webContentsId, extensionId, connectInfo.name)
const {webContentsId, portId} = ipcRenderer.sendSync('CHROME_RUNTIME_CONNECT', extensionId, connectInfo)
return new Port(webContentsId, portId, connectInfo.name)
}
}