diff --git a/src/config.ts b/src/config.ts index 55df1c9992aafe06ae4dacaa5b48b5fe2531c316..78dd79bfb48a6f1cdf35ef0f64c5b204525493f4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -141,20 +141,19 @@ function puppetInstance(instance: Puppet): void function puppetInstance(instance?: Puppet | null): Puppet | void { if (instance === undefined) { - if (!global['_puppetInstance']) { + if (!this._puppetInstance) { throw new Error('no puppet instance') } - return global['_puppetInstance'] + return this._puppetInstance } else if (instance === null) { log.verbose('Config', 'puppetInstance(null)') - global['_puppetInstance'] = null + this._puppetInstance = null return - } log.verbose('Config', 'puppetInstance(%s)', instance.constructor.name) - global['_puppetInstance'] = instance + this._puppetInstance = instance return } diff --git a/src/message-media.ts b/src/message-media.ts index b7ddaefba5a4a864b89a87bf042d3adcfbd53502..684c165074a118bd42f3fd7ec9a6f5149066ead7 100644 --- a/src/message-media.ts +++ b/src/message-media.ts @@ -20,6 +20,7 @@ export class MediaMessage extends Message { constructor(rawObj) { super(rawObj) + // FIXME: decoupling needed this.bridge = (Config.puppetInstance() as PuppetWeb) .bridge } @@ -89,6 +90,7 @@ export class MediaMessage extends Message { public readyStream(): Promise { return this.ready() .then(() => { + // FIXME: decoupling needed return (Config.puppetInstance() as PuppetWeb) .browser.readCookie() }) diff --git a/src/puppet-web/firer.ts b/src/puppet-web/firer.ts index 2751ea5e197d14dd5a3232e2cdc3d0f63b37a12d..2602165b07834b7666723278f526af51d428b836 100644 --- a/src/puppet-web/firer.ts +++ b/src/puppet-web/firer.ts @@ -272,7 +272,7 @@ async function checkRoomJoin(m: Message): Promise { return } -function parseRoomLeave(content: string): string|null { +function parseRoomLeave(content: string): string { const reList = regexConfig.roomLeave let found: string[]|null = [] diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index 4c52cfcc96ceae2b2976b39de71f528be15772c7..f14afbca55b9218630f94bad791f9f83df259568 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -95,7 +95,7 @@ export class PuppetWeb extends Puppet { const food: WatchdogFood = { data: 'inited' - , timeout: 120000 // 2 mins for first login + , timeout: 2 * 60 * 1000 // 2 mins for first login } this.emit('watchdog', food) @@ -368,6 +368,7 @@ export class PuppetWeb extends Puppet { throw e }) } + public logined(): boolean { return !!(this.user) } public ding(data?: any): Promise { if (!this.bridge) { diff --git a/src/puppet.ts b/src/puppet.ts index cf59798542db76a5b43bd9c599dc60d359d94e7f..9f6c500957c3ca0e5411b09b634b011c8f813e8d 100644 --- a/src/puppet.ts +++ b/src/puppet.ts @@ -35,7 +35,7 @@ export abstract class Puppet extends EventEmitter implements Sayable { super() } - public abstract async init(): Promise + public abstract async init(): Promise /** * @deprecated * use Message.self() instead diff --git a/src/room.ts b/src/room.ts index 5118efe8603a7374e9ea74f4f930b7bb0ae8244b..c346027ff95254f196705f52464a576c6aa52f20 100644 --- a/src/room.ts +++ b/src/room.ts @@ -70,22 +70,23 @@ export class Room extends EventEmitter implements Sayable { return !!(this.obj && this.obj.memberList && this.obj.memberList.length) } - public async refresh(): Promise { + public async refresh(): Promise { if (this.isReady()) { this.dirtyObj = this.obj } this.obj = null - return this.ready() + await this.ready() + return } - public async ready(contactGetter?: (id: string) => Promise): Promise { + public async ready(contactGetter?: (id: string) => Promise): Promise { log.silly('Room', 'ready(%s)', contactGetter ? contactGetter.constructor.name : '') if (!this.id) { const e = new Error('ready() on a un-inited Room') log.warn('Room', e.message) - return Promise.reject(e) + throw e } else if (this.isReady()) { - return Promise.resolve(this) + return } else if (this.obj && this.obj.id) { log.warn('Room', 'ready() has obj.id but memberList empty in room %s. reloading', this.obj.topic) } @@ -109,7 +110,7 @@ export class Room extends EventEmitter implements Sayable { } await Promise.all(this.obj.memberList.map(c => c.ready(contactGetter))) - return this + return } catch (e) { log.error('Room', 'contactGetter(%s) exception: %s', this.id, e.message) @@ -423,7 +424,9 @@ export class Room extends EventEmitter implements Sayable { if (!roomList || roomList.length < 1) { throw new Error('no room found') } - return roomList[0].ready() + const room = roomList[0] + await room.ready() + return room } public static load(id: string): Room | null { diff --git a/src/wechaty.ts b/src/wechaty.ts index 323ec3bd001b5859323d54aff34fd536d54ea363..d4751056aef76fb566f0b4efa8f57a312083eb80 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -323,8 +323,8 @@ export class Wechaty extends EventEmitter implements Sayable { return } - public sleep(millisecond: number): Promise { - return new Promise(resolve => { + public async sleep(millisecond: number): Promise { + await new Promise(resolve => { setTimeout(resolve, millisecond) }) }