Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
4e56c04b
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,发现更多精彩内容 >>
提交
4e56c04b
编写于
6月 24, 2018
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
try to use file to save memory
上级
c9b689bd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
103 addition
and
88 deletion
+103
-88
src/puppet-padchat/padchat-manager.ts
src/puppet-padchat/padchat-manager.ts
+93
-78
src/puppet-padchat/puppet-padchat.ts
src/puppet-padchat/puppet-padchat.ts
+3
-3
tslint.json
tslint.json
+7
-7
未找到文件。
src/puppet-padchat/padchat-manager.ts
浏览文件 @
4e56c04b
...
...
@@ -4,7 +4,7 @@ import fs from 'fs-extra'
import
{
MemoryCard
}
from
'
memory-card
'
import
{
StateSwitch
}
from
'
state-switch
'
import
{
FlashStore
Sync
}
from
'
flash-store
'
import
{
FlashStore
}
from
'
flash-store
'
import
{
Subscription
,
}
from
'
rxjs
'
...
...
@@ -71,11 +71,11 @@ export class PadchatManager extends PadchatRpc {
private
userId
?:
string
private
cacheContactRawPayload
?
:
FlashStoreSync
<
string
,
PadchatContactPayload
>
private
cacheRoomMemberRawPayload
?
:
FlashStoreSync
<
string
,
{
private
storeContactRawPayload
?
:
FlashStore
<
string
,
PadchatContactPayload
>
private
storeRoomMemberRawPayload
?
:
FlashStore
<
string
,
{
[
contactId
:
string
]:
PadchatRoomMemberPayload
,
}
>
private
cacheRoomRawPayload
?
:
FlashStoreSync
<
string
,
PadchatRoomPayload
>
private
storeRoomRawPayload
?
:
FlashStore
<
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
.
cach
eContactRawPayload
||
this
.
cach
eRoomMemberRawPayload
||
this
.
cach
eRoomRawPayload
if
(
this
.
stor
eContactRawPayload
||
this
.
stor
eRoomMemberRawPayload
||
this
.
stor
eRoomRawPayload
)
{
throw
new
Error
(
'
cache exists
'
)
}
...
...
@@ -131,27 +131,32 @@ export class PadchatManager extends PadchatRpc {
await
fs
.
mkdirp
(
baseDir
)
}
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
'
))
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
'
))
await
Promise
.
all
([
this
.
cach
eContactRawPayload
.
ready
(),
this
.
cach
eRoomMemberRawPayload
.
ready
(),
this
.
cach
eRoomRawPayload
.
ready
(),
])
//
await Promise.all([
// this.stor
eContactRawPayload.ready(),
// this.stor
eRoomMemberRawPayload.ready(),
// this.stor
eRoomRawPayload.ready(),
//
])
const
roomMemberTotalNum
=
[...
this
.
cacheRoomMemberRawPayload
.
values
()].
reduce
(
(
accuVal
,
currVal
)
=>
{
return
accuVal
+
Object
.
keys
(
currVal
).
length
},
0
,
)
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,
// )
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
initCache() inited %d Contacts, %d RoomMembers, %d Rooms, cachedir="%s"
'
,
this
.
cach
eContactRawPayload
.
size
,
this
.
stor
eContactRawPayload
.
size
,
roomMemberTotalNum
,
this
.
cach
eRoomRawPayload
.
size
,
this
.
stor
eRoomRawPayload
.
size
,
baseDir
,
)
}
...
...
@@ -159,17 +164,17 @@ export class PadchatManager extends PadchatRpc {
protected
async
releaseCache
():
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
releaseCache()
'
)
if
(
this
.
cach
eContactRawPayload
&&
this
.
cach
eRoomMemberRawPayload
&&
this
.
cach
eRoomRawPayload
if
(
this
.
stor
eContactRawPayload
&&
this
.
stor
eRoomMemberRawPayload
&&
this
.
stor
eRoomRawPayload
)
{
await
this
.
cach
eContactRawPayload
.
close
(),
await
this
.
cach
eRoomMemberRawPayload
.
close
(),
await
this
.
cach
eRoomRawPayload
.
close
(),
await
this
.
stor
eContactRawPayload
.
close
(),
await
this
.
stor
eRoomMemberRawPayload
.
close
(),
await
this
.
stor
eRoomRawPayload
.
close
(),
this
.
cach
eContactRawPayload
=
undefined
this
.
cach
eRoomMemberRawPayload
=
undefined
this
.
cach
eRoomRawPayload
=
undefined
this
.
stor
eContactRawPayload
=
undefined
this
.
stor
eRoomMemberRawPayload
=
undefined
this
.
stor
eRoomRawPayload
=
undefined
log
.
silly
(
'
PuppetPadchatManager
'
,
'
releaseCache() cache closed.
'
)
}
else
{
...
...
@@ -277,8 +282,8 @@ export class PadchatManager extends PadchatRpc {
/**
* Refresh the login-ed user payload
*/
if
(
this
.
cach
eContactRawPayload
)
{
this
.
cach
eContactRawPayload
.
delete
(
this
.
userId
)
if
(
this
.
stor
eContactRawPayload
)
{
await
this
.
stor
eContactRawPayload
.
delete
(
this
.
userId
)
await
this
.
contactRawPayload
(
this
.
userId
)
}
...
...
@@ -553,7 +558,7 @@ export class PadchatManager extends PadchatRpc {
// wait 1 second and try again
await
new
Promise
(
r
=>
setTimeout
(
r
,
1000
))
return
await
this
.
emitLoginQrcode
()
return
this
.
emitLoginQrcode
()
}
const
fileBox
=
FileBox
.
fromBase64
(
result
.
qr_code
,
'
qrcode.jpg
'
)
...
...
@@ -671,34 +676,42 @@ export class PadchatManager extends PadchatRpc {
}
}
public
getContactIdList
():
string
[]
{
public
async
getContactIdList
():
Promise
<
string
[]
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getContactIdList()
'
)
if
(
!
this
.
cach
eContactRawPayload
)
{
if
(
!
this
.
stor
eContactRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
contactIdList
=
[...
this
.
cacheContactRawPayload
.
keys
()]
// const contactIdList = [...this.storeContactRawPayload.keys()]
const
contactIdList
:
string
[]
=
[]
for
await
(
const
contactId
of
this
.
storeContactRawPayload
.
keys
())
{
contactIdList
.
push
(
contactId
)
}
log
.
silly
(
'
PuppetPadchatManager
'
,
'
getContactIdList() = %d
'
,
contactIdList
.
length
)
return
contactIdList
}
public
getRoomIdList
():
string
[]
{
public
async
getRoomIdList
():
Promise
<
string
[]
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomIdList()
'
)
if
(
!
this
.
cach
eRoomRawPayload
)
{
if
(
!
this
.
stor
eRoomRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
roomIdList
=
[...
this
.
cacheRoomRawPayload
.
keys
()]
// const roomIdList = [...this.storeRoomRawPayload.keys()]
const
roomIdList
:
string
[]
=
[]
for
await
(
const
roomId
of
this
.
storeRoomRawPayload
.
keys
())
{
roomIdList
.
push
(
roomId
)
}
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomIdList()=%d
'
,
roomIdList
.
length
)
return
roomIdList
}
public
roomMemberRawPayloadDirty
(
public
async
roomMemberRawPayloadDirty
(
roomId
:
string
,
):
void
{
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomMemberRawPayloadDirty(%d)
'
,
roomId
)
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
this
.
cach
eRoomMemberRawPayload
.
delete
(
roomId
)
await
this
.
stor
eRoomMemberRawPayload
.
delete
(
roomId
)
}
public
async
getRoomMemberIdList
(
...
...
@@ -706,15 +719,15 @@ export class PadchatManager extends PadchatRpc {
dirty
=
false
,
):
Promise
<
string
[]
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
getRoomMemberIdList(%d)
'
,
roomId
)
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
if
(
dirty
)
{
this
.
roomMemberRawPayloadDirty
(
roomId
)
await
this
.
roomMemberRawPayloadDirty
(
roomId
)
}
const
memberRawPayloadDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
const
memberRawPayloadDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
||
await
this
.
syncRoomMember
(
roomId
)
if
(
!
memberRawPayloadDict
)
{
...
...
@@ -729,24 +742,24 @@ export class PadchatManager extends PadchatRpc {
return
memberIdList
}
public
roomRawPayloadDirty
(
public
async
roomRawPayloadDirty
(
roomId
:
string
,
):
void
{
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomRawPayloadDirty(%d)
'
,
roomId
)
if
(
!
this
.
cach
eRoomRawPayload
)
{
if
(
!
this
.
stor
eRoomRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
this
.
cach
eRoomRawPayload
.
delete
(
roomId
)
await
this
.
stor
eRoomRawPayload
.
delete
(
roomId
)
}
public
async
roomMemberRawPayload
(
roomId
:
string
):
Promise
<
{
[
contactId
:
string
]:
PadchatRoomMemberPayload
}
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
roomMemberRawPayload(%s)
'
,
roomId
)
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
memberRawPayloadDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
const
memberRawPayloadDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
||
await
this
.
syncRoomMember
(
roomId
)
if
(
!
memberRawPayloadDict
)
{
...
...
@@ -768,8 +781,8 @@ export class PadchatManager extends PadchatRpc {
* Room Id not exist
* See: https://github.com/lijiarui/wechaty-puppet-padchat/issues/64#issuecomment-397319016
*/
this
.
roomMemberRawPayloadDirty
(
roomId
)
this
.
roomRawPayloadDirty
(
roomId
)
await
this
.
roomMemberRawPayloadDirty
(
roomId
)
await
this
.
roomRawPayloadDirty
(
roomId
)
return
{}
...
...
@@ -787,16 +800,16 @@ export class PadchatManager extends PadchatRpc {
memberDict
[
contactId
]
=
memberPayload
}
if
(
!
this
.
cach
eRoomMemberRawPayload
)
{
if
(
!
this
.
stor
eRoomMemberRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
const
oldMemberDict
=
this
.
cach
eRoomMemberRawPayload
.
get
(
roomId
)
const
oldMemberDict
=
await
this
.
stor
eRoomMemberRawPayload
.
get
(
roomId
)
const
newMemberDict
=
{
...
oldMemberDict
,
...
memberDict
,
}
this
.
cach
eRoomMemberRawPayload
.
set
(
roomId
,
newMemberDict
)
await
this
.
stor
eRoomMemberRawPayload
.
set
(
roomId
,
newMemberDict
)
return
newMemberDict
}
...
...
@@ -817,16 +830,16 @@ export class PadchatManager extends PadchatRpc {
continue
}
if
(
!
this
.
cach
eContactRawPayload
||
!
this
.
cach
eRoomRawPayload
if
(
!
this
.
stor
eContactRawPayload
||
!
this
.
stor
eRoomRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
log
.
silly
(
'
PuppetPadchatManager
'
,
'
syncContactsAndRooms() syncing %d out of Contact(%d) & Room(%d) ...
'
,
syncContactList
.
length
,
this
.
cach
eContactRawPayload
.
size
,
this
.
cach
eRoomRawPayload
.
size
,
await
this
.
stor
eContactRawPayload
.
size
,
await
this
.
stor
eRoomRawPayload
.
size
,
)
for
(
const
syncContact
of
syncContactList
)
{
...
...
@@ -849,7 +862,7 @@ export class PadchatManager extends PadchatRpc {
const
roomId
=
syncContact
.
user_name
const
roomPayload
=
syncContact
as
PadchatRoomPayload
this
.
cach
eRoomRawPayload
.
set
(
roomId
,
roomPayload
)
await
this
.
stor
eRoomRawPayload
.
set
(
roomId
,
roomPayload
)
/**
* Use delay queue executor to sync room:
...
...
@@ -872,7 +885,7 @@ export class PadchatManager extends PadchatRpc {
const
contactPayload
=
syncContact
as
PadchatContactPayload
const
contactId
=
contactPayload
.
user_name
this
.
cach
eContactRawPayload
.
set
(
contactId
,
contactPayload
)
await
this
.
stor
eContactRawPayload
.
set
(
contactId
,
contactPayload
)
}
else
{
throw
new
Error
(
'
id is neither room nor contact
'
)
...
...
@@ -896,14 +909,14 @@ export class PadchatManager extends PadchatRpc {
}
}
public
contactRawPayloadDirty
(
public
async
contactRawPayloadDirty
(
contactId
:
string
,
):
void
{
):
Promise
<
void
>
{
log
.
verbose
(
'
PuppetPadchatManager
'
,
'
contactRawPayloadDirty(%d)
'
,
contactId
)
if
(
!
this
.
cach
eContactRawPayload
)
{
if
(
!
this
.
stor
eContactRawPayload
)
{
throw
new
Error
(
'
cache not inited
'
)
}
this
.
cach
eContactRawPayload
.
delete
(
contactId
)
await
this
.
stor
eContactRawPayload
.
delete
(
contactId
)
}
public
async
contactRawPayload
(
contactId
:
string
):
Promise
<
PadchatContactPayload
>
{
...
...
@@ -912,12 +925,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
.
cach
eContactRawPayload
)
{
if
(
!
this
.
stor
eContactRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
if
(
this
.
cach
eContactRawPayload
.
has
(
contactId
))
{
return
this
.
cach
eContactRawPayload
.
get
(
contactId
)
if
(
this
.
stor
eContactRawPayload
.
has
(
contactId
))
{
return
this
.
stor
eContactRawPayload
.
get
(
contactId
)
}
const
tryRawPayload
=
await
this
.
WXGetContactPayload
(
contactId
)
...
...
@@ -925,7 +938,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 */
)
{
this
.
cach
eContactRawPayload
.
set
(
contactId
,
tryRawPayload
)
await
this
.
stor
eContactRawPayload
.
set
(
contactId
,
tryRawPayload
)
return
tryRawPayload
}
return
retry
(
new
Error
(
'
tryRawPayload empty
'
))
...
...
@@ -937,18 +950,20 @@ 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
.
cach
eRoomRawPayload
)
{
if
(
!
this
.
stor
eRoomRawPayload
)
{
throw
new
Error
(
'
no cache
'
)
}
if
(
this
.
cach
eRoomRawPayload
.
has
(
id
))
{
return
this
.
cach
eRoomRawPayload
.
get
(
id
)
if
(
await
this
.
stor
eRoomRawPayload
.
has
(
id
))
{
return
this
.
stor
eRoomRawPayload
.
get
(
id
)
}
const
tryRawPayload
=
await
this
.
WXGetRoomPayload
(
id
)
...
...
@@ -956,7 +971,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 */
)
{
this
.
cach
eRoomRawPayload
.
set
(
id
,
tryRawPayload
)
await
this
.
stor
eRoomRawPayload
.
set
(
id
,
tryRawPayload
)
return
tryRawPayload
}
return
retry
(
new
Error
(
'
tryRawPayload empty
'
))
...
...
src/puppet-padchat/puppet-padchat.ts
浏览文件 @
4e56c04b
...
...
@@ -602,7 +602,7 @@ export class PuppetPadchat extends Puppet {
throw
new
Error
(
'
no padchat manager
'
)
}
const
contactIdList
=
this
.
padchatManager
.
getContactIdList
()
const
contactIdList
=
await
this
.
padchatManager
.
getContactIdList
()
return
contactIdList
}
...
...
@@ -671,7 +671,7 @@ export class PuppetPadchat extends Puppet {
log
.
verbose
(
'
PuppetPadchat
'
,
'
contactPayloadDirty(%s)
'
,
contactId
)
if
(
this
.
padchatManager
)
{
this
.
padchatManager
.
contactRawPayloadDirty
(
contactId
)
await
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
)
{
this
.
padchatManager
.
roomRawPayloadDirty
(
roomId
)
await
this
.
padchatManager
.
roomRawPayloadDirty
(
roomId
)
}
await
super
.
roomPayloadDirty
(
roomId
)
...
...
tslint.json
浏览文件 @
4e56c04b
...
...
@@ -32,13 +32,13 @@
true
,
180
],
"callable-types"
:
true
,
"interface-over-type-literal"
:
true
,
"no-empty-interface"
:
true
,
"no-string-throw"
:
true
,
"prefer-const"
:
true
,
"unified-signatures"
:
false
,
"no-inferrable-types"
:
[
true
,
"ignore-params"
],
"callable-types"
:
true
,
"interface-over-type-literal"
:
true
,
"no-empty-interface"
:
true
,
"no-string-throw"
:
true
,
"prefer-const"
:
true
,
"unified-signatures"
:
false
,
"no-inferrable-types"
:
[
true
,
"ignore-params"
],
"member-access"
:
true
,
"member-ordering"
:
[
false
],
"no-any"
:
false
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录