Don't rely on the global process object for message dispatching

This commit is contained in:
Cheng Zhao 2015-01-26 18:42:43 -08:00
parent c14c6a3521
commit a10782cdea
3 changed files with 27 additions and 25 deletions

View file

@ -1,26 +1,23 @@
EventEmitter = require('events').EventEmitter
process = global.process
ipc = process.atomBinding('ipc')
binding = process.atomBinding 'ipc'
v8Util = process.atomBinding 'v8_util'
class Ipc extends EventEmitter
constructor: ->
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
@emit args...
# Created by init.coffee.
ipc = v8Util.getHiddenValue global, 'ipc'
window.addEventListener 'unload', (event) ->
process.removeAllListeners 'ATOM_INTERNAL_MESSAGE'
ipc.on 'ATOM_INTERNAL_MESSAGE', (args...) ->
@emit args...
send: (args...) ->
ipc.send 'ipc-message', [args...]
ipc.send = (args...) ->
binding.send 'ipc-message', [args...]
sendSync: (args...) ->
JSON.parse ipc.sendSync('ipc-message-sync', [args...])
ipc.sendSync = (args...) ->
JSON.parse binding.sendSync('ipc-message-sync', [args...])
sendToHost: (args...) ->
ipc.send 'ipc-message-host', [args...]
ipc.sendToHost = (args...) ->
binding.send 'ipc-message-host', [args...]
# Discarded
sendChannel: -> @send.apply this, arguments
sendChannelSync: -> @sendSync.apply this, arguments
# Deprecated.
ipc.sendChannel = ipc.send
ipc.sendChannelSync = ipc.sendSync
module.exports = new Ipc
module.exports = ipc