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

Revert "try to use file to save memory"

This reverts commit 4e56c04b.
上级 3b34dfc8
...@@ -4,7 +4,7 @@ import fs from 'fs-extra' ...@@ -4,7 +4,7 @@ import fs from 'fs-extra'
import { MemoryCard } from 'memory-card' import { MemoryCard } from 'memory-card'
import { StateSwitch } from 'state-switch' import { StateSwitch } from 'state-switch'
import { FlashStore } from 'flash-store' import { FlashStoreSync } from 'flash-store'
import { import {
Subscription, Subscription,
} from 'rxjs' } from 'rxjs'
...@@ -71,11 +71,11 @@ export class PadchatManager extends PadchatRpc { ...@@ -71,11 +71,11 @@ export class PadchatManager extends PadchatRpc {
private userId?: string private userId?: string
private storeContactRawPayload? : FlashStore<string, PadchatContactPayload> private cacheContactRawPayload? : FlashStoreSync<string, PadchatContactPayload>
private storeRoomMemberRawPayload? : FlashStore<string, { private cacheRoomMemberRawPayload? : FlashStoreSync<string, {
[contactId: string]: PadchatRoomMemberPayload, [contactId: string]: PadchatRoomMemberPayload,
}> }>
private storeRoomRawPayload? : FlashStore<string, PadchatRoomPayload> private cacheRoomRawPayload? : FlashStoreSync<string, PadchatRoomPayload>
private readonly state : StateSwitch private readonly state : StateSwitch
private readonly delayQueueExecutor : DelayQueueExector private readonly delayQueueExecutor : DelayQueueExector
...@@ -107,9 +107,9 @@ export class PadchatManager extends PadchatRpc { ...@@ -107,9 +107,9 @@ export class PadchatManager extends PadchatRpc {
): Promise<void> { ): Promise<void> {
log.verbose('PuppetPadchatManager', 'initCache(%s, %s)', token, userId) log.verbose('PuppetPadchatManager', 'initCache(%s, %s)', token, userId)
if ( this.storeContactRawPayload if ( this.cacheContactRawPayload
|| this.storeRoomMemberRawPayload || this.cacheRoomMemberRawPayload
|| this.storeRoomRawPayload || this.cacheRoomRawPayload
) { ) {
throw new Error('cache exists') throw new Error('cache exists')
} }
...@@ -131,32 +131,27 @@ export class PadchatManager extends PadchatRpc { ...@@ -131,32 +131,27 @@ export class PadchatManager extends PadchatRpc {
await fs.mkdirp(baseDir) await fs.mkdirp(baseDir)
} }
this.storeContactRawPayload = new FlashStore(path.join(baseDir, 'contact-raw-payload')) this.cacheContactRawPayload = new FlashStoreSync(path.join(baseDir, 'contact-raw-payload'))
this.storeRoomMemberRawPayload = new FlashStore(path.join(baseDir, 'room-member-raw-payload')) this.cacheRoomMemberRawPayload = new FlashStoreSync(path.join(baseDir, 'room-member-raw-payload'))
this.storeRoomRawPayload = new FlashStore(path.join(baseDir, 'room-raw-payload')) this.cacheRoomRawPayload = new FlashStoreSync(path.join(baseDir, 'room-raw-payload'))
// await Promise.all([ await Promise.all([
// this.storeContactRawPayload.ready(), this.cacheContactRawPayload.ready(),
// this.storeRoomMemberRawPayload.ready(), this.cacheRoomMemberRawPayload.ready(),
// this.storeRoomRawPayload.ready(), this.cacheRoomRawPayload.ready(),
// ]) ])
let roomMemberTotalNum = 0 const roomMemberTotalNum = [...this.cacheRoomMemberRawPayload.values()].reduce(
for await (const value of this.storeRoomMemberRawPayload.values()) { (accuVal, currVal) => {
roomMemberTotalNum += Object.keys(value).length return accuVal + Object.keys(currVal).length
} },
0,
// const roomMemberTotalNum = [...this.storeRoomMemberRawPayload.values()].reduce( )
// (accuVal, currVal) => {
// return accuVal + Object.keys(currVal).length
// },
// 0,
// )
log.verbose('PuppetPadchatManager', 'initCache() inited %d Contacts, %d RoomMembers, %d Rooms, cachedir="%s"', log.verbose('PuppetPadchatManager', 'initCache() inited %d Contacts, %d RoomMembers, %d Rooms, cachedir="%s"',
this.storeContactRawPayload.size, this.cacheContactRawPayload.size,
roomMemberTotalNum, roomMemberTotalNum,
this.storeRoomRawPayload.size, this.cacheRoomRawPayload.size,
baseDir, baseDir,
) )
} }
...@@ -164,17 +159,17 @@ export class PadchatManager extends PadchatRpc { ...@@ -164,17 +159,17 @@ export class PadchatManager extends PadchatRpc {
protected async releaseCache(): Promise<void> { protected async releaseCache(): Promise<void> {
log.verbose('PuppetPadchatManager', 'releaseCache()') log.verbose('PuppetPadchatManager', 'releaseCache()')
if ( this.storeContactRawPayload if ( this.cacheContactRawPayload
&& this.storeRoomMemberRawPayload && this.cacheRoomMemberRawPayload
&& this.storeRoomRawPayload && this.cacheRoomRawPayload
) { ) {
await this.storeContactRawPayload.close(), await this.cacheContactRawPayload.close(),
await this.storeRoomMemberRawPayload.close(), await this.cacheRoomMemberRawPayload.close(),
await this.storeRoomRawPayload.close(), await this.cacheRoomRawPayload.close(),
this.storeContactRawPayload = undefined this.cacheContactRawPayload = undefined
this.storeRoomMemberRawPayload = undefined this.cacheRoomMemberRawPayload = undefined
this.storeRoomRawPayload = undefined this.cacheRoomRawPayload = undefined
log.silly('PuppetPadchatManager', 'releaseCache() cache closed.') log.silly('PuppetPadchatManager', 'releaseCache() cache closed.')
} else { } else {
...@@ -282,8 +277,8 @@ export class PadchatManager extends PadchatRpc { ...@@ -282,8 +277,8 @@ export class PadchatManager extends PadchatRpc {
/** /**
* Refresh the login-ed user payload * Refresh the login-ed user payload
*/ */
if (this.storeContactRawPayload) { if (this.cacheContactRawPayload) {
await this.storeContactRawPayload.delete(this.userId) this.cacheContactRawPayload.delete(this.userId)
await this.contactRawPayload(this.userId) await this.contactRawPayload(this.userId)
} }
...@@ -558,7 +553,7 @@ export class PadchatManager extends PadchatRpc { ...@@ -558,7 +553,7 @@ export class PadchatManager extends PadchatRpc {
// wait 1 second and try again // wait 1 second and try again
await new Promise(r => setTimeout(r, 1000)) await new Promise(r => setTimeout(r, 1000))
return this.emitLoginQrcode() return await this.emitLoginQrcode()
} }
const fileBox = FileBox.fromBase64(result.qr_code, 'qrcode.jpg') const fileBox = FileBox.fromBase64(result.qr_code, 'qrcode.jpg')
...@@ -676,42 +671,34 @@ export class PadchatManager extends PadchatRpc { ...@@ -676,42 +671,34 @@ export class PadchatManager extends PadchatRpc {
} }
} }
public async getContactIdList(): Promise<string[]> { public getContactIdList(): string[] {
log.verbose('PuppetPadchatManager', 'getContactIdList()') log.verbose('PuppetPadchatManager', 'getContactIdList()')
if (!this.storeContactRawPayload) { if (!this.cacheContactRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
// const contactIdList = [...this.storeContactRawPayload.keys()] const contactIdList = [...this.cacheContactRawPayload.keys()]
const contactIdList: string[] = []
for await (const contactId of this.storeContactRawPayload.keys()) {
contactIdList.push(contactId)
}
log.silly('PuppetPadchatManager', 'getContactIdList() = %d', contactIdList.length) log.silly('PuppetPadchatManager', 'getContactIdList() = %d', contactIdList.length)
return contactIdList return contactIdList
} }
public async getRoomIdList(): Promise<string[]> { public getRoomIdList(): string[] {
log.verbose('PuppetPadchatManager', 'getRoomIdList()') log.verbose('PuppetPadchatManager', 'getRoomIdList()')
if (!this.storeRoomRawPayload) { if (!this.cacheRoomRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
// const roomIdList = [...this.storeRoomRawPayload.keys()] const roomIdList = [...this.cacheRoomRawPayload.keys()]
const roomIdList: string[] = []
for await (const roomId of this.storeRoomRawPayload.keys()) {
roomIdList.push(roomId)
}
log.verbose('PuppetPadchatManager', 'getRoomIdList()=%d', roomIdList.length) log.verbose('PuppetPadchatManager', 'getRoomIdList()=%d', roomIdList.length)
return roomIdList return roomIdList
} }
public async roomMemberRawPayloadDirty( public roomMemberRawPayloadDirty(
roomId: string, roomId: string,
): Promise<void> { ): void {
log.verbose('PuppetPadchatManager', 'roomMemberRawPayloadDirty(%d)', roomId) log.verbose('PuppetPadchatManager', 'roomMemberRawPayloadDirty(%d)', roomId)
if (!this.storeRoomMemberRawPayload) { if (!this.cacheRoomMemberRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
await this.storeRoomMemberRawPayload.delete(roomId) this.cacheRoomMemberRawPayload.delete(roomId)
} }
public async getRoomMemberIdList( public async getRoomMemberIdList(
...@@ -719,15 +706,15 @@ export class PadchatManager extends PadchatRpc { ...@@ -719,15 +706,15 @@ export class PadchatManager extends PadchatRpc {
dirty = false, dirty = false,
): Promise<string[]> { ): Promise<string[]> {
log.verbose('PuppetPadchatManager', 'getRoomMemberIdList(%d)', roomId) log.verbose('PuppetPadchatManager', 'getRoomMemberIdList(%d)', roomId)
if (!this.storeRoomMemberRawPayload) { if (!this.cacheRoomMemberRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
if (dirty) { if (dirty) {
await this.roomMemberRawPayloadDirty(roomId) this.roomMemberRawPayloadDirty(roomId)
} }
const memberRawPayloadDict = await this.storeRoomMemberRawPayload.get(roomId) const memberRawPayloadDict = this.cacheRoomMemberRawPayload.get(roomId)
|| await this.syncRoomMember(roomId) || await this.syncRoomMember(roomId)
if (!memberRawPayloadDict) { if (!memberRawPayloadDict) {
...@@ -742,24 +729,24 @@ export class PadchatManager extends PadchatRpc { ...@@ -742,24 +729,24 @@ export class PadchatManager extends PadchatRpc {
return memberIdList return memberIdList
} }
public async roomRawPayloadDirty( public roomRawPayloadDirty(
roomId: string, roomId: string,
): Promise<void> { ): void {
log.verbose('PuppetPadchatManager', 'roomRawPayloadDirty(%d)', roomId) log.verbose('PuppetPadchatManager', 'roomRawPayloadDirty(%d)', roomId)
if (!this.storeRoomRawPayload) { if (!this.cacheRoomRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
await this.storeRoomRawPayload.delete(roomId) this.cacheRoomRawPayload.delete(roomId)
} }
public async roomMemberRawPayload(roomId: string): Promise<{ [contactId: string]: PadchatRoomMemberPayload }> { public async roomMemberRawPayload(roomId: string): Promise<{ [contactId: string]: PadchatRoomMemberPayload }> {
log.verbose('PuppetPadchatManager', 'roomMemberRawPayload(%s)', roomId) log.verbose('PuppetPadchatManager', 'roomMemberRawPayload(%s)', roomId)
if (!this.storeRoomMemberRawPayload) { if (!this.cacheRoomMemberRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
const memberRawPayloadDict = await this.storeRoomMemberRawPayload.get(roomId) const memberRawPayloadDict = this.cacheRoomMemberRawPayload.get(roomId)
|| await this.syncRoomMember(roomId) || await this.syncRoomMember(roomId)
if (!memberRawPayloadDict) { if (!memberRawPayloadDict) {
...@@ -781,8 +768,8 @@ export class PadchatManager extends PadchatRpc { ...@@ -781,8 +768,8 @@ export class PadchatManager extends PadchatRpc {
* Room Id not exist * Room Id not exist
* See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016 * See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016
*/ */
await this.roomMemberRawPayloadDirty(roomId) this.roomMemberRawPayloadDirty(roomId)
await this.roomRawPayloadDirty(roomId) this.roomRawPayloadDirty(roomId)
return {} return {}
...@@ -800,16 +787,16 @@ export class PadchatManager extends PadchatRpc { ...@@ -800,16 +787,16 @@ export class PadchatManager extends PadchatRpc {
memberDict[contactId] = memberPayload memberDict[contactId] = memberPayload
} }
if (!this.storeRoomMemberRawPayload) { if (!this.cacheRoomMemberRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
const oldMemberDict = await this.storeRoomMemberRawPayload.get(roomId) const oldMemberDict = this.cacheRoomMemberRawPayload.get(roomId)
const newMemberDict = { const newMemberDict = {
...oldMemberDict, ...oldMemberDict,
...memberDict, ...memberDict,
} }
await this.storeRoomMemberRawPayload.set(roomId, newMemberDict) this.cacheRoomMemberRawPayload.set(roomId, newMemberDict)
return newMemberDict return newMemberDict
} }
...@@ -830,16 +817,16 @@ export class PadchatManager extends PadchatRpc { ...@@ -830,16 +817,16 @@ export class PadchatManager extends PadchatRpc {
continue continue
} }
if ( !this.storeContactRawPayload if ( !this.cacheContactRawPayload
|| !this.storeRoomRawPayload || !this.cacheRoomRawPayload
) { ) {
throw new Error('no cache') throw new Error('no cache')
} }
log.silly('PuppetPadchatManager', 'syncContactsAndRooms() syncing %d out of Contact(%d) & Room(%d) ...', log.silly('PuppetPadchatManager', 'syncContactsAndRooms() syncing %d out of Contact(%d) & Room(%d) ...',
syncContactList.length, syncContactList.length,
await this.storeContactRawPayload.size, this.cacheContactRawPayload.size,
await this.storeRoomRawPayload.size, this.cacheRoomRawPayload.size,
) )
for (const syncContact of syncContactList) { for (const syncContact of syncContactList) {
...@@ -862,7 +849,7 @@ export class PadchatManager extends PadchatRpc { ...@@ -862,7 +849,7 @@ export class PadchatManager extends PadchatRpc {
const roomId = syncContact.user_name const roomId = syncContact.user_name
const roomPayload = syncContact as PadchatRoomPayload const roomPayload = syncContact as PadchatRoomPayload
await this.storeRoomRawPayload.set(roomId, roomPayload) this.cacheRoomRawPayload.set(roomId, roomPayload)
/** /**
* Use delay queue executor to sync room: * Use delay queue executor to sync room:
...@@ -885,7 +872,7 @@ export class PadchatManager extends PadchatRpc { ...@@ -885,7 +872,7 @@ export class PadchatManager extends PadchatRpc {
const contactPayload = syncContact as PadchatContactPayload const contactPayload = syncContact as PadchatContactPayload
const contactId = contactPayload.user_name const contactId = contactPayload.user_name
await this.storeContactRawPayload.set(contactId, contactPayload) this.cacheContactRawPayload.set(contactId, contactPayload)
} else { } else {
throw new Error('id is neither room nor contact') throw new Error('id is neither room nor contact')
...@@ -909,14 +896,14 @@ export class PadchatManager extends PadchatRpc { ...@@ -909,14 +896,14 @@ export class PadchatManager extends PadchatRpc {
} }
} }
public async contactRawPayloadDirty( public contactRawPayloadDirty(
contactId: string, contactId: string,
): Promise<void> { ): void {
log.verbose('PuppetPadchatManager', 'contactRawPayloadDirty(%d)', contactId) log.verbose('PuppetPadchatManager', 'contactRawPayloadDirty(%d)', contactId)
if (!this.storeContactRawPayload) { if (!this.cacheContactRawPayload) {
throw new Error('cache not inited' ) throw new Error('cache not inited' )
} }
await this.storeContactRawPayload.delete(contactId) this.cacheContactRawPayload.delete(contactId)
} }
public async contactRawPayload(contactId: string): Promise<PadchatContactPayload> { public async contactRawPayload(contactId: string): Promise<PadchatContactPayload> {
...@@ -925,12 +912,12 @@ export class PadchatManager extends PadchatRpc { ...@@ -925,12 +912,12 @@ export class PadchatManager extends PadchatRpc {
const rawPayload = await Misc.retry(async (retry, attempt) => { const rawPayload = await Misc.retry(async (retry, attempt) => {
log.silly('PuppetPadchatManager', 'contactRawPayload(%s) retry() attempt=%d', contactId, attempt) log.silly('PuppetPadchatManager', 'contactRawPayload(%s) retry() attempt=%d', contactId, attempt)
if (!this.storeContactRawPayload) { if (!this.cacheContactRawPayload) {
throw new Error('no cache') throw new Error('no cache')
} }
if (this.storeContactRawPayload.has(contactId)) { if (this.cacheContactRawPayload.has(contactId)) {
return this.storeContactRawPayload.get(contactId) return this.cacheContactRawPayload.get(contactId)
} }
const tryRawPayload = await this.WXGetContactPayload(contactId) const tryRawPayload = await this.WXGetContactPayload(contactId)
...@@ -938,7 +925,7 @@ export class PadchatManager extends PadchatRpc { ...@@ -938,7 +925,7 @@ export class PadchatManager extends PadchatRpc {
// check user_name too becasue the server might return {} // check user_name too becasue the server might return {}
// See issue #1358 https://github.com/Chatie/wechaty/issues/1358 // See issue #1358 https://github.com/Chatie/wechaty/issues/1358
if (tryRawPayload /* && tryRawPayload.user_name */) { if (tryRawPayload /* && tryRawPayload.user_name */) {
await this.storeContactRawPayload.set(contactId, tryRawPayload) this.cacheContactRawPayload.set(contactId, tryRawPayload)
return tryRawPayload return tryRawPayload
} }
return retry(new Error('tryRawPayload empty')) return retry(new Error('tryRawPayload empty'))
...@@ -950,20 +937,18 @@ export class PadchatManager extends PadchatRpc { ...@@ -950,20 +937,18 @@ export class PadchatManager extends PadchatRpc {
return rawPayload return rawPayload
} }
public async roomRawPayload( public async roomRawPayload(id: string): Promise<PadchatRoomPayload> {
id: string,
): Promise<PadchatRoomPayload> {
log.verbose('PuppetPadchatManager', 'roomRawPayload(%s)', id) log.verbose('PuppetPadchatManager', 'roomRawPayload(%s)', id)
const rawPayload = await Misc.retry(async (retry, attempt) => { const rawPayload = await Misc.retry(async (retry, attempt) => {
log.silly('PuppetPadchatManager', 'roomRawPayload(%s) retry() attempt=%d', id, attempt) log.silly('PuppetPadchatManager', 'roomRawPayload(%s) retry() attempt=%d', id, attempt)
if (!this.storeRoomRawPayload) { if (!this.cacheRoomRawPayload) {
throw new Error('no cache') throw new Error('no cache')
} }
if (await this.storeRoomRawPayload.has(id)) { if (this.cacheRoomRawPayload.has(id)) {
return this.storeRoomRawPayload.get(id) return this.cacheRoomRawPayload.get(id)
} }
const tryRawPayload = await this.WXGetRoomPayload(id) const tryRawPayload = await this.WXGetRoomPayload(id)
...@@ -971,7 +956,7 @@ export class PadchatManager extends PadchatRpc { ...@@ -971,7 +956,7 @@ export class PadchatManager extends PadchatRpc {
// check user_name too becasue the server might return {} // check user_name too becasue the server might return {}
// See issue #1358 https://github.com/Chatie/wechaty/issues/1358 // See issue #1358 https://github.com/Chatie/wechaty/issues/1358
if (tryRawPayload /* && tryRawPayload.user_name */) { if (tryRawPayload /* && tryRawPayload.user_name */) {
await this.storeRoomRawPayload.set(id, tryRawPayload) this.cacheRoomRawPayload.set(id, tryRawPayload)
return tryRawPayload return tryRawPayload
} }
return retry(new Error('tryRawPayload empty')) return retry(new Error('tryRawPayload empty'))
......
...@@ -602,7 +602,7 @@ export class PuppetPadchat extends Puppet { ...@@ -602,7 +602,7 @@ export class PuppetPadchat extends Puppet {
throw new Error('no padchat manager') throw new Error('no padchat manager')
} }
const contactIdList = await this.padchatManager.getContactIdList() const contactIdList = this.padchatManager.getContactIdList()
return contactIdList return contactIdList
} }
...@@ -671,7 +671,7 @@ export class PuppetPadchat extends Puppet { ...@@ -671,7 +671,7 @@ export class PuppetPadchat extends Puppet {
log.verbose('PuppetPadchat', 'contactPayloadDirty(%s)', contactId) log.verbose('PuppetPadchat', 'contactPayloadDirty(%s)', contactId)
if (this.padchatManager) { if (this.padchatManager) {
await this.padchatManager.contactRawPayloadDirty(contactId) this.padchatManager.contactRawPayloadDirty(contactId)
} }
await super.contactPayloadDirty(contactId) await super.contactPayloadDirty(contactId)
...@@ -924,7 +924,7 @@ export class PuppetPadchat extends Puppet { ...@@ -924,7 +924,7 @@ export class PuppetPadchat extends Puppet {
log.verbose('PuppetPadchat', 'roomPayloadDirty(%s)', roomId) log.verbose('PuppetPadchat', 'roomPayloadDirty(%s)', roomId)
if (this.padchatManager) { if (this.padchatManager) {
await this.padchatManager.roomRawPayloadDirty(roomId) this.padchatManager.roomRawPayloadDirty(roomId)
} }
await super.roomPayloadDirty(roomId) await super.roomPayloadDirty(roomId)
......
...@@ -32,13 +32,13 @@ ...@@ -32,13 +32,13 @@
true, true,
180 180
], ],
"callable-types": true, "callable-types": true,
"interface-over-type-literal": true, "interface-over-type-literal": true,
"no-empty-interface": true, "no-empty-interface": true,
"no-string-throw": true, "no-string-throw": true,
"prefer-const": true, "prefer-const": true,
"unified-signatures": false, "unified-signatures": false,
"no-inferrable-types": [true, "ignore-params"], "no-inferrable-types": [true, "ignore-params"],
"member-access": true, "member-access": true,
"member-ordering": [false], "member-ordering": [false],
"no-any": false, "no-any": false,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册