Merge pull request #2391 from seanchas116/support-remote-promise

Support Promise over remote objects
This commit is contained in:
Cheng Zhao 2015-08-03 15:22:36 +08:00
commit 4ac59e2674
4 changed files with 20 additions and 0 deletions

View file

@ -10,6 +10,7 @@ valueToMeta = (sender, value) ->
meta.type = 'buffer' if Buffer.isBuffer value meta.type = 'buffer' if Buffer.isBuffer value
meta.type = 'value' if value is null meta.type = 'value' if value is null
meta.type = 'array' if Array.isArray value meta.type = 'array' if Array.isArray value
meta.type = 'promise' if value? and value.constructor.name is 'Promise'
# Treat the arguments object as array. # Treat the arguments object as array.
meta.type = 'array' if meta.type is 'object' and value.callee? and value.length? meta.type = 'array' if meta.type is 'object' and value.callee? and value.length?
@ -29,6 +30,8 @@ valueToMeta = (sender, value) ->
meta.members.push {name: prop, type: typeof field} for prop, field of value meta.members.push {name: prop, type: typeof field} for prop, field of value
else if meta.type is 'buffer' else if meta.type is 'buffer'
meta.value = Array::slice.call value, 0 meta.value = Array::slice.call value, 0
else if meta.type is 'promise'
meta.then = valueToMeta(sender, value.then.bind(value))
else else
meta.type = 'value' meta.type = 'value'
meta.value = value meta.value = value
@ -47,6 +50,7 @@ unwrapArgs = (sender, args) ->
when 'remote-object' then objectsRegistry.get meta.id when 'remote-object' then objectsRegistry.get meta.id
when 'array' then unwrapArgs sender, meta.value when 'array' then unwrapArgs sender, meta.value
when 'buffer' then new Buffer(meta.value) when 'buffer' then new Buffer(meta.value)
when 'promise' then Promise.resolve(then: metaToValue(meta.then))
when 'object' when 'object'
ret = v8Util.createObjectWithName meta.name ret = v8Util.createObjectWithName meta.name
for member in meta.members for member in meta.members

View file

@ -20,6 +20,8 @@ wrapArgs = (args, visited=[]) ->
type: 'array', value: wrapArgs(value, visited) type: 'array', value: wrapArgs(value, visited)
else if Buffer.isBuffer value else if Buffer.isBuffer value
type: 'buffer', value: Array::slice.call(value, 0) type: 'buffer', value: Array::slice.call(value, 0)
else if value? and value.constructor.name is 'Promise'
type: 'promise', then: valueToMeta(value.then.bind(value))
else if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId' else if value? and typeof value is 'object' and v8Util.getHiddenValue value, 'atomId'
type: 'remote-object', id: v8Util.getHiddenValue value, 'atomId' type: 'remote-object', id: v8Util.getHiddenValue value, 'atomId'
else if value? and typeof value is 'object' else if value? and typeof value is 'object'
@ -44,6 +46,7 @@ metaToValue = (meta) ->
when 'value' then meta.value when 'value' then meta.value
when 'array' then (metaToValue(el) for el in meta.members) when 'array' then (metaToValue(el) for el in meta.members)
when 'buffer' then new Buffer(meta.value) when 'buffer' then new Buffer(meta.value)
when 'promise' then Promise.resolve(then: metaToValue(meta.then))
when 'error' when 'error'
throw new Error("#{meta.message}\n#{meta.stack}") throw new Error("#{meta.message}\n#{meta.stack}")
else else

View file

@ -52,6 +52,14 @@ describe 'ipc module', ->
print_name = remote.require path.join(fixtures, 'module', 'print_name.js') print_name = remote.require path.join(fixtures, 'module', 'print_name.js')
assert.equal print_name.print(buf), 'Buffer' assert.equal print_name.print(buf), 'Buffer'
describe 'remote promise', ->
it 'can be used as promise in each side', (done) ->
promise = remote.require path.join(fixtures, 'module', 'promise.js')
promise.twicePromise(Promise.resolve(1234))
.then (value) =>
assert.equal value, 2468
done()
describe 'ipc.sender.send', -> describe 'ipc.sender.send', ->
it 'should work when sending an object containing id property', (done) -> it 'should work when sending an object containing id property', (done) ->
obj = id: 1, name: 'ly' obj = id: 1, name: 'ly'

5
spec/fixtures/module/promise.js vendored Normal file
View file

@ -0,0 +1,5 @@
exports.twicePromise = function (promise) {
return promise.then(function (value) {
return value * 2;
});
}