提交 1f3f2221 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

Add Async Support between Bridge & WechatyBro, so we can get Room UserName...

Add Async Support between Bridge & WechatyBro, so we can get Room UserName from Create(it return a promise in browser)
上级 c589150e
......@@ -201,7 +201,7 @@ class Bridge {
throw new Error('no valid contactIdList')
}
return this.proxyWechaty('roomCreate', contactIdList)
return this.proxyWechaty('roomCreateAsync', contactIdList)
.catch(e => {
log.error('PuppetWebBridge', 'roomCreate(%s) exception: %s', contactIdList, e.message)
throw e
......@@ -299,16 +299,37 @@ class Bridge {
// see: http://blog.sqrtthree.com/2015/08/29/utf8-to-b64/
const argsDecoded = `JSON.parse(decodeURIComponent(window.atob('${argsEncoded}')))`
const wechatyScript = `return WechatyBro.${wechatyFunc}.apply(undefined, ${argsDecoded})`
// log.silly('PuppetWebBridge', 'proxyWechaty(%s, ...args) %s', wechatyFunc, wechatyScript)
const wechatyScript = `
const callback = arguments[arguments.length - 1]
const isAsync = (typeof callback === 'function')
return WechatyBro
.${wechatyFunc}
.apply(undefined
, isAsync
? ${argsDecoded}.concat(callback)
: ${argsDecoded}
)
`.replace(/[\n\s]+/, ' ')
log.silly('PuppetWebBridge', 'proxyWechaty(%s, ...args) %s', wechatyFunc, wechatyScript)
// console.log('proxyWechaty wechatyFunc args[0]: ')
// console.log(args[0])
/**
*
* WechatyBro method named end with "Async", will be treated as a Async function
*/
let funcExecuter
if (/Async$/.test(wechatyFunc)) {
funcExecuter = this.executeAsync.bind(this)
} else {
funcExecuter = this.execute.bind(this)
}
return this.execute('return typeof WechatyBro === "undefined"')
.then(noWechaty => {
if (noWechaty) {
throw new Error('there is no WechatyBro in browser(yet)')
}
})
.then(() => this.execute(wechatyScript))
.then(() => funcExecuter(wechatyScript))
.catch(e => {
log.warn('PuppetWebBridge', 'proxyWechaty() exception: %s', e.message)
throw e
......@@ -329,6 +350,17 @@ class Bridge {
})
}
executeAsync(script, ...args) {
if (!this.puppet || !this.puppet.browser) {
return Promise.reject(new Error('execute(): no puppet or no puppet.browser in bridge'))
}
return this.puppet.browser.executeAsync(script, ...args)
.catch(e => {
log.warn('PuppetWebBridge', 'executeAsync() exception: %s', e.message)
throw e
})
}
ding(data) {
return this.proxyWechaty('ding', data)
.catch(e => {
......
......@@ -144,6 +144,10 @@ class Browser extends EventEmitter {
throw new Error('unsupported head: ' + this.head)
}
this.driver.manage()
.timeouts()
.setScriptTimeout(10000)
// XXX: if no `setTimeout()` here, promise will hang forever!
// with a confirmed bug in selenium-webdriver v2.53.2:
// https://github.com/SeleniumHQ/selenium/issues/2233
......@@ -411,7 +415,7 @@ this.onResourceRequested = function(request, net) {
}
execute(script, ...args) {
log.silly('PuppetWebBrowser', `Browser.execute(${script.slice(0, 80)})`)
log.silly('PuppetWebBrowser', 'Browser.execute(%s)', script.slice(0, 80))
// log.verbose('PuppetWebBrowser', `Browser.execute() driver.getSession: %s`, util.inspect(this.driver.getSession()))
if (this.dead()) { return Promise.reject(new Error('browser dead')) }
......@@ -428,6 +432,18 @@ this.onResourceRequested = function(request, net) {
})
}
executeAsync(script, ...args) {
log.silly('PuppetWebBrowser', 'Browser.executeAsync(%s)', script.slice(0, 80))
if (this.dead()) { return Promise.reject(new Error('browser dead')) }
// console.log(script)
return this.driver.executeAsyncScript.apply(this.driver, arguments)
.catch(e => {
// this.dead(e)
log.warn('PuppetWebBrowser', 'executeAsync() exception: %s', e.message.slice(0,99))
throw e
})
}
/**
*
* check whether browser is full functional
......
......@@ -399,7 +399,8 @@ class PuppetWeb extends Puppet {
roomTopic(room, topic) {
if (!this.bridge) {
return Promise.reject(new Error('fail: no bridge(yet)!'))
} else if (!room || typeof topic === 'undefined') {
}
if (!room || typeof topic === 'undefined') {
return Promise.reject(new Error('room or topic not found'))
}
......@@ -422,9 +423,11 @@ class PuppetWeb extends Puppet {
const contactIdList = contactList.map(c => c.id)
// console.log('puppet roomCreate: ')
// console.log(contactIdList)
return this.bridge.roomCreate(contactIdList, topic)
.catch(e => {
log.warn('PuppetWeb', 'roomCreate(%s, %s) rejected: %s', contactIdList.join(,), topic, e.message)
log.warn('PuppetWeb', 'roomCreate(%s, %s) rejected: %s', contactIdList.join(','), topic, e.message)
throw e
})
}
......
......@@ -499,7 +499,13 @@
return chatroomFactory.modTopic(ChatRoomName, topic)
}
function roomCreate(UserNameList, topic) {
function roomCreateAsync(UserNameList, topic) {
const callback = arguments[arguments.length - 1]
if (typeof callback !== 'function') {
// here we should in sync mode, because there's no callback
throw new Error('async method need to be called via webdriver.executeAsyncScript')
}
const UserNameListArg = UserNameList.map(n => { return { UserName: n } })
const chatroomFactory = WechatyBro.glue.chatroomFactory
......@@ -512,14 +518,19 @@
if (topic) {
roomModTopic(r.ChatRoomName, topic)
}
callback(r.ChatRoomName, topic)
} else {
throw new Error('chatroomFactory.create() error with Ret: '
+ r && r.BaseResponse.Ret
+ 'with ErrMsg: '
+ r && r.BaseResponse.ErrMsg
)
}
return r.ChatRoomName
})
.catch(e => {
// TBD
log(e)
// Async can only return by call callback
callback(e)
})
return 'no callback (yet)'
}
function verifyUserRequest(UserName, VerifyContent = '') {
......@@ -631,8 +642,8 @@
, contactFind
// for Wechaty Room Class
, roomCreateAsync
, roomFind
, roomCreate
, roomAddMember
, roomDelMember
, roomModTopic
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册