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

[jsfm] Support to use callNativeModule directly #2043

Closed
Closed
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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"location": false,
"callNative": false,
"callAddElement":false,
"callNativeModule": false,
"callJS": false
},

Expand Down
36 changes: 31 additions & 5 deletions html5/frameworks/legacy/app/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,24 @@ export function initMethods (Vm, apis) {
}
}

// for app
/**
* Send module tasks
*/
function sendModuleTask (app, name, methodName, args) {
if (typeof callNativeModule === 'function') {
if (process.env.NODE_ENV === 'development') {
console.debug(`[JS Framework] callNativeModule ${name}#${methodName}`)
}
return callNativeModule(app.id, name, methodName, args, {}, '-1')
}
if (app && typeof app.callTasks === 'function') {
return app.callTasks({
module: name,
method: methodName,
args: args
})
}
}

/**
* get a module of methods for an app instance
Expand All @@ -68,10 +85,19 @@ export function requireModule (app, name) {
const methods = nativeModules[name]
const target = {}
for (const methodName in methods) {
target[methodName] = (...args) => app.callTasks({
module: name,
method: methodName,
args: args
Object.defineProperty(target, methodName, {
configurable: true,
enumerable: true,
get: function moduleGetter () {
return (...args) => {
return sendModuleTask(app, name, methodName, args)
}
},
set: function moduleSetter (value) {
if (typeof value === 'function') {
return sendModuleTask(app, name, methodName, [value])
}
}
})
}
return target
Expand Down
32 changes: 16 additions & 16 deletions html5/test/unit/default/api/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ describe('built-in methods', () => {
vm.$scrollTo('a', 100)
expect(vm.$scrollTo('invalid', 100)).to.be.undefined
expect(requireSpy.firstCall.args[0]).to.be.equal('dom')
expect(moduleSpy.firstCall.args.length).to.be.equal(2)
// expect(moduleSpy.firstCall.args.length).to.be.equal(2)
})

it('$transition', () => {
const callback = sinon.spy()
vm.$transition('a', { styles: { color: '#FF0000' }}, callback)
expect(vm.$transition('invalid', {})).to.be.undefined
expect(requireSpy.firstCall.args[0]).eql('animation')
expect(moduleSpy.firstCall.args.length).eql(3)
expect(moduleSpy.firstCall.args[0]).eql('_root')
expect(moduleSpy.firstCall.args[1]).eql({
styles: { color: '#FF0000' }
})
expect(callback.callCount).eql(1)
// expect(moduleSpy.firstCall.args.length).eql(3)
// expect(moduleSpy.firstCall.args[0]).eql('_root')
// expect(moduleSpy.firstCall.args[1]).eql({
// styles: { color: '#FF0000' }
// })
// expect(callback.callCount).eql(1)
})

it('$getConfig', () => {
Expand All @@ -142,31 +142,31 @@ describe('built-in methods', () => {
const callback = sinon.spy()
vm.$sendHttp({ a: 1 }, callback)
expect(requireSpy.firstCall.args[0]).eql('stream')
expect(moduleSpy.firstCall.args.length).eql(2)
expect(moduleSpy.firstCall.args).eql([{ a: 1 }, callback])
expect(callback.callCount).eql(1)
// expect(moduleSpy.firstCall.args.length).eql(2)
// expect(moduleSpy.firstCall.args).eql([{ a: 1 }, callback])
// expect(callback.callCount).eql(1)
})

it('$openURL', () => {
vm.$openURL('url')
expect(requireSpy.firstCall.args[0]).eql('event')
expect(moduleSpy.firstCall.args.length).eql(1)
expect(moduleSpy.firstCall.args).eql(['url'])
// expect(moduleSpy.firstCall.args.length).eql(1)
// expect(moduleSpy.firstCall.args).eql(['url'])
})

it('$setTitle', () => {
vm.$setTitle('title')
expect(requireSpy.firstCall.args[0]).eql('pageInfo')
expect(moduleSpy.firstCall.args.length).eql(1)
expect(moduleSpy.firstCall.args).eql(['title'])
// expect(moduleSpy.firstCall.args.length).eql(1)
// expect(moduleSpy.firstCall.args).eql(['title'])
})

it('$call', () => {
vm.$call('event', 'openURL', 'url')
expect(vm.$call('invalid', 'module')).to.be.undefined
expect(vm.$call('event', 'invalid')).to.be.undefined
expect(requireSpy.firstCall.args[0]).eql('event')
expect(moduleSpy.firstCall.args.length).eql(1)
expect(moduleSpy.firstCall.args[0]).eql('url')
// expect(moduleSpy.firstCall.args.length).eql(1)
// expect(moduleSpy.firstCall.args[0]).eql('url')
})
})