Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
76b145f6
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
76b145f6
编写于
6月 24, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "try to use file to save memory"
This reverts commit
4e56c04b
.
上级
3b34dfc8
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
81 addition
and
96 deletion
+81
-96
src/puppet-padchat/padchat-manager.ts
src/puppet-padchat/padchat-manager.ts
+78
-93
src/puppet-padchat/puppet-padchat.ts
src/puppet-padchat/puppet-padchat.ts
+3
-3
未找到文件。
src/puppet-padchat/padchat-manager.ts
浏览文件 @
76b145f6
...
...
@@ -4,7 +4,7 @@ import fs from 'fs-extra'
import
{
MemoryCard
}
from
'
memory-card
'
import
{
StateSwitch
}
from
'
state-switch
'
import
{
FlashStore
}
from
'
flash-store
'
import
{
FlashStore
Sync
}
from
'
flash-store
'
import
{
Subscription
,
}
from
'
rxjs
'
...
...
@@ -71,11 +71,11 @@ export class PadchatManager extends PadchatRpc {
private
userId
?:
string
private
storeContactRawPayload
?
:
FlashStore
<
string
,
PadchatContactPayload
>
private
storeRoomMemberRawPayload
?
:
FlashStore
<
string
,
{
private
cacheContactRawPayload
?
:
FlashStoreSync
<
string
,
PadchatContactPayload
>
private
cacheRoomMemberRawPayload
?
:
FlashStoreSync
<
string
,
{
[
contactId
:
string
]:
PadchatRoomMemberPayload
,
}
>
private
storeRoomRawPayload
?
:
FlashStore
<
string
,
PadchatRoomPayload
>
private
cacheRoomRawPayload
?
:
FlashStoreSync
<
string
,
PadchatRoomPayload
>
private
readonly
state
:
StateSwitch
private
readonly
delayQueueExecutor
:
DelayQueueExector
...
...
@@ -107,9 +107,9 @@ export class PadchatManager extends PadchatRpc {
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
initCache(%s, %s)
'
,
token
,
userId
)
if
(
this
.
stor
eContactRawPayload
||
this
.
stor
eRoomMemberRawPayload
||
this
.
stor
eRoomRawPayload
if
(
this
.
cach
eContactRawPayload
||
this
.
cach
eRoomMemberRawPayload
||
this
.
cach
eRoomRawPayload
)
{
throw
new
Error
(
'
cache exists
'
)
}
...
...
@@ -131,32 +131,27 @@ export class PadchatManager extends PadchatRpc {
await
fs
.
mkdirp
(
baseDir
)
}
this
.
storeContactRawPayload
=
new
FlashStore
(
path
.
join
(
baseDir
,
'
contact-raw-payload
'
))
this
.
storeRoomMemberRawPayload
=
new
FlashStore
(
path
.
join
(
baseDir
,
'
room-member-raw-payload
'
))
this
.
storeRoomRawPayload
=
new
FlashStore
(
path
.
join
(
baseDir
,
'
room-raw-payload
'
))
this
.
cacheContactRawPayload
=
new
FlashStoreSync
(
path
.
join
(
baseDir
,
'
contact-raw-payload
'
))
this
.
cacheRoomMemberRawPayload
=
new
FlashStoreSync
(
path
.
join
(
baseDir
,
'
room-member-raw-payload
'
))
this
.
cacheRoomRawPayload
=
new
FlashStoreSync
(
path
.
join
(
baseDir
,
'
room-raw-payload
'
))
//
await Promise.all([
// this.stor
eContactRawPayload.ready(),
// this.stor
eRoomMemberRawPayload.ready(),
// this.stor
eRoomRawPayload.ready(),
//
])
await
Promise
.
all
([
this
.
cach
eContactRawPayload
.
ready
(),
this
.
cach
eRoomMemberRawPayload
.
ready
(),
this
.
cach
eRoomRawPayload
.
ready
(),
])
let
roomMemberTotalNum
=
0
for
await
(
const
value
of
this
.
storeRoomMemberRawPayload
.
values
())
{
roomMemberTotalNum
+=
Object
.
keys
(
value
).
length
}
// const roomMemberTotalNum = [...this.storeRoomMemberRawPayload.values()].reduce(
// (accuVal, currVal) => {
// return accuVal + Object.keys(currVal).length
// },
// 0,
// )
const
roomMemberTotalNum
=
[...
this
.
cacheRoomMemberRawPayload
.
values
()].
reduce
(
(
accuVal
,
currVal
)
=>
{
return
accuVal
+
Object
.
keys
(
currVal
).
length
},
0
,
)
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
initCache() inited %d Contacts, %d RoomMembers, %d Rooms, cachedir="%s"
'
,
this
.
stor
eContactRawPayload
.
size
,
this
.
cach
eContactRawPayload
.
size
,
roomMemberTotalNum
,
this
.
stor
eRoomRawPayload
.
size
,
this
.
cach
eRoomRawPayload
.
size
,
baseDir
,
)
}
...
...
@@ -164,17 +159,17 @@ export class PadchatManager extends PadchatRpc {
protected
async
releaseCache
():
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
releaseCache()
'
)
if
(
this
.
stor
eContactRawPayload
&&
this
.
stor
eRoomMemberRawPayload
&&
this
.
stor
eRoomRawPayload
if
(
this
.
cach
eContactRawPayload
&&
this
.
cach
eRoomMemberRawPayload
&&
this
.
cach
eRoomRawPayload
)
{
await
this
.
stor
eContactRawPayload
.
close
(),
await
this
.
stor
eRoomMemberRawPayload
.
close
(),
await
this
.
stor
eRoomRawPayload
.
close
(),
await
this
.
cach
eContactRawPayload
.
close
(),
await
this
.
cach
eRoomMemberRawPayload
.
close
(),
await
this
.
cach
eRoomRawPayload
.
close
(),
this
.
stor
eContactRawPayload
=
undefined
this
.
stor
eRoomMemberRawPayload
=
undefined
this
.
stor
eRoomRawPayload
=
undefined
this
.
cach
eContactRawPayload
=
undefined
this
.
cach
eRoomMemberRawPayload
=
undefined
this
.
cach
eRoomRawPayload
=
undefined
log
.
silly
(
'
PuppetPadchatManager
'
,
'
releaseCache() cache closed.
'
)
}
else
{
...
...
@@ -282,8 +277,8 @@ export class PadchatManager extends PadchatRpc {
/**
* Refresh the login-ed user payload
*/
if
(
this
.
stor
eContactRawPayload
)
{
await
this
.
stor
eContactRawPayload
.
delete
(
this
.
userId
)
if
(
this
.
cach
eContactRawPayload
)
{
this
.
cach
eContactRawPayload
.
delete
(
this
.
userId
)
await
this
.
contactRawPayload
(
this
.
userId
)
}
...
...
@@ -558,7 +553,7 @@ export class PadchatManager extends PadchatRpc {
// wait 1 second and try again
await
new
Promise
(
r
=>
setTimeout
(
r
,
1000
))
return
this
.
emitLoginQrcode
()
return
await
this
.
emitLoginQrcode
()
}
const
fileBox
=
FileBox
.
fromBase64
(
result
.
qr_code
,
'
qrcode.jpg
'
)
...
...
@@ -676,42 +671,34 @@ export class PadchatManager extends PadchatRpc {
}
}
public
async
getContactIdList
():
Promise
<
string
[]
>
{
public
getContactIdList
():
string
[]
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getContactIdList()
'
)
if
(
!
this
.
stor
eContactRawPayload
)
{
if
(
!
this
.
cach
eContactRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
// const contactIdList = [...this.storeContactRawPayload.keys()]
const
contactIdList
:
string
[]
=
[]
for
await
(
const
contactId
of
this
.
storeContactRawPayload
.
keys
())
{
contactIdList
.
push
(
contactId
)
}
const
contactIdList
=
[...
this
.
cacheContactRawPayload
.
keys
()]
log
.
silly
(
'
PuppetPadchatManager
'
,
'
getContactIdList() = %d
'
,
contactIdList
.
length
)
return
contactIdList
}
public
async
getRoomIdList
():
Promise
<
string
[]
>
{
public
getRoomIdList
():
string
[]
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomIdList()
'
)
if
(
!
this
.
stor
eRoomRawPayload
)
{
if
(
!
this
.
cach
eRoomRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
// const roomIdList = [...this.storeRoomRawPayload.keys()]
const
roomIdList
:
string
[]
=
[]
for
await
(
const
roomId
of
this
.
storeRoomRawPayload
.
keys
())
{
roomIdList
.
push
(
roomId
)
}
const
roomIdList
=
[...
this
.
cacheRoomRawPayload
.
keys
()]
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomIdList()=%d
'
,
roomIdList
.
length
)
return
roomIdList
}
public
async
roomMemberRawPayloadDirty
(
public
roomMemberRawPayloadDirty
(
roomId
:
string
,
):
Promise
<
void
>
{
):
void
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomMemberRawPayloadDirty(%d)
'
,
roomId
)
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
await
this
.
stor
eRoomMemberRawPayload
.
delete
(
roomId
)
this
.
cach
eRoomMemberRawPayload
.
delete
(
roomId
)
}
public
async
getRoomMemberIdList
(
...
...
@@ -719,15 +706,15 @@ export class PadchatManager extends PadchatRpc {
dirty
=
false
,
):
Promise
<
string
[]
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomMemberIdList(%d)
'
,
roomId
)
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
if
(
dirty
)
{
await
this
.
roomMemberRawPayloadDirty
(
roomId
)
this
.
roomMemberRawPayloadDirty
(
roomId
)
}
const
memberRawPayloadDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
const
memberRawPayloadDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
||
await
this
.
syncRoomMember
(
roomId
)
if
(
!
memberRawPayloadDict
)
{
...
...
@@ -742,24 +729,24 @@ export class PadchatManager extends PadchatRpc {
return
memberIdList
}
public
async
roomRawPayloadDirty
(
public
roomRawPayloadDirty
(
roomId
:
string
,
):
Promise
<
void
>
{
):
void
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomRawPayloadDirty(%d)
'
,
roomId
)
if
(
!
this
.
stor
eRoomRawPayload
)
{
if
(
!
this
.
cach
eRoomRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
await
this
.
stor
eRoomRawPayload
.
delete
(
roomId
)
this
.
cach
eRoomRawPayload
.
delete
(
roomId
)
}
public
async
roomMemberRawPayload
(
roomId
:
string
):
Promise
<
{
[
contactId
:
string
]:
PadchatRoomMemberPayload
}
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomMemberRawPayload(%s)
'
,
roomId
)
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
memberRawPayloadDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
const
memberRawPayloadDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
||
await
this
.
syncRoomMember
(
roomId
)
if
(
!
memberRawPayloadDict
)
{
...
...
@@ -781,8 +768,8 @@ export class PadchatManager extends PadchatRpc {
* Room Id not exist
* See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016
*/
await
this
.
roomMemberRawPayloadDirty
(
roomId
)
await
this
.
roomRawPayloadDirty
(
roomId
)
this
.
roomMemberRawPayloadDirty
(
roomId
)
this
.
roomRawPayloadDirty
(
roomId
)
return
{}
...
...
@@ -800,16 +787,16 @@ export class PadchatManager extends PadchatRpc {
memberDict
[
contactId
]
=
memberPayload
}
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
oldMemberDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
const
oldMemberDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
const
newMemberDict
=
{
...
oldMemberDict
,
...
memberDict
,
}
await
this
.
stor
eRoomMemberRawPayload
.
set
(
roomId
,
newMemberDict
)
this
.
cach
eRoomMemberRawPayload
.
set
(
roomId
,
newMemberDict
)
return
newMemberDict
}
...
...
@@ -830,16 +817,16 @@ export class PadchatManager extends PadchatRpc {
continue
}
if
(
!
this
.
stor
eContactRawPayload
||
!
this
.
stor
eRoomRawPayload
if
(
!
this
.
cach
eContactRawPayload
||
!
this
.
cach
eRoomRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
log
.
silly
(
'
PuppetPadchatManager
'
,
'
syncContactsAndRooms() syncing %d out of Contact(%d) & Room(%d) ...
'
,
syncContactList
.
length
,
await
this
.
stor
eContactRawPayload
.
size
,
await
this
.
stor
eRoomRawPayload
.
size
,
this
.
cach
eContactRawPayload
.
size
,
this
.
cach
eRoomRawPayload
.
size
,
)
for
(
const
syncContact
of
syncContactList
)
{
...
...
@@ -862,7 +849,7 @@ export class PadchatManager extends PadchatRpc {
const
roomId
=
syncContact
.
user_name
const
roomPayload
=
syncContact
as
PadchatRoomPayload
await
this
.
stor
eRoomRawPayload
.
set
(
roomId
,
roomPayload
)
this
.
cach
eRoomRawPayload
.
set
(
roomId
,
roomPayload
)
/**
* Use delay queue executor to sync room:
...
...
@@ -885,7 +872,7 @@ export class PadchatManager extends PadchatRpc {
const
contactPayload
=
syncContact
as
PadchatContactPayload
const
contactId
=
contactPayload
.
user_name
await
this
.
stor
eContactRawPayload
.
set
(
contactId
,
contactPayload
)
this
.
cach
eContactRawPayload
.
set
(
contactId
,
contactPayload
)
}
else
{
throw
new
Error
(
'
id is neither room nor contact
'
)
...
...
@@ -909,14 +896,14 @@ export class PadchatManager extends PadchatRpc {
}
}
public
async
contactRawPayloadDirty
(
public
contactRawPayloadDirty
(
contactId
:
string
,
):
Promise
<
void
>
{
):
void
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
contactRawPayloadDirty(%d)
'
,
contactId
)
if
(
!
this
.
stor
eContactRawPayload
)
{
if
(
!
this
.
cach
eContactRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
await
this
.
stor
eContactRawPayload
.
delete
(
contactId
)
this
.
cach
eContactRawPayload
.
delete
(
contactId
)
}
public
async
contactRawPayload
(
contactId
:
string
):
Promise
<
PadchatContactPayload
>
{
...
...
@@ -925,12 +912,12 @@ export class PadchatManager extends PadchatRpc {
const
rawPayload
=
await
Misc
.
retry
(
async
(
retry
,
attempt
)
=>
{
log
.
silly
(
'
PuppetPadchatManager
'
,
'
contactRawPayload(%s) retry() attempt=%d
'
,
contactId
,
attempt
)
if
(
!
this
.
stor
eContactRawPayload
)
{
if
(
!
this
.
cach
eContactRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
if
(
this
.
stor
eContactRawPayload
.
has
(
contactId
))
{
return
this
.
stor
eContactRawPayload
.
get
(
contactId
)
if
(
this
.
cach
eContactRawPayload
.
has
(
contactId
))
{
return
this
.
cach
eContactRawPayload
.
get
(
contactId
)
}
const
tryRawPayload
=
await
this
.
WXGetContactPayload
(
contactId
)
...
...
@@ -938,7 +925,7 @@ export class PadchatManager extends PadchatRpc {
// check user_name too becasue the server might return {}
// See issue #1358 https://github.com/Chatie/wechaty/issues/1358
if
(
tryRawPayload
/* && tryRawPayload.user_name */
)
{
await
this
.
stor
eContactRawPayload
.
set
(
contactId
,
tryRawPayload
)
this
.
cach
eContactRawPayload
.
set
(
contactId
,
tryRawPayload
)
return
tryRawPayload
}
return
retry
(
new
Error
(
'
tryRawPayload empty
'
))
...
...
@@ -950,20 +937,18 @@ export class PadchatManager extends PadchatRpc {
return
rawPayload
}
public
async
roomRawPayload
(
id
:
string
,
):
Promise
<
PadchatRoomPayload
>
{
public
async
roomRawPayload
(
id
:
string
):
Promise
<
PadchatRoomPayload
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomRawPayload(%s)
'
,
id
)
const
rawPayload
=
await
Misc
.
retry
(
async
(
retry
,
attempt
)
=>
{
log
.
silly
(
'
PuppetPadchatManager
'
,
'
roomRawPayload(%s) retry() attempt=%d
'
,
id
,
attempt
)
if
(
!
this
.
stor
eRoomRawPayload
)
{
if
(
!
this
.
cach
eRoomRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
if
(
await
this
.
stor
eRoomRawPayload
.
has
(
id
))
{
return
this
.
stor
eRoomRawPayload
.
get
(
id
)
if
(
this
.
cach
eRoomRawPayload
.
has
(
id
))
{
return
this
.
cach
eRoomRawPayload
.
get
(
id
)
}
const
tryRawPayload
=
await
this
.
WXGetRoomPayload
(
id
)
...
...
@@ -971,7 +956,7 @@ export class PadchatManager extends PadchatRpc {
// check user_name too becasue the server might return {}
// See issue #1358 https://github.com/Chatie/wechaty/issues/1358
if
(
tryRawPayload
/* && tryRawPayload.user_name */
)
{
await
this
.
stor
eRoomRawPayload
.
set
(
id
,
tryRawPayload
)
this
.
cach
eRoomRawPayload
.
set
(
id
,
tryRawPayload
)
return
tryRawPayload
}
return
retry
(
new
Error
(
'
tryRawPayload empty
'
))
...
...
src/puppet-padchat/puppet-padchat.ts
浏览文件 @
76b145f6
...
...
@@ -602,7 +602,7 @@ export class PuppetPadchat extends Puppet {
throw
new
Error
(
'
no padchat manager
'
)
}
const
contactIdList
=
await
this
.
padchatManager
.
getContactIdList
()
const
contactIdList
=
this
.
padchatManager
.
getContactIdList
()
return
contactIdList
}
...
...
@@ -671,7 +671,7 @@ export class PuppetPadchat extends Puppet {
log
.
verbose
(
'
PuppetPadchat
'
,
'
contactPayloadDirty(%s)
'
,
contactId
)
if
(
this
.
padchatManager
)
{
await
this
.
padchatManager
.
contactRawPayloadDirty
(
contactId
)
this
.
padchatManager
.
contactRawPayloadDirty
(
contactId
)
}
await
super
.
contactPayloadDirty
(
contactId
)
...
...
@@ -924,7 +924,7 @@ export class PuppetPadchat extends Puppet {
log
.
verbose
(
'
PuppetPadchat
'
,
'
roomPayloadDirty(%s)
'
,
roomId
)
if
(
this
.
padchatManager
)
{
await
this
.
padchatManager
.
roomRawPayloadDirty
(
roomId
)
this
.
padchatManager
.
roomRawPayloadDirty
(
roomId
)
}
await
super
.
roomPayloadDirty
(
roomId
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录