Better support of array type in RPC.
This commit is contained in:
		
					parent
					
						
							
								ab4015ef51
							
						
					
				
			
			
				commit
				
					
						678a4953fa
					
				
			
		
					 2 changed files with 45 additions and 42 deletions
				
			
		| 
						 | 
					@ -8,14 +8,17 @@ class PlainObject
 | 
				
			||||||
  constructor: (value) ->
 | 
					  constructor: (value) ->
 | 
				
			||||||
    @type = typeof value
 | 
					    @type = typeof value
 | 
				
			||||||
    @type = 'value' if value is null
 | 
					    @type = 'value' if value is null
 | 
				
			||||||
 | 
					    @type = 'array' if Array.isArray value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if @type is 'object' or @type is 'function'
 | 
					    if @type is 'array'
 | 
				
			||||||
 | 
					      @members = []
 | 
				
			||||||
 | 
					      @members.push new PlainObject(el) for el in value
 | 
				
			||||||
 | 
					    else if @type is 'object' or @type is 'function'
 | 
				
			||||||
      @name = value.constructor.name
 | 
					      @name = value.constructor.name
 | 
				
			||||||
      @id = objectsRegistry.add value
 | 
					      @id = objectsRegistry.add value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      @members = []
 | 
					      @members = []
 | 
				
			||||||
      for prop, field of value
 | 
					      @members.push { name: prop, type: typeof field } for prop, field of value
 | 
				
			||||||
        @members.push { name: prop, type: typeof field }
 | 
					 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
      @type = 'value'
 | 
					      @type = 'value'
 | 
				
			||||||
      @value = value
 | 
					      @value = value
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,49 +1,49 @@
 | 
				
			||||||
ipc = require 'ipc'
 | 
					ipc = require 'ipc'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
generateFromPainObject = (plain) ->
 | 
					generateFromPainObject = (plain) ->
 | 
				
			||||||
  if plain.type is 'value'
 | 
					  switch plain.type
 | 
				
			||||||
    return plain.value
 | 
					    when 'error' then throw new Error('Remote Error: ' + plain.value)
 | 
				
			||||||
  else if plain.type is 'error'
 | 
					    when 'value' then plain.value
 | 
				
			||||||
    throw new Error('Remote Error: ' + plain.value)
 | 
					    when 'array' then (generateFromPainObject(el) for el in plain.members)
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      ret = {}
 | 
				
			||||||
 | 
					      if plain.type is 'function'
 | 
				
			||||||
 | 
					        # A shadow class to represent the remote function object.
 | 
				
			||||||
 | 
					        ret =
 | 
				
			||||||
 | 
					        class RemoteObject
 | 
				
			||||||
 | 
					          constructor: ->
 | 
				
			||||||
 | 
					            if @constructor == RemoteObject
 | 
				
			||||||
 | 
					              # Constructor call.
 | 
				
			||||||
 | 
					              obj = ipc.sendChannelSync 'ATOM_INTERNAL_CONSTRUCTOR', plain.id, Array::slice.call(arguments)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ret = {}
 | 
					              # Returning object in constructor will replace constructed object
 | 
				
			||||||
  if plain.type is 'function'
 | 
					              # with the returned object.
 | 
				
			||||||
    # A shadow class to represent the remote function object.
 | 
					              # http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this
 | 
				
			||||||
    ret =
 | 
					              return generateFromPainObject obj
 | 
				
			||||||
    class RemoteObject
 | 
					            else
 | 
				
			||||||
      constructor: ->
 | 
					              # Function call.
 | 
				
			||||||
        if @constructor == RemoteObject
 | 
					              ret = ipc.sendChannelSync 'ATOM_INTERNAL_FUNCTION_CALL', plain.id, Array::slice.call(arguments)
 | 
				
			||||||
          # Constructor call.
 | 
					              return generateFromPainObject ret
 | 
				
			||||||
          obj = ipc.sendChannelSync 'ATOM_INTERNAL_CONSTRUCTOR', plain.id, Array::slice.call(arguments)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          # Returning object in constructor will replace constructed object
 | 
					      # Polulate delegate members.
 | 
				
			||||||
          # with the returned object.
 | 
					      for member in plain.members
 | 
				
			||||||
          # http://stackoverflow.com/questions/1978049/what-values-can-a-constructor-return-to-avoid-returning-this
 | 
					        do (member) ->
 | 
				
			||||||
          return generateFromPainObject obj
 | 
					          if member.type is 'function'
 | 
				
			||||||
        else
 | 
					            ret[member.name] = ->
 | 
				
			||||||
          # Function call.
 | 
					              # Call member function.
 | 
				
			||||||
          ret = ipc.sendChannelSync 'ATOM_INTERNAL_FUNCTION_CALL', plain.id, Array::slice.call(arguments)
 | 
					              ret = ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_CALL', plain.id, member.name, Array::slice.call(arguments)
 | 
				
			||||||
          return generateFromPainObject ret
 | 
					              generateFromPainObject ret
 | 
				
			||||||
 | 
					          else
 | 
				
			||||||
 | 
					            ret.__defineSetter__ member.name, (value) ->
 | 
				
			||||||
 | 
					              # Set member data.
 | 
				
			||||||
 | 
					              ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_SET', plain.id, member.name, value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Polulate delegate members.
 | 
					            ret.__defineGetter__ member.name, ->
 | 
				
			||||||
  for member in plain.members
 | 
					              # Get member data.
 | 
				
			||||||
    do (member) ->
 | 
					              ret = ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_GET', plain.id, member.name
 | 
				
			||||||
      if member.type is 'function'
 | 
					              generateFromPainObject ret
 | 
				
			||||||
        ret[member.name] = ->
 | 
					 | 
				
			||||||
          # Call member function.
 | 
					 | 
				
			||||||
          ret = ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_CALL', plain.id, member.name, Array::slice.call(arguments)
 | 
					 | 
				
			||||||
          generateFromPainObject ret
 | 
					 | 
				
			||||||
      else
 | 
					 | 
				
			||||||
        ret.__defineSetter__ member.name, (value) ->
 | 
					 | 
				
			||||||
          # Set member data.
 | 
					 | 
				
			||||||
          ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_SET', plain.id, member.name, value
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ret.__defineGetter__ member.name, ->
 | 
					      ret
 | 
				
			||||||
          # Get member data.
 | 
					 | 
				
			||||||
          ret = ipc.sendChannelSync 'ATOM_INTERNAL_MEMBER_GET', plain.id, member.name
 | 
					 | 
				
			||||||
          generateFromPainObject ret
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  ret
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.require = (module) ->
 | 
					exports.require = (module) ->
 | 
				
			||||||
  plain = ipc.sendChannelSync 'ATOM_INTERNAL_REQUIRE', module
 | 
					  plain = ipc.sendChannelSync 'ATOM_INTERNAL_REQUIRE', module
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue