Don't rely on the global process object for message dispatching
This commit is contained in:
parent
c14c6a3521
commit
a10782cdea
3 changed files with 27 additions and 25 deletions
|
@ -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
|
||||
|
|
|
@ -29,10 +29,10 @@ namespace atom {
|
|||
|
||||
namespace {
|
||||
|
||||
v8::Handle<v8::Object> GetProcessObject(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Context> context) {
|
||||
v8::Handle<v8::String> key = mate::StringToV8(isolate, "process");
|
||||
return context->Global()->Get(key)->ToObject();
|
||||
v8::Handle<v8::Object> GetIPCObject(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Context> context) {
|
||||
v8::Handle<v8::String> key = mate::StringToV8(isolate, "ipc");
|
||||
return context->Global()->GetHiddenValue(key)->ToObject();
|
||||
}
|
||||
|
||||
std::vector<v8::Handle<v8::Value>> ListValueToVector(
|
||||
|
@ -118,8 +118,8 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
|
|||
isolate, args);
|
||||
arguments.insert(arguments.begin(), mate::ConvertToV8(isolate, channel));
|
||||
|
||||
v8::Handle<v8::Object> process = GetProcessObject(isolate, context);
|
||||
node::MakeCallback(isolate, process, "emit", arguments.size(), &arguments[0]);
|
||||
v8::Handle<v8::Object> ipc = GetIPCObject(isolate, context);
|
||||
node::MakeCallback(isolate, ipc, "emit", arguments.size(), &arguments[0]);
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
process = global.process
|
||||
events = require 'events'
|
||||
path = require 'path'
|
||||
url = require 'url'
|
||||
Module = require 'module'
|
||||
|
@ -21,6 +22,10 @@ globalPaths.push path.join(process.resourcesPath, 'app')
|
|||
# Import common settings.
|
||||
require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init')
|
||||
|
||||
# The global variable will be used by ipc for event dispatching
|
||||
v8Util = process.atomBinding 'v8_util'
|
||||
v8Util.setHiddenValue global, 'ipc', new events.EventEmitter
|
||||
|
||||
# Process command line arguments.
|
||||
nodeIntegration = 'false'
|
||||
for arg in process.argv
|
||||
|
|
Loading…
Add table
Reference in a new issue