From 4cc52f1aa37c97557bb7bac7d6cb5a2e3af73ba5 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Fri, 17 Aug 2018 11:29:26 +0800 Subject: [PATCH] fix sync for contact; add roomMemberPayloadDirty for sync; prepare for #1552 --- src/user/contact.ts | 16 ++++++++----- src/user/room.ts | 55 +++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/user/contact.ts b/src/user/contact.ts index fd52cd63..01131b9d 100644 --- a/src/user/contact.ts +++ b/src/user/contact.ts @@ -632,30 +632,36 @@ export class Contact extends Accessory implements Sayable { } /** - * Force reload(re-ready()) data for Contact, + * Force reload data for Contact, Sync data from lowlevel API again. * * @returns {Promise} * @example * await contact.sync() - * @private */ public async sync (): Promise { await this.ready(true) } /** + * `ready()` is For FrameWork ONLY! + * + * Please not to use `ready()` at the user land. + * If you want to sync data, uyse `sync()` instead. + * * @private */ - public async ready (noCache = false): Promise { + public async ready ( + forceSync = false, + ): Promise { log.silly('Contact', 'ready() @ %s', this.puppet) - if (this.isReady()) { // already ready + if (!forceSync && this.isReady()) { // already ready log.silly('Contact', 'ready() isReady() true') return } try { - if (noCache) { + if (forceSync) { await this.puppet.contactPayloadDirty(this.id) } this.payload = await this.puppet.contactPayload(this.id) diff --git a/src/user/room.ts b/src/user/room.ts index a8da2284..83396e84 100644 --- a/src/user/room.ts +++ b/src/user/room.ts @@ -315,19 +315,45 @@ export class Room extends Accessory implements Sayable { } /** + * @ignore + * @private + * @deprecated: Use `sync()` instead + */ + public async refresh (): Promise { + await this.sync() + } + + /** + * Force reload data for Room, Sync data from lowlevel API again. + * + * @returns {Promise} + * @example + * await room.sync() + */ + public async sync (): Promise { + await this.ready(true) + } + + /** + * `ready()` is For FrameWork ONLY! + * + * Please not to use `ready()` at the user land. + * If you want to sync data, uyse `sync()` instead. + * * @private */ public async ready ( - dirty = false, + forceSync = false, ): Promise { log.verbose('Room', 'ready()') - if (!dirty && this.isReady()) { + if (!forceSync && this.isReady()) { return } - if (dirty) { + if (forceSync) { await this.puppet.roomPayloadDirty(this.id) + await this.puppet.roomMemberPayloadDirty(this.id) } this.payload = await this.puppet.roomPayload(this.id) @@ -342,8 +368,8 @@ export class Room extends Accessory implements Sayable { .map(id => this.wechaty.Contact.load(id)) .map(contact => { contact.ready() - .catch(() => { - // + .catch(e => { + log.verbose('Room', 'ready() member.ready() rejection: %s', e) }) }), ) @@ -932,25 +958,6 @@ export class Room extends Accessory implements Sayable { return contactList } - /** - * @ignore - */ - public async refresh (): Promise { - return this.sync() - } - - /** - * Force reload data for Room, Sync data for Room - * - * @returns {Promise} - * @example - * await room.sync() - * @private - */ - public async sync (): Promise { - await this.ready(true) - } - /** * Get room's owner from the room. * > Tips: -- GitLab