Skip to content
This repository has been archived by the owner. It is now read-only.

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

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

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

cxfeng1 opened this issue Nov 21, 2016 · 2 comments
Assignees

Comments

@cxfeng1
Copy link

@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
Copy link
Contributor

@Jinjiang Jinjiang commented Nov 28, 2016

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

@emptywalker
Copy link

@emptywalker emptywalker commented Mar 23, 2017

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

@Hanks10100 Hanks10100 mentioned this issue Jul 4, 2017
3 of 8 tasks complete
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.