2013-04-23 04:18:07 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2013-04-27 11:13:24 +00:00
|
|
|
send = process.atomBinding('ipc').send
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2013-05-17 15:40:37 +00:00
|
|
|
sendWrap = (channel, processId, routingId, args...) ->
|
2013-05-30 12:16:54 +00:00
|
|
|
BrowserWindow = require 'browser-window'
|
2013-05-17 15:40:37 +00:00
|
|
|
if processId?.constructor is BrowserWindow
|
|
|
|
window = processId
|
|
|
|
processId = window.getProcessId()
|
|
|
|
routingId = window.getRoutingId()
|
|
|
|
|
|
|
|
send channel, processId, routingId, args...
|
|
|
|
|
2013-04-23 04:18:07 +00:00
|
|
|
class Ipc extends EventEmitter
|
|
|
|
constructor: ->
|
|
|
|
process.on 'ATOM_INTERNAL_MESSAGE', (args...) =>
|
2013-04-23 12:57:14 +00:00
|
|
|
@emit(args...)
|
2013-09-20 14:32:59 +00:00
|
|
|
process.on 'ATOM_INTERNAL_MESSAGE_SYNC', (channel, event, args...) =>
|
2013-09-22 01:52:58 +00:00
|
|
|
returnValue = null
|
2013-09-20 14:32:59 +00:00
|
|
|
get = -> returnValue
|
2013-09-22 01:52:58 +00:00
|
|
|
set = (value) ->
|
|
|
|
throw new Error('returnValue can be only set once') if returnValue?
|
|
|
|
returnValue = JSON.stringify(value)
|
|
|
|
event.sendReply()
|
2013-09-20 14:32:59 +00:00
|
|
|
|
|
|
|
Object.defineProperty event, 'returnValue', {get, set}
|
|
|
|
Object.defineProperty event, 'result', {get, set}
|
|
|
|
|
|
|
|
@emit(channel, event, args...)
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2013-04-27 11:13:24 +00:00
|
|
|
send: (processId, routingId, args...) ->
|
|
|
|
@sendChannel(processId, routingId, 'message', args...)
|
2013-04-23 09:21:34 +00:00
|
|
|
|
|
|
|
sendChannel: (args...) ->
|
2013-05-17 15:40:37 +00:00
|
|
|
sendWrap('ATOM_INTERNAL_MESSAGE', args...)
|
2013-04-23 04:18:07 +00:00
|
|
|
|
|
|
|
module.exports = new Ipc
|