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

add targetState() and currentState() to puppet-web compoments

上级 8d90a5ec
......@@ -32,16 +32,52 @@ class Browser extends EventEmitter {
this.head = head
this.sessionFile = sessionFile // a file to save session cookies
this.live = false
// this.live = false
this.targetState('close')
this.currentState('close')
}
// targetState : 'open' | 'close'
targetState(newState) {
if (newState) {
log.verbose('Browser', 'targetState(%s)', newState)
this._targetState = newState
}
return this._targetState
}
// currentState : 'opening' | 'open' | 'closing' | 'close'
currentState(newState) {
if (newState) {
log.verbose('Browser', 'currentState(%s)', newState)
this._currentState = newState
}
return this._currentState
}
toString() { return `Browser({head:${this.head})` }
init() {
return this.initDriver()
.then(() => {
this.live = true
this.targetState('open')
this.currentState('opening')
// fastUrl is used to open in browser for we can set cookies.
// backup: 'https://res.wx.qq.com/zh_CN/htmledition/v2/images/icon/ico_loading28a2f7.gif'
const fastUrl = 'https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg'
return co.call(this, function* () {
yield this.initDriver()
// this.live = true
yield this.open(fastUrl)
yield this.loadSession()
.catch(e => { // fail safe
log.verbose('PuppetWeb', 'browser.loadSession(%s) exception: %s', this.profile, e.message || e)
})
yield this.open()
this.currentState('open')
return this
})
.catch(e => {
......@@ -50,6 +86,12 @@ class Browser extends EventEmitter {
// with selenium-webdriver v2.53.2
// XXX: https://github.com/SeleniumHQ/selenium/issues/2233
log.error('PuppetWebBrowser', 'init() exception: %s', e.message)
this.currentState('closing')
this.quit().then(_ => {
this.currentState('close')
})
throw e
})
}
......@@ -164,7 +206,7 @@ class Browser extends EventEmitter {
.build()
/**
* ISSUE #21 - https://github.com/zixia/wechaty/issues/21
* FIXME: ISSUE #21 - https://github.com/zixia/wechaty/issues/21
*
* http://phantomjs.org/api/webpage/handler/on-resource-requested.html
* http://stackoverflow.com/a/29544970/1123955
......@@ -190,13 +232,20 @@ this.onResourceRequested = function(request, net) {
quit() {
log.verbose('PuppetWebBrowser', 'quit()')
this.live = false
this.targetState('close')
this.currentState('closing')
// this.live = false
if (!this.driver) {
log.verbose('PuppetWebBrowser', 'driver.quit() skipped because no driver')
this.currentState('close')
return Promise.resolve('no driver')
} else if (!this.driver.getSession()) {
this.driver = null
log.verbose('PuppetWebBrowser', 'driver.quit() skipped because no driver session')
this.currentState('close')
return Promise.resolve('no driver session')
}
......@@ -217,6 +266,7 @@ this.onResourceRequested = function(request, net) {
*/
yield this.clean()
this.currentState('close')
log.silly('PuppetWebBrowser', 'quit() co() end')
}).catch(e => {
// console.log(e)
......@@ -232,6 +282,9 @@ this.onResourceRequested = function(request, net) {
if (crashRegex.test(e.message)) { log.warn('PuppetWebBrowser', 'driver.quit() browser crashed') }
else { log.warn('PuppetWebBrowser', 'driver.quit() exception: %s', e.message) }
// XXX fail safe to `close` ?
this.currentState('close')
})
}
......@@ -399,9 +452,11 @@ this.onResourceRequested = function(request, net) {
if (forceReason) {
dead = true
errMsg = forceReason
} else if (!this.live) {
// } else if (!this.live) {
} else if (this.targetState() !== 'open') {
dead = true
errMsg = 'browser not live'
// errMsg = 'browser not live'
errMsg = 'targetState not open'
} else if (!this.driver || !this.driver.getSession()) {
dead = true
errMsg = 'no driver or session'
......@@ -409,7 +464,10 @@ this.onResourceRequested = function(request, net) {
if (dead) {
log.warn('PuppetWebBrowser', 'dead() because %s', errMsg)
this.live = false
// this.live = false
this.currentState('closing')
this.quit().then(_ => this.currentState('close'))
// must use nextTick here, or promise will hang... 2016/6/10
process.nextTick(_ => {
log.verbose('PuppetWebBrowser', 'dead() emit a `dead` event because %s', errMsg)
......
......@@ -157,8 +157,12 @@ function onServerDisconnect(data) {
this.user = null
}
if (this.readyState() === 'disconnecting') {
log.verbose('PuppetWebEvent', 'onServerDisconnect() be called when readyState is `disconnecting`')
// if (this.readyState() === 'disconnecting') {
// log.verbose('PuppetWebEvent', 'onServerDisconnect() be called when readyState is `disconnecting`')
// return
// }
if (this.currentState() === 'killing') {
log.verbose('PuppetWebEvent', 'onServerDisconnect() be called when currentState is `killing`')
return
}
......@@ -217,8 +221,12 @@ function onServerUnload(data) {
log.warn('PuppetWebEvent', 'onServerUnload(%s)', data)
// onServerLogout.call(this, data) // XXX: should emit event[logout] from browser
if (this.readyState() === 'disconnecting') {
log.verbose('PuppetWebEvent', 'onServerUnload() will return because readyState is `disconnecting`')
// if (this.readyState() === 'disconnecting') {
// log.verbose('PuppetWebEvent', 'onServerUnload() will return because readyState is `disconnecting`')
// return
// }
if (this.currentState() === 'killing') {
log.verbose('PuppetWebEvent', 'onServerUnload() will return because currentState is `killing`')
return
}
......
......@@ -56,7 +56,9 @@ class PuppetWeb extends Puppet {
init() {
log.verbose('PuppetWeb', `init() with head:${this.head}, profile:${this.profile}`)
this.readyState('connecting')
// this.readyState('connecting')
this.targetState('live')
this.currentState('birthing')
return co.call(this, function* () {
......@@ -66,13 +68,13 @@ class PuppetWeb extends Puppet {
yield this.initAttach(this)
log.verbose('PuppetWeb', 'initAttach() done')
this.server = yield this.initServer()
yield this.initServer()
log.verbose('PuppetWeb', 'initServer() done')
this.browser = yield this.initBrowser()
yield this.initBrowser()
log.verbose('PuppetWeb', 'initBrowser() done')
this.bridge = yield this.initBridge()
yield this.initBridge()
log.verbose('PuppetWeb', 'initBridge() done')
this.emit('watchdog', { data: 'inited' })
......@@ -84,16 +86,20 @@ class PuppetWeb extends Puppet {
})
.then(() => { // Finally
log.verbose('PuppetWeb', 'init() done')
this.readyState('connected')
// this.readyState('connected')
this.currentState('live')
return this // for Chaining
})
}
quit() {
log.verbose('PuppetWeb', 'quit()')
this.targetState('dead')
if (this.readyState() === 'disconnecting') {
log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
// if (this.readyState() === 'disconnecting') {
if (this.currentState() === 'killing') {
// log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?')
log.warn('PuppetWeb', 'quit() is called but currentState is `killing`?')
throw new Error('do not call quit again when quiting')
}
......@@ -103,7 +109,8 @@ class PuppetWeb extends Puppet {
type: 'POISON'
})
this.readyState('disconnecting')
// this.readyState('disconnecting')
this.currentState('killing')
return co.call(this, function* () {
......@@ -133,14 +140,18 @@ class PuppetWeb extends Puppet {
log.verbose('PuppetWeb', 'quit() server.quit() this.initAttach(null)')
yield this.initAttach(null)
this.currentState('dead')
})
.catch(e => { // Reject
log.error('PuppetWeb', 'quit() exception: %s', e.message)
this.currentState('dead')
throw e
})
.then(() => { // Finally, Fail Safe
log.verbose('PuppetWeb', 'quit() done')
this.readyState('disconnected')
// this.readyState('disconnected')
this.currentState('dead')
return this // for Chaining
})
}
......@@ -162,18 +173,10 @@ class PuppetWeb extends Puppet {
browser.on('dead', Event.onBrowserDead.bind(this))
// fastUrl is used to open in browser for we can set cookies.
// backup: 'https://res.wx.qq.com/zh_CN/htmledition/v2/images/icon/ico_loading28a2f7.gif'
const fastUrl = 'https://wx.qq.com/zh_CN/htmledition/v2/images/webwxgeticon.jpg'
this.browser = browser
return co.call(this, function* () {
yield browser.init()
yield browser.open(fastUrl)
yield browser.loadSession()
.catch(e => { // fail safe
log.verbose('PuppetWeb', 'browser.loadSession(%s) exception: %s', this.profile, e.message || e)
})
yield browser.open()
return browser // follow func name meaning
}).catch(e => {
log.error('PuppetWeb', 'initBrowser() exception: %s', e.message)
......@@ -188,6 +191,8 @@ class PuppetWeb extends Puppet {
, port: this.port
})
this.bridge = bridge
return bridge.init()
.catch(e => {
if (this.browser.dead()) {
......@@ -220,6 +225,8 @@ class PuppetWeb extends Puppet {
server.on('log' , Event.onServerLog.bind(this))
server.on('ding' , Event.onServerDing.bind(this))
this.server = server
return server.init()
.catch(e => {
log.error('PuppetWeb', 'initServer() exception: %s', e.message)
......@@ -316,12 +323,4 @@ class PuppetWeb extends Puppet {
}
}
// Object.assign(PuppetWeb, {
// default: PuppetWeb
// , PuppetWeb
// , Server
// , Browser
// , Bridge
// })
module.exports = PuppetWeb.default = PuppetWeb.PuppetWeb = PuppetWeb
......@@ -43,12 +43,17 @@ function onFeed({
const feed = `${type}:[${data}]`
log.verbose('PuppetWebWatchdog', 'onFeed: %d, %s', timeout, feed)
if (this.readyState() === 'disconnecting'
// || this.readyState() === 'disconnected'
if (this.currentState() === 'killing'
) {
log.warn('PuppetWebWatchdog', 'onFeed() is disabled because readyState is `disconnecting`')
log.warn('PuppetWebWatchdog', 'onFeed() is disabled because currentState is `killing`')
return
}
// if (this.readyState() === 'disconnecting'
// // || this.readyState() === 'disconnected'
// ) {
// log.warn('PuppetWebWatchdog', 'onFeed() is disabled because readyState is `disconnecting`')
// return
// }
setWatchDogTimer.call(this, timeout, feed)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册