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

code clean

上级 6ce351b1
......@@ -30,7 +30,7 @@
"test:phantomjs.bak": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=10m \"dist/{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=verbose WECHATY_HEAD=chrome ava --concurrency 0 --ext js --timeout=10m \"dist/{src,test}/**/*.spec.js\"",
"testdev": "cross-env LC_ALL=C WECHATY_LOG=silly ava --ext ts --serial --verbose --fail-fast --timeout=3m",
"testdist": "WECHATY_HEAD=phantomjs ava --ext ts --verbose --fail-fast --timeout=3m",
"testdist": "WECHATY_LOG=SILLY WECHATY_HEAD=chrome ava --ext ts --verbose --fail-fast --timeout=2m",
"ava": "cross-env LC_ALL=C WECHATY_LOG=verbose ts-node node_modules/.bin/ava \"{src,test}/**/*.spec.js\"",
"start": "ts-node bin/client",
"dev": "ts-node dev.ts",
......
......@@ -22,7 +22,9 @@ if (levelRegex.test(level)) {
log.silly('Brolog', 'WECHATY_LOG set level to %s', level)
} else {
log = new Brolog()
log.warn('Brolog', 'env WECHATY_LOG(%s) must be one of silly|verbose|info|warn|error|silent', level)
if (level) {
log.warn('Brolog', 'env WECHATY_LOG(%s) must be one of silly|verbose|info|warn|error|silent', level)
}
}
export { Brolog }
......
......@@ -211,9 +211,9 @@ export class Contact implements Sayable {
return contactList[0]
}
public static load(id: string): Contact | null {
public static load(id: string): Contact {
if (!id || typeof id !== 'string') {
return null
throw new Error('id not found')
}
if (!(id in Contact.pool)) {
......
......@@ -24,8 +24,8 @@ import brolog from './brolog-env'
export class IoClient {
private wechaty: Wechaty | null
private io: Io | null
private wechaty: Wechaty
private io: Io // XXX keep io `null-able` or not? 20161026
private state = new StateMonitor<'online', 'offline'>('IoClient', 'offline')
......@@ -44,9 +44,19 @@ export class IoClient {
this.log.error('IoClient', e.message)
throw e
}
this.wechaty = Wechaty.instance({
profile: token
})
this.io = new Io({
wechaty: this.wechaty
, token: this.token
})
}
public async init(): Promise<IoClient> {
public async init(): Promise<void> {
this.log.verbose('IoClient', 'init()')
// if (/connecting|disconnecting/.test(this.currentState())) {
......@@ -61,18 +71,12 @@ export class IoClient {
this.state.target('online')
this.state.current('online', false)
this.wechaty = Wechaty.instance({
profile: Config.DEFAULT_PROFILE
})
// return co.call(this, function* () {
try {
this.io = await this.initIo()
this.wechaty = await this.initWechaty()
await this.initIo()
await this.initWechaty()
// this.currentState('connected')
this.state.current('online')
return this
// }).catch(e => {
return
} catch (e) {
this.log.error('IoClient', 'init() exception: %s', e.message)
// this.currentState('disconnected')
......@@ -81,13 +85,14 @@ export class IoClient {
}
}
private initWechaty(): Promise<Wechaty> {
private async initWechaty(): Promise<void> {
this.log.verbose('IoClient', 'initWechaty()')
// if (this.targetState() !== 'connected') {
if (this.state.target() !== 'online') {
this.log.warn('IoClient', 'initWechaty() targetState is not `connected`, skipped')
return Promise.resolve(this.wechaty)
const e = new Error('state.target() is not `online`, skipped')
this.log.warn('IoClient', 'initWechaty() %s', e.message)
throw e
}
const wechaty = this.wechaty
......@@ -106,7 +111,7 @@ export class IoClient {
.catch(e => this.log.error('IoClient', 'message.ready() %s' , e))
})
return wechaty.init()
await wechaty.init()
.then(_ => {
this.log.verbose('IoClient', 'wechaty.init() succ')
return wechaty
......@@ -116,34 +121,32 @@ export class IoClient {
wechaty.quit()
throw e
})
return
}
private initIo(): Promise<Io> {
private async initIo(): Promise<void> {
this.log.verbose('IoClient', 'initIo() with token %s', this.token)
// if (this.targetState() !== 'connected') {
if (this.state.target() !== 'online') {
const errMsg = 'initIo() targetState is not `connected`, skipped'
this.log.warn('IoClient', errMsg)
return Promise.reject(errMsg)
const e = new Error('initIo() targetState is not `connected`, skipped')
this.log.warn('IoClient', e.message)
throw e
}
if (!this.wechaty) {
throw new Error('initIo() need a wechaty instance')
}
// const io = this.io = new Io({
// wechaty: this.wechaty
// , token: this.token
// })
const wechaty = this.wechaty
const io = this.io
const io = new Io({
wechaty
, token: this.token
})
return io.init()
await io.init()
.catch(e => {
this.log.verbose('IoClient', 'initIo() init fail: %s', e.message)
throw e
})
return
}
public initWeb(port = Config.httpPort) {
......@@ -184,7 +187,7 @@ export class IoClient {
}
}
public start(): Promise<IoClient> {
public async start(): Promise<void> {
this.log.verbose('IoClient', 'start()')
if (!this.wechaty) {
......@@ -202,28 +205,29 @@ export class IoClient {
this.state.target('online')
this.state.current('online', false)
return this.initIo()
.then(io => {
this.io = io
// this.currentState('connected')
this.state.current('online')
return this
})
.catch(e => {
this.log.error('IoClient', 'start() exception: %s', e.message)
// this.currentState('disconnected')
this.state.current('offline')
throw e
})
try {
await this.initIo()
// .then(() => {
// // this.io = io
// // this.currentState('connected')
this.state.current('online')
return
} catch (e) {
this.log.error('IoClient', 'start() exception: %s', e.message)
// this.currentState('disconnected')
this.state.current('offline')
throw e
}
}
public stop() {
public async stop(): Promise<void> {
this.log.verbose('IoClient', 'stop()')
// this.targetState('disconnected')
// this.currentState('disconnecting')
this.state.target('offline')
this.state.current('offline', false)
// XXX
if (!this.io) {
this.log.warn('IoClient', 'stop() without this.io')
// this.currentState('connected')
......@@ -231,29 +235,30 @@ export class IoClient {
return Promise.resolve()
}
const p = this.io.quit()
await this.io.quit()
// .then(_ => this.currentState('disconnected'))
.then(_ => this.state.current('offline'))
// .then(_ => this.state.current('offline'))
this.state.current('offline')
// XXX 20161026
// this.io = null
return p
return
}
public async restart(): Promise<IoClient> {
public async restart(): Promise<void> {
this.log.verbose('IoClient', 'restart()')
// return co.call(this, function* () {
try {
await this.stop()
await this.start()
return this
// }).catch(e => {
return
} catch (e) {
this.log.error('IoClient', 'restart() exception %s', e.message)
throw e
}
}
public async quit(): Promise<any> {
public async quit(): Promise<void> {
this.log.verbose('IoClient', 'quit()')
// if (this.currentState() === 'disconnecting') {
......@@ -270,17 +275,19 @@ export class IoClient {
try {
if (this.wechaty) {
await this.wechaty.quit()
this.wechaty = null
// this.wechaty = null
} else { this.log.warn('IoClient', 'quit() no this.wechaty') }
if (this.io) {
await this.io.quit()
this.io = null
// this.io = null
} else { this.log.warn('IoClient', 'quit() no this.io') }
// this.currentState('disconnected')
this.state.current('offline')
return
} catch (e) {
this.log.error('IoClient', 'exception: %s', e.message)
......
......@@ -106,7 +106,7 @@ export class Io {
private connected() { return this.ws && this.ws.readyState === WebSocket.OPEN }
public async init(): Promise<Io> {
public async init(): Promise<void> {
log.verbose('Io', 'init()')
// this.targetState('connected')
......@@ -114,7 +114,6 @@ export class Io {
this.state.target('online')
this.state.current('online', false)
// return co.call(this, function* () {
try {
await this.initEventHook()
await this.initWebSocket()
......@@ -122,8 +121,7 @@ export class Io {
// this.currentState('connected')
this.state.current('online')
return this
// }).catch(e => {
return
} catch (e) {
log.warn('Io', 'init() exception: %s', e.message)
// this.currentState('disconnected')
......
......@@ -97,31 +97,36 @@ export class BrowserDriver {
const customChrome = Capabilities.chrome()
.set('chromeOptions', options)
return new Builder()
.setAlertBehavior('ignore')
.forBrowser('chrome')
.withCapabilities(customChrome)
.build()
/**
* XXX when will Builder().build() throw exception???
*/
let driver
let ttl = 3
let err
while (!driver && ttl--) {
try {
driver = new Builder()
.setAlertBehavior('ignore')
.forBrowser('chrome')
.withCapabilities(customChrome)
.build()
} catch (e) {
log.warn('PuppetWebBrowserDriver', 'getChromeDriver() exception: %s, retry ttl: %d', e.message, ttl)
err = e
}
}
// let driver
// let ttl = 3
// let err
// while (!driver && ttl--) {
// try {
// driver = new Builder()
// .setAlertBehavior('ignore')
// .forBrowser('chrome')
// .withCapabilities(customChrome)
// .build()
// } catch (e) {
// log.warn('PuppetWebBrowserDriver', 'getChromeDriver() exception: %s, retry ttl: %d', e.message, ttl)
// err = e
// }
// }
if (driver) {
return driver
}
// if (driver) {
// return driver
// }
throw err
// throw err
}
private getPhantomJsDriver(): WebDriver {
......
......@@ -65,7 +65,7 @@ export class PuppetWeb extends Puppet {
public toString() { return `Class PuppetWeb({browser:${this.browser},port:${this.port}})` }
public async init(): Promise<this> {
public async init(): Promise<void> {
log.verbose('PuppetWeb', `init() with head:${this.setting.head}, profile:${this.setting.profile}`)
// this.targetState('live')
......@@ -100,7 +100,7 @@ export class PuppetWeb extends Puppet {
log.verbose('PuppetWeb', 'init() done')
// this.currentState('live')
this.state.current('live')
return this // for Chaining
return
} catch (e) {
log.error('PuppetWeb', 'init() exception: %s', e.stack)
......@@ -182,14 +182,12 @@ export class PuppetWeb extends Puppet {
public async initBrowser(): Promise<void> {
log.verbose('PuppetWeb', 'initBrowser()')
const browser = new Browser({
this.browser = new Browser({
head: <HeadName>this.setting.head
, sessionFile: this.setting.profile
})
browser.on('dead', Event.onBrowserDead.bind(this))
this.browser = browser
this.browser.on('dead', Event.onBrowserDead.bind(this))
// if (this.targetState() !== 'live') {
if (this.state.target() !== 'live') {
......@@ -199,7 +197,7 @@ export class PuppetWeb extends Puppet {
}
try {
await browser.init()
await this.browser.init()
} catch (e) {
log.error('PuppetWeb', 'initBrowser() exception: %s', e.message)
throw e
......@@ -209,13 +207,11 @@ export class PuppetWeb extends Puppet {
public async initBridge(): Promise<void> {
log.verbose('PuppetWeb', 'initBridge()')
const bridge = new Bridge(
this.bridge = new Bridge(
this // use puppet instead of browser, is because browser might change(die) duaring run time
, this.port
)
this.bridge = bridge
// if (this.targetState() !== 'live') {
if (this.state.target() !== 'live') {
const errMsg = 'initBridge() found targetState != live, no init anymore'
......@@ -224,7 +220,7 @@ export class PuppetWeb extends Puppet {
}
try {
await bridge.init()
await this.bridge.init()
} catch (e) {
if (!this.browser) {
log.warn('PuppetWeb', 'initBridge() without browser?')
......@@ -240,12 +236,12 @@ export class PuppetWeb extends Puppet {
private async initServer(): Promise<void> {
log.verbose('PuppetWeb', 'initServer()')
const server = new Server(this.port)
this.server = new Server(this.port)
server.on('scan' , Event.onServerScan.bind(this))
server.on('login' , Event.onServerLogin.bind(this))
server.on('logout' , Event.onServerLogout.bind(this))
server.on('message' , Event.onServerMessage.bind(this))
this.server.on('scan' , Event.onServerScan.bind(this))
this.server.on('login' , Event.onServerLogin.bind(this))
this.server.on('logout' , Event.onServerLogout.bind(this))
this.server.on('message' , Event.onServerMessage.bind(this))
/**
* @depreciated 20160825 zixia
......@@ -254,12 +250,10 @@ export class PuppetWeb extends Puppet {
*/
// server.on('unload' , Event.onServerUnload.bind(this))
server.on('connection', Event.onServerConnection.bind(this))
server.on('disconnect', Event.onServerDisconnect.bind(this))
server.on('log' , Event.onServerLog.bind(this))
server.on('ding' , Event.onServerDing.bind(this))
this.server = server
this.server.on('connection', Event.onServerConnection.bind(this))
this.server.on('disconnect', Event.onServerDisconnect.bind(this))
this.server.on('log' , Event.onServerLog.bind(this))
this.server.on('ding' , Event.onServerDing.bind(this))
// if (this.targetState() !== 'live') {
if (this.state.target() !== 'live') {
......@@ -268,7 +262,7 @@ export class PuppetWeb extends Puppet {
return Promise.reject(errMsg)
}
await server.init()
await this.server.init()
.catch(e => {
log.error('PuppetWeb', 'initServer() exception: %s', e.message)
throw e
......@@ -304,7 +298,7 @@ export class PuppetWeb extends Puppet {
return this.userId === message.from().id
}
public send(message: Message) {
public async send(message: Message): Promise<void> {
const to = message.to()
const room = message.room()
......@@ -327,23 +321,25 @@ export class PuppetWeb extends Puppet {
, room ? room.topic() : (to as Contact).name()
, content
)
return this.bridge.send(destination.id, content)
await this.bridge.send(destination.id, content)
.catch(e => {
log.error('PuppetWeb', 'send() exception: %s', e.message)
throw e
})
}
return
}
/**
* Bot say...
* send to `filehelper` for notice / log
*/
public say(content: string) {
public async say(content: string): Promise<void> {
const m = new Message()
m.to('filehelper')
m.content(content)
return this.send(m)
await this.send(m)
return
}
// @deprecated
......@@ -378,7 +374,7 @@ export class PuppetWeb extends Puppet {
throw e
})
}
public logined() { return !!(this.user) }
public logined(): boolean { return !!(this.user) }
public ding(data?: any): Promise<string> {
if (!this.bridge) {
return Promise.reject(new Error('ding fail: no bridge(yet)!'))
......
......@@ -39,7 +39,7 @@ export class Server extends EventEmitter {
public toString() { return `Server({port:${this.port}})` }
public async init(): Promise<Server> {
public async init(): Promise<void> {
log.verbose('PuppetWebServer', `init() on port ${this.port}`)
// return new Promise((resolve, reject) => {
......@@ -51,7 +51,7 @@ export class Server extends EventEmitter {
this.socketServer = this.createSocketIo(this.httpsServer)
log.verbose('PuppetWebServer', 'init()-ed')
return this
return
} catch (e) {
// .catch(e => {
log.error('PuppetWebServer', 'init() exception: %s', e.message)
......@@ -162,7 +162,7 @@ export class Server extends EventEmitter {
return
}
public quit(): Promise<any> {
public async quit(): Promise<void> {
log.verbose('PuppetWebServer', 'quit()')
if (this.socketServer) {
log.verbose('PuppetWebServer', 'closing socketServer')
......@@ -178,7 +178,7 @@ export class Server extends EventEmitter {
this.httpsServer.close()
this.httpsServer = null
}
return Promise.resolve()
return
}
}
......
......@@ -56,7 +56,7 @@ test.serial('PuppetWeb login/logout events', async t => {
await pw.quit()
})
test.serial('PuppetWeb server/browser communication', async t => {
test.only('PuppetWeb server/browser communication', async t => {
let pw = new PuppetWeb()
t.truthy(pw, 'should instantiated a PuppetWeb')
......
......@@ -70,7 +70,6 @@ test('Puppet Web watchdog timer', async t => {
try {
const r = await pw.ding(data)
// .then(r => {
if (!r) {
throw new Error('got empty return')
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册