Modify funtion's names to make RPC code more easy to understand.

This commit is contained in:
Cheng Zhao 2013-05-04 21:38:25 +08:00
parent 36c260f4d5
commit 6bf0d5d798
2 changed files with 63 additions and 64 deletions

View file

@ -3,59 +3,59 @@ path = require 'path'
objectsRegistry = require './objects_registry.js'
v8_util = process.atomBinding 'v8_util'
# Convert array of meta data from renderer into array of real values.
unwrapArgs = (processId, routingId, args) ->
args.map (arg) ->
if arg.type is 'value'
arg.value
else if arg.type is 'remoteObject'
objectsRegistry.get arg.id
else if arg.type is 'function'
ret = ->
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', arg.id, new Meta(processId, routingId, arguments)
v8_util.setDestructor ret, ->
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_RELEASE_CALLBACK', arg.id
ret
else
throw new TypeError("Unknown type: #{arg.type}")
args.map (meta) ->
switch meta.type
when 'value' then meta.value
when 'object' then objectsRegistry.get meta.id
when 'function'
ret = ->
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(processId, routingId, arguments)
v8_util.setDestructor ret, ->
ipc.sendChannel processId, routingId, 'ATOM_RENDERER_RELEASE_CALLBACK', meta.id
ret
else throw new TypeError("Unknown type: #{meta.type}")
# Convert a real value into a POD structure which carries information of this
# value.
class Meta
constructor: (processId, routingId, value) ->
@type = typeof value
@type = 'value' if value is null
@type = 'array' if Array.isArray value
# Convert a real value into meta data.
valueToMeta = (processId, routingId, value) ->
meta = type: typeof value
# Treat the arguments object as array.
@type = 'array' if @type is 'object' and value.callee? and value.length?
meta.type = 'value' if value is null
meta.type = 'array' if Array.isArray value
if @type is 'array'
@members = []
@members.push new Meta(processId, routingId, el) for el in value
else if @type is 'object' or @type is 'function'
@name = value.constructor.name
# Treat the arguments object as array.
meta.type = 'array' if meta.type is 'object' and value.callee? and value.length?
# Reference the original value if it's an object, because when it's
# passed to renderer we would assume the renderer keeps a reference of
# it.
@storeId = objectsRegistry.add processId, routingId, value
@id = value.id
if meta.type is 'array'
meta.members = []
meta.members.push valueToMeta(processId, routingId, el) for el in value
else if meta.type is 'object' or meta.type is 'function'
meta.name = value.constructor.name
@members = []
@members.push { name: prop, type: typeof field } for prop, field of value
else
@type = 'value'
@value = value
# Reference the original value if it's an object, because when it's
# passed to renderer we would assume the renderer keeps a reference of
# it.
meta.storeId = objectsRegistry.add processId, routingId, value
meta.id = value.id
meta.members = []
meta.members.push {name: prop, type: typeof field} for prop, field of value
else
meta.type = 'value'
meta.value = value
meta
ipc.on 'ATOM_BROWSER_REQUIRE', (event, processId, routingId, module) ->
try
event.result = new Meta(processId, routingId, require(module))
event.result = valueToMeta processId, routingId, require(module)
catch e
event.result = type: 'error', value: e.message
ipc.on 'ATOM_BROWSER_GLOBAL', (event, processId, routingId, name) ->
try
event.result = new Meta(processId, routingId, global[name])
event.result = valueToMeta processId, routingId, global[name]
catch e
event.result = type: 'error', value: e.message
@ -68,7 +68,7 @@ ipc.on 'ATOM_BROWSER_CURRENT_WINDOW', (event, processId, routingId) ->
for window in windows
break if window.getProcessId() == processId and
window.getRoutingId() == routingId
event.result = new Meta(processId, routingId, window)
event.result = valueToMeta processId, routingId, window
catch e
event.result = type: 'error', value: e.message
@ -79,7 +79,7 @@ ipc.on 'ATOM_BROWSER_CONSTRUCTOR', (event, processId, routingId, id, args) ->
# Call new with array of arguments.
# http://stackoverflow.com/questions/1606797/use-of-apply-with-new-operator-is-this-possible
obj = new (Function::bind.apply(constructor, [null].concat(args)))
event.result = new Meta(processId, routingId, obj)
event.result = valueToMeta processId, routingId, obj
catch e
event.result = type: 'error', value: e.message
@ -88,7 +88,7 @@ ipc.on 'ATOM_BROWSER_FUNCTION_CALL', (event, processId, routingId, id, args) ->
args = unwrapArgs processId, routingId, args
func = objectsRegistry.get id
ret = func.apply global, args
event.result = new Meta(processId, routingId, ret)
event.result = valueToMeta processId, routingId, ret
catch e
event.result = type: 'error', value: e.message
@ -97,7 +97,7 @@ ipc.on 'ATOM_BROWSER_MEMBER_CALL', (event, processId, routingId, id, method, arg
args = unwrapArgs processId, routingId, args
obj = objectsRegistry.get id
ret = obj[method].apply(obj, args)
event.result = new Meta(processId, routingId, ret)
event.result = valueToMeta processId, routingId, ret
catch e
event.result = type: 'error', value: e.message
@ -111,14 +111,14 @@ ipc.on 'ATOM_BROWSER_MEMBER_SET', (event, processId, routingId, id, name, value)
ipc.on 'ATOM_BROWSER_MEMBER_GET', (event, processId, routingId, id, name) ->
try
obj = objectsRegistry.get id
event.result = new Meta(processId, routingId, obj[name])
event.result = valueToMeta processId, routingId, obj[name]
catch e
event.result = type: 'error', value: e.message
ipc.on 'ATOM_BROWSER_REFERENCE', (event, processId, routingId, id) ->
try
obj = objectsRegistry.get id
event.result = new Meta(processId, routingId, obj)
event.result = valueToMeta processId, routingId, obj
catch e
event.result = type: 'error', value: e.message

View file

@ -1,34 +1,31 @@
ipc = require 'ipc'
v8_util = process.atomBinding 'v8_util'
nextCallbackId = 0
storedCallbacks = {}
class CallbacksRegistry
@nextId = 0
@callbacks = {}
storeCallback = (callback) ->
++nextCallbackId
storedCallbacks[nextCallbackId] = callback
nextCallbackId
@add: (callback) ->
@callbacks[++@nextId] = callback
@nextId
makeCallback = (id, args) ->
storedCallbacks[id].apply global, args
@call: (id, args) ->
@callbacks[id].apply global, args
releaseCallback = (id) ->
delete storedCallbacks[id]
@remove: (id) ->
delete @callbacks[id]
# Transform the arguments passed to browser into list of descriptions.
#
# This function assumes an array is passed, and it only converts remote objects
# and functions, other types of values are passed as it is.
# Convert the arguments object into an array of meta data.
wrapArgs = (args) ->
Array::slice.call(args).map (value) ->
if typeof value is 'object' and v8_util.getHiddenValue value, 'isRemoteObject'
type: 'remoteObject', id: value.id
type: 'object', id: value.id
else if typeof value is 'function'
type: 'function', id: storeCallback(value)
type: 'function', id: CallbacksRegistry.add(value)
else
type: 'value', value: value
# Transform the description of value into a value or delegate object.
# Convert meta data from browser into real value.
metaToValue = (meta) ->
switch meta.type
when 'error' then throw new Error(meta.value)
@ -83,11 +80,13 @@ metaToValue = (meta) ->
ret
# Browser calls a callback in renderer.
ipc.on 'ATOM_RENDERER_CALLBACK', (id, args) ->
makeCallback id, metaToValue(args)
CallbacksRegistry.call id, metaToValue(args)
# A callback in browser is released.
ipc.on 'ATOM_RENDERER_RELEASE_CALLBACK', (id) ->
releaseCallback id
CallbacksRegistry.remove id
# Release all resources of current render view when it's going to be unloaded.
window.addEventListener 'unload', (event) ->