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

add logonoff() method (#926)

上级 fa966db0
...@@ -69,11 +69,7 @@ bot ...@@ -69,11 +69,7 @@ bot
.on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('login' , user => { .on('login' , user => {
log.info('Bot', `${user.name()} logined`) log.info('Bot', `${user.name()} logined`)
bot.say('Wechaty login') bot.say('Wechaty login').catch(console.error)
})
.on('error' , e => {
log.info('Bot', 'error: %s', e)
bot.say('Wechaty error: ' + e.message)
}) })
.on('scan', (url, code) => { .on('scan', (url, code) => {
if (!/201|200/.test(String(code))) { if (!/201|200/.test(String(code))) {
...@@ -111,12 +107,20 @@ bot ...@@ -111,12 +107,20 @@ bot
bot.start() bot.start()
.catch(e => { .catch(e => {
log.error('Bot', 'init() fail: %s', e) log.error('Bot', 'init() fail: %s', e)
bot.quit() bot.stop()
process.exit(-1) process.exit(-1)
}) })
bot.on('error', async e => {
log.error('Bot', 'error: %s', e)
await bot.say('Wechaty error: ' + e.message).catch(console.error)
await bot.stop()
})
finis((code, signal) => { finis((code, signal) => {
const exitMsg = `Wechaty exit ${code} because of ${signal} ` const exitMsg = `Wechaty exit ${code} because of ${signal} `
console.log(exitMsg) console.log(exitMsg)
bot.say(exitMsg) if (bot.logonoff()) {
bot.say(exitMsg).catch(console.error)
}
}) })
...@@ -287,7 +287,7 @@ export class Bridge extends EventEmitter { ...@@ -287,7 +287,7 @@ export class Bridge extends EventEmitter {
log.silly('PuppetWebBridge', 'quit() browser.close()-ed') log.silly('PuppetWebBridge', 'quit() browser.close()-ed')
} catch (e) { } catch (e) {
log.warn('PuppetWebBridge', 'quit() exception: %s', e) log.warn('PuppetWebBridge', 'quit() exception: %s', e)
this.emit('error', e) // this.emit('error', e)
} finally { } finally {
this.state.off(true) this.state.off(true)
} }
......
...@@ -263,6 +263,7 @@ export class PuppetWeb extends Puppet { ...@@ -263,6 +263,7 @@ export class PuppetWeb extends Puppet {
}) })
this.bridge.on('ding' , Event.onDing.bind(this)) this.bridge.on('ding' , Event.onDing.bind(this))
this.bridge.on('error' , e => this.emit('error', e))
this.bridge.on('log' , Event.onLog.bind(this)) this.bridge.on('log' , Event.onLog.bind(this))
this.bridge.on('login' , Event.onLogin.bind(this)) this.bridge.on('login' , Event.onLogin.bind(this))
this.bridge.on('logout' , Event.onLogout.bind(this)) this.bridge.on('logout' , Event.onLogout.bind(this))
...@@ -301,6 +302,11 @@ export class PuppetWeb extends Puppet { ...@@ -301,6 +302,11 @@ export class PuppetWeb extends Puppet {
} }
public logined(): boolean { public logined(): boolean {
log.warn('PuppetWeb', 'logined() DEPRECATED. use logonoff() instead.')
return this.logonoff()
}
public logonoff(): boolean {
return !!(this.user) return !!(this.user)
} }
...@@ -722,7 +728,7 @@ export class PuppetWeb extends Puppet { ...@@ -722,7 +728,7 @@ export class PuppetWeb extends Puppet {
* send to `filehelper` for notice / log * send to `filehelper` for notice / log
*/ */
public async say(content: string): Promise<boolean> { public async say(content: string): Promise<boolean> {
if (!this.logined()) { if (!this.logonoff()) {
throw new Error('can not say before login') throw new Error('can not say before login')
} }
......
...@@ -110,14 +110,24 @@ export abstract class Puppet extends EventEmitter implements Sayable { ...@@ -110,14 +110,24 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract self() : Contact public abstract self() : Contact
/**
* Message
*/
public abstract forward(message: MediaMessage, contact: Contact | Room) : Promise<boolean> public abstract forward(message: MediaMessage, contact: Contact | Room) : Promise<boolean>
public abstract say(content: string) : Promise<boolean> public abstract say(content: string) : Promise<boolean>
public abstract send(message: Message | MediaMessage) : Promise<boolean> public abstract send(message: Message | MediaMessage) : Promise<boolean>
/**
* Login / Logout
*/
public abstract logonoff() : boolean
public abstract reset(reason?: string) : void public abstract reset(reason?: string) : void
public abstract logout() : Promise<void> public abstract logout() : Promise<void>
public abstract quit() : Promise<void> public abstract quit() : Promise<void>
/**
* Misc
*/
public abstract ding() : Promise<string> public abstract ding() : Promise<string>
/** /**
......
...@@ -523,6 +523,24 @@ export class Wechaty extends EventEmitter implements Sayable { ...@@ -523,6 +523,24 @@ export class Wechaty extends EventEmitter implements Sayable {
return return
} }
/**
* Get the logon / logoff state
*
* @returns {boolean}
* @example
* if (bot.logonoff()) {
* console.log('Bot logined')
* } else {
* console.log('Bot not logined')
* }
*/
public logonoff(): Boolean {
if (!this.puppet) {
return false
}
return this.puppet.logonoff()
}
/** /**
* Get current user * Get current user
* *
......
...@@ -64,13 +64,13 @@ test('login/logout events', sinonTest(async function (t: test.Test) { ...@@ -64,13 +64,13 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
await pw.init() await pw.init()
t.pass('should be inited') t.pass('should be inited')
t.is(pw.logined() , false , 'should be not logined') t.is(pw.logonoff() , false , 'should be not logined')
const EXPECTED_CHIPER = 'loginFired' const EXPECTED_CHIPER = 'loginFired'
const loginPromise = new Promise(r => pw.once('login', _ => r(EXPECTED_CHIPER))) const loginPromise = new Promise(r => pw.once('login', _ => r(EXPECTED_CHIPER)))
pw.bridge.emit('login', 'TestPuppetWeb') pw.bridge.emit('login', 'TestPuppetWeb')
t.is(await loginPromise, EXPECTED_CHIPER, 'should fired login event') t.is(await loginPromise, EXPECTED_CHIPER, 'should fired login event')
t.is(pw.logined(), true , 'should be logined') t.is(pw.logonoff(), true , 'should be logined')
t.ok((pw.bridge.getUserName as any).called, 'bridge.getUserName should be called') t.ok((pw.bridge.getUserName as any).called, 'bridge.getUserName should be called')
t.ok((pw.getContact as any).called, 'pw.getContact should be called') t.ok((pw.getContact as any).called, 'pw.getContact should be called')
...@@ -81,7 +81,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) { ...@@ -81,7 +81,7 @@ test('login/logout events', sinonTest(async function (t: test.Test) {
const logoutPromise = new Promise((res, rej) => pw.once('logout', _ => res('logoutFired'))) const logoutPromise = new Promise((res, rej) => pw.once('logout', _ => res('logoutFired')))
pw.bridge.emit('logout') pw.bridge.emit('logout')
t.is(await logoutPromise, 'logoutFired', 'should fire logout event') t.is(await logoutPromise, 'logoutFired', 'should fire logout event')
t.is(pw.logined(), false, 'should be logouted') t.is(pw.logonoff(), false, 'should be logouted')
await pw.quit() await pw.quit()
profile.destroy() profile.destroy()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册