Skip to content

Commit b1512d8

Browse files
Hanks10100yyx990803
authored andcommitted
feat(weex): implement "weex.supports" api to support feature detection (#6053)
* feat(weex): add "weex.supports" api for feature detection * test(weex): add test case for weex.supports and related methods
1 parent 0d6ad12 commit b1512d8

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/platforms/weex/entry-framework.js

+42
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function createInstance (
7979
const weexInstanceVar = {
8080
config,
8181
document,
82+
supports,
8283
requireModule: moduleGetter
8384
}
8485
Object.freeze(weexInstanceVar)
@@ -216,6 +217,18 @@ export function registerModules (newModules) {
216217
}
217218
}
218219

220+
/**
221+
* Check whether the module or the method has been registered.
222+
* @param {String} module name
223+
* @param {String} method name (optional)
224+
*/
225+
export function isRegisteredModule (name, method) {
226+
if (typeof method === 'string') {
227+
return !!(modules[name] && modules[name][method])
228+
}
229+
return !!modules[name]
230+
}
231+
219232
/**
220233
* Register native components information.
221234
* @param {array} newComponents
@@ -235,6 +248,35 @@ export function registerComponents (newComponents) {
235248
}
236249
}
237250

251+
/**
252+
* Check whether the component has been registered.
253+
* @param {String} component name
254+
*/
255+
export function isRegisteredComponent (name) {
256+
return !!components[name]
257+
}
258+
259+
/**
260+
* Detects whether Weex supports specific features.
261+
* @param {String} condition
262+
*/
263+
export function supports (condition) {
264+
if (typeof condition !== 'string') return null
265+
266+
const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i)
267+
if (res) {
268+
const type = res[1]
269+
const name = res[2]
270+
const method = res[4]
271+
switch (type) {
272+
case 'module': return isRegisteredModule(name, method)
273+
case 'component': return isRegisteredComponent(name)
274+
}
275+
}
276+
277+
return null
278+
}
279+
238280
/**
239281
* Create a fresh instance of Vue for each Weex instance.
240282
*/

test/weex/runtime/framework.spec.js

+60
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,30 @@ describe('framework APIs', () => {
409409
).toEqual(['b(1)'])
410410
})
411411

412+
it('isRegisteredModule', () => {
413+
framework.registerModules({
414+
foo: ['a', 'b'],
415+
bar: [
416+
{ name: 'x', args: ['string'] },
417+
{ name: 'y', args: ['number'] }
418+
]
419+
})
420+
expect(framework.isRegisteredModule('foo')).toBe(true)
421+
expect(framework.isRegisteredModule('bar')).toBe(true)
422+
expect(framework.isRegisteredModule('foo', 'a')).toBe(true)
423+
expect(framework.isRegisteredModule('foo', 'b')).toBe(true)
424+
expect(framework.isRegisteredModule('bar', 'x')).toBe(true)
425+
expect(framework.isRegisteredModule('bar', 'y')).toBe(true)
426+
expect(framework.isRegisteredModule('FOO')).toBe(false)
427+
expect(framework.isRegisteredModule(' bar ')).toBe(false)
428+
expect(framework.isRegisteredModule('unknown')).toBe(false)
429+
expect(framework.isRegisteredModule('#}{)=}')).toBe(false)
430+
expect(framework.isRegisteredModule('foo', '')).toBe(false)
431+
expect(framework.isRegisteredModule('foo', 'c')).toBe(false)
432+
expect(framework.isRegisteredModule('bar', 'z')).toBe(false)
433+
expect(framework.isRegisteredModule('unknown', 'unknown')).toBe(false)
434+
})
435+
412436
it('registerComponents', () => {
413437
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
414438
const instance = new Instance(runtime)
@@ -431,6 +455,42 @@ describe('framework APIs', () => {
431455
})
432456
})
433457

458+
it('isRegisteredComponent', () => {
459+
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
460+
expect(framework.isRegisteredComponent('foo')).toBe(true)
461+
expect(framework.isRegisteredComponent('bar')).toBe(true)
462+
expect(framework.isRegisteredComponent('text')).toBe(true)
463+
expect(framework.isRegisteredComponent('FOO')).toBe(false)
464+
expect(framework.isRegisteredComponent(' bar ')).toBe(false)
465+
expect(framework.isRegisteredComponent('<text>')).toBe(false)
466+
expect(framework.isRegisteredComponent('#}{)=}')).toBe(false)
467+
})
468+
469+
it('weex.supports', () => {
470+
framework.registerComponents(['apple', { type: 'banana' }])
471+
framework.registerModules({
472+
cat: ['eat', 'sleep'],
473+
dog: [
474+
{ name: 'bark', args: ['string'] }
475+
]
476+
})
477+
expect(framework.supports('@component/apple')).toBe(true)
478+
expect(framework.supports('@component/banana')).toBe(true)
479+
expect(framework.supports('@module/cat')).toBe(true)
480+
expect(framework.supports('@module/cat.eat')).toBe(true)
481+
expect(framework.supports('@module/cat.sleep')).toBe(true)
482+
expect(framework.supports('@module/dog.bark')).toBe(true)
483+
expect(framework.supports('@component/candy')).toBe(false)
484+
expect(framework.supports('@module/bird')).toBe(false)
485+
expect(framework.supports('@module/bird.sing')).toBe(false)
486+
expect(framework.supports('@module/dog.sleep')).toBe(false)
487+
expect(framework.supports('apple')).toBe(null)
488+
expect(framework.supports('<banana>')).toBe(null)
489+
expect(framework.supports('cat')).toBe(null)
490+
expect(framework.supports('@dog')).toBe(null)
491+
expect(framework.supports('@component/dog#bark')).toBe(null)
492+
})
493+
434494
it('vm.$getConfig', () => {
435495
const instance = new Instance(runtime)
436496
instance.$create(`

0 commit comments

Comments
 (0)