Skip to content
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

feat(weex): implement "weex.supports" api to support feature detection #6053

Merged
merged 2 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/platforms/weex/entry-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function createInstance (
const weexInstanceVar = {
config,
document,
supports,
requireModule: moduleGetter
}
Object.freeze(weexInstanceVar)
Expand Down Expand Up @@ -216,6 +217,18 @@ export function registerModules (newModules) {
}
}

/**
* Check whether the module or the method has been registered.
* @param {String} module name
* @param {String} method name (optional)
*/
export function isRegisteredModule (name, method) {
if (typeof method === 'string') {
return !!(modules[name] && modules[name][method])
}
return !!modules[name]
}

/**
* Register native components information.
* @param {array} newComponents
Expand All @@ -235,6 +248,35 @@ export function registerComponents (newComponents) {
}
}

/**
* Check whether the component has been registered.
* @param {String} component name
*/
export function isRegisteredComponent (name) {
return !!components[name]
}

/**
* Detects whether Weex supports specific features.
* @param {String} condition
*/
export function supports (condition) {
if (typeof condition !== 'string') return null

const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i)
if (res) {
const type = res[1]
const name = res[2]
const method = res[4]
switch (type) {
case 'module': return isRegisteredModule(name, method)
case 'component': return isRegisteredComponent(name)
}
}

return null
}

/**
* Create a fresh instance of Vue for each Weex instance.
*/
Expand Down
60 changes: 60 additions & 0 deletions test/weex/runtime/framework.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ describe('framework APIs', () => {
).toEqual(['b(1)'])
})

it('isRegisteredModule', () => {
framework.registerModules({
foo: ['a', 'b'],
bar: [
{ name: 'x', args: ['string'] },
{ name: 'y', args: ['number'] }
]
})
expect(framework.isRegisteredModule('foo')).toBe(true)
expect(framework.isRegisteredModule('bar')).toBe(true)
expect(framework.isRegisteredModule('foo', 'a')).toBe(true)
expect(framework.isRegisteredModule('foo', 'b')).toBe(true)
expect(framework.isRegisteredModule('bar', 'x')).toBe(true)
expect(framework.isRegisteredModule('bar', 'y')).toBe(true)
expect(framework.isRegisteredModule('FOO')).toBe(false)
expect(framework.isRegisteredModule(' bar ')).toBe(false)
expect(framework.isRegisteredModule('unknown')).toBe(false)
expect(framework.isRegisteredModule('#}{)=}')).toBe(false)
expect(framework.isRegisteredModule('foo', '')).toBe(false)
expect(framework.isRegisteredModule('foo', 'c')).toBe(false)
expect(framework.isRegisteredModule('bar', 'z')).toBe(false)
expect(framework.isRegisteredModule('unknown', 'unknown')).toBe(false)
})

it('registerComponents', () => {
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
const instance = new Instance(runtime)
Expand All @@ -431,6 +455,42 @@ describe('framework APIs', () => {
})
})

it('isRegisteredComponent', () => {
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
expect(framework.isRegisteredComponent('foo')).toBe(true)
expect(framework.isRegisteredComponent('bar')).toBe(true)
expect(framework.isRegisteredComponent('text')).toBe(true)
expect(framework.isRegisteredComponent('FOO')).toBe(false)
expect(framework.isRegisteredComponent(' bar ')).toBe(false)
expect(framework.isRegisteredComponent('<text>')).toBe(false)
expect(framework.isRegisteredComponent('#}{)=}')).toBe(false)
})

it('weex.supports', () => {
framework.registerComponents(['apple', { type: 'banana' }])
framework.registerModules({
cat: ['eat', 'sleep'],
dog: [
{ name: 'bark', args: ['string'] }
]
})
expect(framework.supports('@component/apple')).toBe(true)
expect(framework.supports('@component/banana')).toBe(true)
expect(framework.supports('@module/cat')).toBe(true)
expect(framework.supports('@module/cat.eat')).toBe(true)
expect(framework.supports('@module/cat.sleep')).toBe(true)
expect(framework.supports('@module/dog.bark')).toBe(true)
expect(framework.supports('@component/candy')).toBe(false)
expect(framework.supports('@module/bird')).toBe(false)
expect(framework.supports('@module/bird.sing')).toBe(false)
expect(framework.supports('@module/dog.sleep')).toBe(false)
expect(framework.supports('apple')).toBe(null)
expect(framework.supports('<banana>')).toBe(null)
expect(framework.supports('cat')).toBe(null)
expect(framework.supports('@dog')).toBe(null)
expect(framework.supports('@component/dog#bark')).toBe(null)
})

it('vm.$getConfig', () => {
const instance = new Instance(runtime)
instance.$create(`
Expand Down