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

code clean

上级 f0a53a1c
The Internet Software Consortium License (ISC)
Copyright (c) 2016, Zhuohuan LI
Copyright (c) 2016, Zhuohuan LI <zixia@zixia.net> https://github.com/zixia/
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
......
......@@ -9,6 +9,11 @@ export default async function onMessage(message: Message): Promise<void> {
const sender = message.from()
const content = message.content()
if (room) {
await room.ready()
}
await sender.ready()
console.log((room ? '[' + room.topic() + ']' : '')
+ '<' + sender.name() + '>'
+ ':' + message.toStringDigest()
......
......@@ -84,6 +84,7 @@ export class Message implements Sayable {
constructor(public rawObj?: MessageRawObj) {
this._counter = Message.counter++
log.silly('Message', 'constructor() #%d', this._counter)
if (typeof rawObj === 'string') {
this.rawObj = JSON.parse(rawObj)
......@@ -172,8 +173,8 @@ export class Message implements Sayable {
public to(contact: Contact): Contact
public to(room: Room): Room
public to(id: string): Contact | Room
public to(): Contact | Room
public to(id: string): Contact|Room
public to(): Contact|Room
public to(contact?: Contact|Room|string): Contact|Room {
if (contact) {
if (contact instanceof Contact || contact instanceof Room) {
......@@ -240,7 +241,6 @@ export class Message implements Sayable {
public async ready(): Promise<this> {
log.silly('Message', 'ready()')
// return co.call(this, function* () {
try {
const from = Contact.load(this.obj.from)
const to = Contact.load(this.obj.to)
......@@ -254,7 +254,6 @@ export class Message implements Sayable {
if (room) { await room.ready() } // Room member list
return this // return this for chain
// }).catch(e => { // Exception
} catch (e) {
log.error('Message', 'ready() exception: %s', e)
// console.log(e)
......@@ -264,7 +263,12 @@ export class Message implements Sayable {
}
}
/**
* @deprecated
*/
public get(prop: string): string {
log.warn('Message', 'DEPRECATED get()')
if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']'
throw new Error(`Message.get(${prop}) must be in: ${s}`)
......@@ -272,7 +276,12 @@ export class Message implements Sayable {
return this.obj[prop]
}
/**
* @deprecated
*/
public set(prop: string, value: string): this {
log.warn('Message', 'DEPRECATED set()')
if (typeof value !== 'string') {
throw new Error('value must be string, we got: ' + typeof value)
}
......
/**
* Wechat for Bot. and for human who can talk with bot/robot
* Wechat for Bot. Connecting ChatBots
*
* Interface for puppet
*
......@@ -353,7 +353,7 @@ export class Browser extends EventEmitter {
return
}
public clean(): Promise<any> {
public clean(): Promise<void> {
const max = 30
const backoff = 100
......
......@@ -378,9 +378,7 @@ async function onServerMessage(this: PuppetWeb, data): Promise<void> {
}
// To Be Deleted: set self...
if (this.userId) {
m.set('self', this.userId)
} else {
if (!this.userId) {
log.warn('PuppetWebEvent', 'onServerMessage() without this.userId')
}
......
......@@ -35,10 +35,10 @@ test('PuppetWebFriendRequest.confirm smoking test', t => {
const rawObj = JSON.parse(rawMessageData)
const m = new Message(rawObj)
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.get('content')), 'should match confirm message')
t.true(/^You have added (.+) as your WeChat contact. Start chatting!$/.test(m.content()), 'should match confirm message')
const fr = new PuppetWebFriendRequest()
const contact = Contact.load(m.get('from'))
const contact = m.from()
fr.confirm(contact || new Contact('xx'))
t.true(fr.contact instanceof Contact, 'should have a Contact instance')
......
/**
* Wechat for Bot. and for human who can talk with bot/robot
* Wechat for Bot. Connecting ChatBots
*
* Web Server for puppet
*
......
/**
* Wechat for Bot. and for human who can talk with bot/robot
* Wechat for Bot. Connecting ChatBots
*
* Interface for Puppet
*
......
......@@ -125,16 +125,17 @@ export class Room extends EventEmitter implements Sayable {
public on(event: RoomEventName, listener: Function): this {
log.verbose('Room', 'on(%s, %s)', event, typeof listener)
const thisWithSay = {
say: (content: string) => {
return Config.puppetInstance()
.say(content)
}
}
super.on(event, function() {
return listener.apply(thisWithSay, arguments)
})
// const thisWithSay = {
// say: (content: string) => {
// return Config.puppetInstance()
// .say(content)
// }
// }
// super.on(event, function() {
// return listener.apply(thisWithSay, arguments)
// })
super.on(event, listener) // Room is `Sayable`
return this
}
......
......@@ -188,15 +188,19 @@ export class Wechaty extends EventEmitter implements Sayable {
public on(event: WechatyEventName, listener: Function): this {
log.verbose('Wechaty', 'on(%s, %s)', event, typeof listener)
const thisWithSay: Sayable = {
say: (content: string) => {
return Config.puppetInstance()
.say(content)
}
}
super.on(event, function() {
return listener.apply(thisWithSay, arguments)
})
// const thisWithSay: Sayable = {
// say: (content: string) => {
// return Config.puppetInstance()
// .say(content)
// }
// }
super.on(event, listener) // `this: Wechaty` is Sayable
// (...args) => {
//
// return listener.apply(this, args)
// })
return this
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册