From 461dc8405b5da6fb71db9ade7f1407bdac18217b Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Sat, 29 Oct 2016 02:18:19 +0800 Subject: [PATCH] code clean --- src/puppet-web/browser.ts | 21 +++++---------------- src/puppet-web/event.ts | 1 - src/puppet-web/puppet-web.ts | 22 ++++------------------ 3 files changed, 9 insertions(+), 35 deletions(-) diff --git a/src/puppet-web/browser.ts b/src/puppet-web/browser.ts index ff94b1b6..c75a34b2 100644 --- a/src/puppet-web/browser.ts +++ b/src/puppet-web/browser.ts @@ -85,6 +85,7 @@ export class Browser extends EventEmitter { } catch (e) { log.error('PuppetWebBrowser', 'init() exception: %s', e.message) + this.state.current('close') await this.quit() throw e @@ -99,7 +100,6 @@ export class Browser extends EventEmitter { await this.driver.get(url) } catch (e) { log.error('PuppetWebBrowser', 'open() exception: %s', e.message) - this.dead(e.message) throw e } } @@ -117,7 +117,6 @@ export class Browser extends EventEmitter { await this.quit() - // if (this.currentState() === 'opening') { if (this.state.current() === 'open' && this.state.inprocess()) { log.warn('PuppetWebBrowser', 'restart() found state.current() === open and inprocess()') return @@ -129,14 +128,12 @@ export class Browser extends EventEmitter { public async quit(): Promise { log.verbose('PuppetWebBrowser', 'quit()') - // if (this.currentState() === 'closing') { if (this.state.current() === 'close' && this.state.inprocess()) { const e = new Error('quit() be called when state.current() is close with inprocess()?') log.warn('PuppetWebBrowser', e.message) throw e } - // this.currentState('closing') this.state.current('close', false) try { @@ -327,7 +324,6 @@ export class Browser extends EventEmitter { if (forceReason) { dead = true msg = forceReason - // } else if (this.targetState() !== 'open') { } else if (this.state.target() !== 'open') { dead = true msg = 'state.target() not open' @@ -343,15 +339,7 @@ export class Browser extends EventEmitter { : '' , msg ) - // no need to quit here. dead event listener will do this async job, and do it better than here - // this.quit() - - // if (this.targetState() !== 'open' || this.currentState() === 'opening') { - // log.warn('PuppetWebBrowser', 'dead() wil not emit `dead` event because currentState is `opening` or targetState !== open') - // } else { - // log.verbose('PuppetWebBrowser', 'dead() emit a `dead` event because %s', msg) - // this.emit('dead', msg) - // } + if ( this.state.target() === 'open' && this.state.current() === 'open' && this.state.stable() @@ -359,10 +347,11 @@ export class Browser extends EventEmitter { log.verbose('PuppetWebBrowser', 'dead() emit a `dead` event because %s', msg) this.emit('dead', msg) } else { - log.warn('PuppetWebBrowser', 'dead() wil not emit `dead` event because %s' - , 'state is not `stable open`' + log.warn('PuppetWebBrowser', 'dead() wil not emit `dead` event because states are: target(%s), current(%s), stable(%s)' + , this.state.target(), this.state.current(), this.state.stable() ) } + } return dead } diff --git a/src/puppet-web/event.ts b/src/puppet-web/event.ts index e8c813c2..85126ba0 100644 --- a/src/puppet-web/event.ts +++ b/src/puppet-web/event.ts @@ -82,7 +82,6 @@ async function onBrowserDead(this: PuppetWeb, e): Promise { await this.browser.quit() log.verbose('PuppetWebEvent', 'onBrowserDead() browser quit-ed') - // if (this.browser.targetState() !== 'open') { if (browser.state.target() !== 'open') { log.warn('PuppetWebEvent', 'onBrowserDead() will not init browser because browser.state.target(%s) !== open' , browser.state.target() diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index d873479c..388b4ddb 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -105,28 +105,24 @@ export class PuppetWeb extends Puppet { public async quit(): Promise { log.verbose('PuppetWeb', 'quit()') - // this.targetState('dead') // XXX should we set `target` to `dead` in `quit()`? - this.state.target('dead') + // this.state.target('dead') - // if (this.currentState() === 'killing') { if (this.state.current() === 'dead' && this.state.inprocess()) { // log.warn('PuppetWeb', 'quit() is called but readyState is `disconnecting`?') log.warn('PuppetWeb', 'quit() is called but state.current()) is `dead` and inprocess()?') throw new Error('do not call quit again when quiting') } - // POISON must feed before readyState set to "disconnecting" + // POISON must feed before state set to `dead` & `inprocess` this.emit('watchdog', { data: 'PuppetWeb.quit()', type: 'POISON' }) - // this.currentState('killing') this.state.current('dead', false) - // return co.call(this, function* () { try { if (this.bridge) { // TODO use StateMonitor @@ -153,25 +149,15 @@ export class PuppetWeb extends Puppet { // this.browser = null } else { log.warn('PuppetWeb', 'quit() without a browser') } - // @deprecated 20161004 - // log.verbose('PuppetWeb', 'quit() server.quit() this.initAttach(null)') - // await this.initAttach(null) - - // this.currentState('dead') this.state.current('dead') - // }).catch(e => { // Reject } catch (e) { log.error('PuppetWeb', 'quit() exception: %s', e.message) - // this.currentState('dead') this.state.current('dead') throw e } - // .then(() => { // Finally, Fail Safe - log.verbose('PuppetWeb', 'quit() done') - // this.currentState('dead') - this.state.current('dead') - // }) + log.verbose('PuppetWeb', 'quit() done') + this.state.current('dead') } public async initBrowser(): Promise { -- GitLab