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

View file

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