New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] support returning value synchronously for module methods #1677

Closed
cxfeng1 opened this Issue Nov 21, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@cxfeng1

cxfeng1 commented Nov 21, 2016

Proposal: Support returning value synchronously for module methods

Background

Now all module methods are asynchronous only and have no return value.

We have to switch all APIs into async mode like this:

var storage = require('@weex-module/storage');
  storage.length(function(e) {
});

As discussed in #968 , it's not simple and clear enough to use and different with web-standard API, which always bring a lot of trouble to us.

I consider that module extending principle should be:

If there is a web API standard already, we should export API according to the standard definition, providing extension API according to the actual situation at the same time.

There are a lot of web APIs that need a return value, such as Geolocation, so the ability to return value synchronously should be necessary.

Introduction

We will add two js-native call functions:

1.callNativeModule

var returnValue = callNativeModule(instanceId, module, method, arguments, options)

instanceId: (string) identify of the weex instance
module: (string) name of the module to be called on.
method: (string) name of the method to be called.
arguments:(array) an array object specifying the arguments with which method should be called.
options:(object) options object used to API extending, default is {}.
returnValue:(all-types) the return value returned by calling module's method.

2.callNativeComponent

callNativeComponent(instanceId,  componentRef,  method,  arguments, options) // Not support return value.

instanceId: (string) identify of the weex instance
componentRef: (string) ref of the component to be called on.
method: (string) name of the method to be called.
arguments:(array) an array object specifying the arguments with which method should be called.
options:(object) options object used to API extending, default is {}.
returnValue:(all-types) the return value returned by calling component's method.

Renderer

  • Renderer should implement the two APIs,dispatch them to original module/component method and export the return value to js.
  • module methods that returning value synchronously should be registered differently from the async ones.

JS Framework

JS's module call:

var storage = require('@weex-module/storage')
var length = storage.length()

JS Framework should dispatch them to callNativeModule call:

var returnValue = callNativeModule(instanceId, 'storage', 'length',[], {});

In a similar way, component method:

var el = this.$el('web')
el.goBack()

should be dispatched to callNativeComponent:

var returnValue = callNativeComponent(instanceId,  componentRef,  'goback',  [],  {});

Devtool

Dev-tool should also implement the two APIs using web socket communication.

Performance

  • Calling callNativeModule once takes about 0.1ms on iPhone 6
  • Other benchmarks will come soon...
@Jinjiang

This comment has been minimized.

Show comment
Hide comment
@Jinjiang

Jinjiang Nov 28, 2016

Contributor

Duplicated to #968 but it's better to come here I think.

Contributor

Jinjiang commented Nov 28, 2016

Duplicated to #968 but it's better to come here I think.

@emptywalker

This comment has been minimized.

Show comment
Hide comment
@emptywalker

emptywalker Mar 23, 2017

想知道这个问题现在解决了吗

emptywalker commented Mar 23, 2017

想知道这个问题现在解决了吗

@Hanks10100 Hanks10100 referenced this issue Jul 4, 2017

Merged

[weex] Sync recent changes of Weex #6028

3 of 8 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment