From f708ea8e979a487d60cdd4b6f85b4ced37a4c580 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Mon, 23 Oct 2017 00:43:07 +0800 Subject: [PATCH] code clean --- src/puppet-web/bridge.ts | 11 ++--------- src/puppet-web/puppet-web.ts | 21 +++++++-------------- src/wechaty.ts | 8 ++++---- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 82aba851..0802ce26 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -114,10 +114,6 @@ export class Bridge extends EventEmitter { await page.goto(url) // Does this related to(?) the CI Error: exception: Navigation Timeout Exceeded: 30000ms exceeded log.verbose('PuppetWebBridge', 'initPage() after page.goto(url)') - // , { - // waitUntil: 'load', // https://github.com/GoogleChrome/puppeteer/issues/805 - // }) - if (cookieList && cookieList.length) { await page.setCookie(...cookieList) log.silly('PuppetWebBridge', 'initPage() page.setCookie() %s cookies set back', cookieList.length) @@ -213,7 +209,6 @@ export class Bridge extends EventEmitter { public async quit(): Promise { log.verbose('PuppetWebBridge', 'quit()') - this.removeAllListeners() try { await this.page.close() log.silly('PuppetWebBridge', 'quit() page.close()-ed') @@ -221,10 +216,8 @@ export class Bridge extends EventEmitter { log.silly('PuppetWebBridge', 'quit() browser.close()-ed') } catch (e) { log.warn('PuppetWebBridge', 'quit() exception: %s', e && e.message || e) - // throw e - /* fail safe */ + this.emit('error', e) } - } public async getUserName(): Promise { @@ -681,7 +674,7 @@ export class Bridge extends EventEmitter { log.silly('PuppetWebBridge', 'clickSwitchAccount() clicked!') return true } else { - log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found') + // log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found') return false } } catch (e) { diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index 5f524763..05a6b728 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -219,16 +219,15 @@ export class PuppetWeb extends Puppet { } public async quit(): Promise { - log.verbose('PuppetWeb', 'quit() state OFF: %s', - this.state.off(), - ) + log.verbose('PuppetWeb', 'quit()') - if (this.state.off() === 'pending') { + const off = this.state.off() + if (off === 'pending') { const e = new Error('quit() is called on a PENDING OFF PuppetWeb') log.warn('PuppetWeb', e.message) this.emit('error', e) return - } else if (this.state.off() === true) { + } else if (off === true) { log.warn('PuppetWeb', 'quit() is called on a OFF puppet. return directly.') return } @@ -239,21 +238,15 @@ export class PuppetWeb extends Puppet { this.state.off('pending') try { - if (this.bridge) { - await this.bridge.quit() - setImmediate(() => this.bridge.removeAllListeners()) - } else { - log.warn('PuppetWeb', 'quit() no bridge!') - } + await this.bridge.quit() + // register the removeListeners micro task at then end of the task queue + setImmediate(() => this.bridge.removeAllListeners()) } catch (e) { log.error('PuppetWeb', 'quit() exception: %s', e.message) Raven.captureException(e) throw e } finally { this.state.off(true) - - // register the removeListeners micro task at then end of the task queue - // setImmediate(() => this.removeAllListeners()) } } diff --git a/src/wechaty.ts b/src/wechaty.ts index 36a1556a..329c87b0 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -477,13 +477,13 @@ export class Wechaty extends EventEmitter implements Sayable { return } - const puppetBeforeDie = this.puppet + const puppet = this.puppet + this.puppet = null config.puppetInstance(null) try { - await puppetBeforeDie.quit() - setImmediate(() => puppetBeforeDie.removeAllListeners()) + await puppet.quit() } catch (e) { log.error('Wechaty', 'stop() exception: %s', e.message) Raven.captureException(e) @@ -494,7 +494,7 @@ export class Wechaty extends EventEmitter implements Sayable { // MUST use setImmediate at here(the end of this function), // because we need to run the micro task registered by the `emit` method - // setImmediate(() => this.removeAllListeners()) + setImmediate(() => puppet.removeAllListeners()) } return } -- GitLab