Skip to content

Commit 63fba06

Browse files
committed
refactor(weex): move module and component apis to weex runtime
Modules and components should be registered in weex runtime, not the specific framework. The `registerModules`, `registerComponents`, `weex.supports` and `weex.requireModule` api is moved to weex runtime, which is in the "apache/incubator-weex" repo.
1 parent 10fdb57 commit 63fba06

File tree

1 file changed

+10
-141
lines changed

1 file changed

+10
-141
lines changed

src/platforms/weex/entry-framework.js

Lines changed: 10 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ import TextNode from 'weex/runtime/text-node'
44
const VueFactory = require('./factory')
55

66
const instances = {}
7-
const modules = {}
8-
const components = {}
97

108
const renderer = {
11-
TextNode,
12-
instances,
13-
modules,
14-
components
9+
TextNode
1510
}
1611

1712
/**
@@ -29,8 +24,6 @@ export function init (cfg) {
2924
*/
3025
export function reset () {
3126
clear(instances)
32-
clear(modules)
33-
clear(components)
3427
delete renderer.Document
3528
delete renderer.Element
3629
delete renderer.Comment
@@ -61,43 +54,31 @@ export function createInstance (
6154
data,
6255
env = {}
6356
) {
64-
// Virtual-DOM object.
65-
const document = new renderer.Document(instanceId, config.bundleUrl)
66-
57+
const weex = env.weex
58+
const document = weex.document
6759
const instance = instances[instanceId] = {
6860
instanceId, config, data,
6961
document
7062
}
7163

72-
// Prepare native module getter and HTML5 Timer APIs.
73-
const moduleGetter = genModuleGetter(instanceId)
74-
const timerAPIs = getInstanceTimer(instanceId, moduleGetter)
75-
76-
// Prepare `weex` instance variable.
77-
const weexInstanceVar = {
78-
config,
79-
document,
80-
supports,
81-
requireModule: moduleGetter
82-
}
83-
Object.freeze(weexInstanceVar)
64+
const timerAPIs = getInstanceTimer(instanceId, weex.requireModule)
8465

8566
// Each instance has a independent `Vue` module instance
86-
const Vue = instance.Vue = createVueModuleInstance(instanceId, moduleGetter)
67+
const Vue = instance.Vue = createVueModuleInstance(instanceId, weex)
8768

8869
// The function which create a closure the JS Bundle will run in.
8970
// It will declare some instance variables like `Vue`, HTML5 Timer APIs etc.
9071
const instanceVars = Object.assign({
9172
Vue,
92-
weex: weexInstanceVar
73+
weex
9374
}, timerAPIs, env.services)
9475

9576
appCode = `(function(global){ \n${appCode}\n })(Object.create(this))`
9677

9778
callFunction(instanceVars, appCode)
9879

9980
// Send `createFinish` signal to native.
100-
instance.document.taskCenter.send('dom', { action: 'createFinish' }, [])
81+
document.taskCenter.send('dom', { action: 'createFinish' }, [])
10182

10283
return instance
10384
}
@@ -194,89 +175,10 @@ export function receiveTasks (id, tasks) {
194175
return new Error(`invalid instance id "${id}" or tasks`)
195176
}
196177

197-
/**
198-
* Register native modules information.
199-
* @param {object} newModules
200-
*/
201-
export function registerModules (newModules) {
202-
for (const name in newModules) {
203-
if (!modules[name]) {
204-
modules[name] = {}
205-
}
206-
newModules[name].forEach(method => {
207-
if (typeof method === 'string') {
208-
modules[name][method] = true
209-
} else {
210-
modules[name][method.name] = method.args
211-
}
212-
})
213-
}
214-
}
215-
216-
/**
217-
* Check whether the module or the method has been registered.
218-
* @param {String} module name
219-
* @param {String} method name (optional)
220-
*/
221-
export function isRegisteredModule (name, method) {
222-
if (typeof method === 'string') {
223-
return !!(modules[name] && modules[name][method])
224-
}
225-
return !!modules[name]
226-
}
227-
228-
/**
229-
* Register native components information.
230-
* @param {array} newComponents
231-
*/
232-
export function registerComponents (newComponents) {
233-
if (Array.isArray(newComponents)) {
234-
newComponents.forEach(component => {
235-
if (!component) {
236-
return
237-
}
238-
if (typeof component === 'string') {
239-
components[component] = true
240-
} else if (typeof component === 'object' && typeof component.type === 'string') {
241-
components[component.type] = component
242-
}
243-
})
244-
}
245-
}
246-
247-
/**
248-
* Check whether the component has been registered.
249-
* @param {String} component name
250-
*/
251-
export function isRegisteredComponent (name) {
252-
return !!components[name]
253-
}
254-
255-
/**
256-
* Detects whether Weex supports specific features.
257-
* @param {String} condition
258-
*/
259-
export function supports (condition) {
260-
if (typeof condition !== 'string') return null
261-
262-
const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i)
263-
if (res) {
264-
const type = res[1]
265-
const name = res[2]
266-
const method = res[4]
267-
switch (type) {
268-
case 'module': return isRegisteredModule(name, method)
269-
case 'component': return isRegisteredComponent(name)
270-
}
271-
}
272-
273-
return null
274-
}
275-
276178
/**
277179
* Create a fresh instance of Vue for each Weex instance.
278180
*/
279-
function createVueModuleInstance (instanceId, moduleGetter) {
181+
function createVueModuleInstance (instanceId, weex) {
280182
const exports = {}
281183
VueFactory(exports, renderer)
282184
const Vue = exports.Vue
@@ -289,7 +191,7 @@ function createVueModuleInstance (instanceId, moduleGetter) {
289191
const isReservedTag = Vue.config.isReservedTag || (() => false)
290192
const isRuntimeComponent = Vue.config.isRuntimeComponent || (() => false)
291193
Vue.config.isReservedTag = name => {
292-
return (!isRuntimeComponent(name) && components[name]) ||
194+
return (!isRuntimeComponent(name) && weex.supports(`@component/${name}`)) ||
293195
isReservedTag(name) ||
294196
weexRegex.test(name)
295197
}
@@ -301,7 +203,7 @@ function createVueModuleInstance (instanceId, moduleGetter) {
301203

302204
// expose weex native module getter on subVue prototype so that
303205
// vdom runtime modules can access native modules via vnode.context
304-
Vue.prototype.$requireWeexModule = moduleGetter
206+
Vue.prototype.$requireWeexModule = weex.requireModule
305207

306208
// Hack `Vue` behavior to handle instance information and data
307209
// before root component created.
@@ -334,39 +236,6 @@ function createVueModuleInstance (instanceId, moduleGetter) {
334236
return Vue
335237
}
336238

337-
/**
338-
* Generate native module getter. Each native module has several
339-
* methods to call. And all the behaviors is instance-related. So
340-
* this getter will return a set of methods which additionally
341-
* send current instance id to native when called.
342-
* @param {string} instanceId
343-
* @return {function}
344-
*/
345-
function genModuleGetter (instanceId) {
346-
const instance = instances[instanceId]
347-
return function (name) {
348-
const nativeModule = modules[name] || []
349-
const output = {}
350-
for (const methodName in nativeModule) {
351-
Object.defineProperty(output, methodName, {
352-
enumerable: true,
353-
configurable: true,
354-
get: function proxyGetter () {
355-
return (...args) => {
356-
return instance.document.taskCenter.send('module', { module: name, method: methodName }, args)
357-
}
358-
},
359-
set: function proxySetter (val) {
360-
if (typeof val === 'function') {
361-
return instance.document.taskCenter.send('module', { module: name, method: methodName }, [val])
362-
}
363-
}
364-
})
365-
}
366-
return output
367-
}
368-
}
369-
370239
/**
371240
* Generate HTML5 Timer APIs. An important point is that the callback
372241
* will be converted into callback id when sent to native. So the

0 commit comments

Comments
 (0)