From d3471839ca001062f346aa7faf9a1be4d0c658ff Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Mon, 5 Sep 2016 16:49:49 +0800 Subject: [PATCH] code clean up --- src/puppet-web/puppet-web.js | 77 ++++++++++++++++++------------------ src/wechaty.js | 12 +++--- 2 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/puppet-web/puppet-web.js b/src/puppet-web/puppet-web.js index 2a7e423d..84cfecd9 100644 --- a/src/puppet-web/puppet-web.js +++ b/src/puppet-web/puppet-web.js @@ -47,6 +47,8 @@ class PuppetWeb extends Puppet { this.userId = null // user id this.user = null // of user self + + this.on('watchdog', Watchdog.onFeed.bind(this)) } toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` } @@ -56,8 +58,6 @@ class PuppetWeb extends Puppet { this.readyState('connecting') - this.on('watchdog', Watchdog.onFeed.bind(this)) - return co.call(this, function* () { this.port = yield UtilLib.getPort(Config.DEFAULT_PUPPET_PORT) @@ -75,7 +75,6 @@ class PuppetWeb extends Puppet { this.bridge = yield this.initBridge() log.verbose('PuppetWeb', 'initBridge() done') - // this.watchDog('inited') // start watchdog this.emit('watchdog', { data: 'inited' }) }) .catch(e => { // Reject @@ -190,14 +189,14 @@ class PuppetWeb extends Puppet { }) return bridge.init() - .catch(e => { - if (this.browser.dead()) { - log.warn('PuppetWeb', 'initBridge() found browser dead, wait it to restore') - } else { - log.error('PuppetWeb', 'initBridge() exception: %s', e.message) - throw e - } - }) + .catch(e => { + if (this.browser.dead()) { + log.warn('PuppetWeb', 'initBridge() found browser dead, wait it to restore') + } else { + log.error('PuppetWeb', 'initBridge() exception: %s', e.message) + throw e + } + }) } initServer() { @@ -222,10 +221,10 @@ class PuppetWeb extends Puppet { server.on('ding' , Event.onServerDing.bind(this)) return server.init() - .catch(e => { - log.error('PuppetWeb', 'initServer() exception: %s', e.message) - throw e - }) + .catch(e => { + log.error('PuppetWeb', 'initServer() exception: %s', e.message) + throw e + }) } @@ -257,10 +256,10 @@ class PuppetWeb extends Puppet { log.silly('PuppetWeb', `send(${destination}, ${content})`) return this.bridge.send(destination, content) - .catch(e => { - log.error('PuppetWeb', 'send() exception: %s', e.message) - throw e - }) + .catch(e => { + log.error('PuppetWeb', 'send() exception: %s', e.message) + throw e + }) } reply(message, replyContent) { if (this.self(message)) { @@ -287,10 +286,10 @@ class PuppetWeb extends Puppet { */ logout() { return this.bridge.logout() - .catch(e => { - log.error('PuppetWeb', 'logout() exception: %s', e.message) - throw e - }) + .catch(e => { + log.error('PuppetWeb', 'logout() exception: %s', e.message) + throw e + }) } getContact(id) { @@ -299,10 +298,10 @@ class PuppetWeb extends Puppet { } return this.bridge.getContact(id) - .catch(e => { - log.error('PuppetWeb', 'getContact(%d) exception: %s', id, e.message) - throw e - }) + .catch(e => { + log.error('PuppetWeb', 'getContact(%d) exception: %s', id, e.message) + throw e + }) } logined() { return !!(this.user) } ding(data) { @@ -310,19 +309,19 @@ class PuppetWeb extends Puppet { return Promise.reject(new Error('ding fail: no bridge(yet)!')) } return this.bridge.ding(data) - .catch(e => { - log.warn('PuppetWeb', 'ding(%s) rejected: %s', data, e.message) - throw e - }) + .catch(e => { + log.warn('PuppetWeb', 'ding(%s) rejected: %s', data, e.message) + throw e + }) } } -Object.assign(PuppetWeb, { - default: PuppetWeb - , PuppetWeb - , Server - , Browser - , Bridge -}) +// Object.assign(PuppetWeb, { +// default: PuppetWeb +// , PuppetWeb +// , Server +// , Browser +// , Bridge +// }) -module.exports = PuppetWeb +module.exports = PuppetWeb.default = PuppetWeb.PuppetWeb = PuppetWeb diff --git a/src/wechaty.js b/src/wechaty.js index 184a8974..27861953 100644 --- a/src/wechaty.js +++ b/src/wechaty.js @@ -9,16 +9,14 @@ * */ const EventEmitter = require('events') +const path = require('path') const co = require('co') const fs = require('fs') -const path = require('path') - -const log = require('./brolog-env') -const UtilLib = require('./util-lib') - -const PuppetWeb = require('./puppet-web') -const Config = require('./config') +const PuppetWeb = require('./puppet-web') +const UtilLib = require('./util-lib') +const Config = require('./config') +const log = require('./brolog-env') class Wechaty extends EventEmitter { -- GitLab