Skip to content

Commit 094d86c

Browse files
committed
Merge branch 'html5-feature-0.10' of https://github.com/alibaba/weex into html5-feature-0.10
2 parents 9b90faf + 42ce3eb commit 094d86c

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
* save the original listener for 'addEventListener'.
4+
* this is the dipaching listener which is waiting for feed.
5+
* map structure: moduleName -> eventName -> [handlers].
6+
*/
7+
const handlerMap = {}
8+
9+
window.addEventListener('moduleevent', event => {
10+
const dt = event.detail || {}
11+
const module = dt.module
12+
const type = dt.type
13+
try {
14+
handlerMap[module][type].forEach(handler => handler(dt))
15+
}
16+
catch (e) {
17+
console.warn(`[h5-render] no such event ${type} for module ${module}.`)
18+
}
19+
})
20+
21+
export function addEventListener (module, evt, callbackId, options) {
22+
const once = options && !!options.once
23+
let moduleMap = handlerMap[module]
24+
if (!moduleMap) {
25+
moduleMap = handlerMap[module] = {}
26+
}
27+
let handlers = moduleMap[evt]
28+
if (!handlers) {
29+
handlers = moduleMap[evt] = []
30+
}
31+
handlers.push((data) => this.sender.performCallback(callbackId, data, !once))
32+
}
33+
34+
export function removeAllEventListeners (module, evt) {
35+
try {
36+
delete handlerMap[module][evt]
37+
}
38+
catch (e) {
39+
// do nothing.
40+
}
41+
}

html5/render/browser/bridge/receiver.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import protocol from './protocol'
55
import { isArray } from '../utils'
66
import Sender from './sender'
77

8+
// sync call native component method.
9+
function callNativeComponent (instanceId, ref, method, args, options) {
10+
return processCall(instanceId, {
11+
component: options.component,
12+
ref,
13+
method,
14+
args
15+
})
16+
}
17+
18+
// sync call native module api.
19+
function callNativeModule (instanceId, module, method, args, options) {
20+
return processCall(instanceId, { module, method, args })
21+
}
22+
823
// callNative: jsFramework will call this method to talk to
924
// this renderer.
1025
// params:
@@ -35,7 +50,7 @@ function callNative (instanceId, tasks, callbackId) {
3550

3651
function processCall (instanceId, call) {
3752
const isComponent = typeof call.module === 'undefined'
38-
isComponent ? componentCall(instanceId, call) : moduleCall(instanceId, call)
53+
const res = isComponent ? componentCall(instanceId, call) : moduleCall(instanceId, call)
3954

4055
const callbackId = call.callbackId
4156
if ((callbackId
@@ -45,6 +60,9 @@ function processCall (instanceId, call) {
4560
&& callbackId !== -1) {
4661
performNextTick(instanceId, callbackId)
4762
}
63+
64+
// for sync call.
65+
return res
4866
}
4967

5068
function moduleCall (instanceId, call) {
@@ -60,7 +78,7 @@ function moduleCall (instanceId, call) {
6078
return
6179
}
6280

63-
method.apply(global.weex.getInstance(instanceId), args)
81+
return method.apply(global.weex.getInstance(instanceId), args)
6482
}
6583

6684
function componentCall (instanceId, call) {
@@ -80,7 +98,7 @@ function componentCall (instanceId, call) {
8098
return console.error(`[h5-render] component ${componentName} doesn't have a method named ${methodName}.`)
8199
}
82100

83-
method.apply(elem, args)
101+
return method.apply(elem, args)
84102
}
85103

86104
function performNextTick (instanceId, callbackId) {
@@ -99,6 +117,8 @@ function nativeLog () {
99117

100118
function exportsBridgeMethodsToGlobal () {
101119
global.callNative = callNative
120+
global.callNativeComponent = callNativeComponent
121+
global.callNativeModule = callNativeModule
102122
global.nativeLog = nativeLog
103123
}
104124

html5/render/browser/render/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const { framework, transformer } = subversion
1515
global.frameworkVersion = framework
1616
global.transformerVersion = transformer
1717

18+
// init bridge.
19+
import { Sender, receiver } from '../bridge'
20+
receiver.init()
21+
1822
// init frameworks
1923
import Listener from '../dom/componentManager'
2024
runtime.config.Document.Listener = Listener
@@ -34,7 +38,6 @@ for (const methodName in globalMethods) {
3438
import config from './config'
3539
import { load } from './loader'
3640
import * as utils from '../utils'
37-
import { Sender, receiver } from '../bridge'
3841
import Component from '../base/component'
3942
import Atomic from '../base/atomic'
4043
import ComponentManager from '../dom/componentManager'
@@ -126,7 +129,6 @@ export default function Weex (options) {
126129
})
127130

128131
this.data = options.data
129-
receiver.init(this)
130132
this.sender = new Sender(this)
131133

132134
_weexInstance[this.instanceId] = this

html5/render/browser/render/register.js

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ import ComponentManager from '../dom/componentManager'
22
import { registerLoader } from './loader'
33
import { protocol } from '../bridge'
44
import { extend } from '../utils'
5+
import { addEventListener, removeAllEventListeners } from '../base/moduleEvent'
6+
7+
/**
8+
* register module event listener for every api module except 'globalEvent'.
9+
*/
10+
function registerModuleEventListener (name, module, meta) {
11+
if (name !== 'globalEvent') {
12+
module['addEventListener'] = function (evt, callbackId, options) {
13+
return addEventListener.call(this, name, evt, callbackId, options)
14+
}
15+
module['removeAllEventListeners'] = function (evt) {
16+
return removeAllEventListeners.call(this, name, evt)
17+
}
18+
; [{
19+
name: 'addEventListener',
20+
args: ['string', 'function', 'object']
21+
}, {
22+
name: 'removeAllEventListeners',
23+
args: ['string']
24+
}].forEach(info => meta[name].push(info))
25+
}
26+
}
527

628
const methods = {
729
// Register a new component with the specified name.
@@ -13,6 +35,7 @@ const methods = {
1335
// If the module already exists, just add methods from the
1436
// new module to the old one.
1537
registerApiModule (name, module, meta) {
38+
registerModuleEventListener(name, module, meta)
1639
if (!protocol.apiModule[name]) {
1740
protocol.apiModule[name] = module
1841
}

0 commit comments

Comments
 (0)