-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor ack logic to not fetch all acks but fetch only the…
… ones that are needed
- Loading branch information
1 parent
5c338b5
commit 14613cc
Showing
8 changed files
with
224 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useAppDispatch, useAppSelector } from './useAppSelector'; | ||
import { chatActions } from '../slices'; | ||
import { useEvent } from '../../hooks/useEvent'; | ||
import { getCachedAckInfo } from '../../cache/cache'; | ||
|
||
export function useAckInfoChecker() { | ||
const ack = useAppSelector((state) => state.chat.ack); | ||
const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap); | ||
const dispatch = useAppDispatch(); | ||
|
||
const ensureAckInfo = useEvent((converseId: string) => { | ||
if ( | ||
ack[converseId] === undefined || | ||
lastMessageMap[converseId] === undefined | ||
) { | ||
getCachedAckInfo(converseId).then((info) => { | ||
if (info.ack?.lastMessageId) { | ||
dispatch( | ||
chatActions.setConverseAck({ | ||
converseId, | ||
lastMessageId: info.ack.lastMessageId, | ||
}) | ||
); | ||
} | ||
|
||
if (info.lastMessage?.lastMessageId) { | ||
dispatch( | ||
chatActions.setLastMessageMap([ | ||
{ | ||
converseId, | ||
lastMessageId: info.lastMessage.lastMessageId, | ||
}, | ||
]) | ||
); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return { ensureAckInfo }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import { useAppSelector } from './useAppSelector'; | ||
import { useAckInfoChecker } from './useAckInfo'; | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* 返回某些会话是否有未读信息 | ||
*/ | ||
export function useUnread(converseIds: string[]) { | ||
const ack = useAppSelector((state) => state.chat.ack); | ||
const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap); | ||
const { ensureAckInfo } = useAckInfoChecker(); | ||
|
||
return converseIds.map((converseId) => { | ||
useEffect(() => { | ||
converseIds.forEach((converseId) => ensureAckInfo(converseId)); | ||
}, [converseIds]); | ||
|
||
const unreadList = converseIds.map((converseId) => { | ||
if ( | ||
ack[converseId] === undefined && | ||
lastMessageMap[converseId] !== undefined | ||
) { | ||
// 没有已读记录且远程有数据 | ||
// 远程没有已读记录且获取到了最后一条消息 | ||
return true; | ||
} | ||
|
||
// 当远端最后一条消息的id > 本地已读状态的最后一条消息id, | ||
// 则返回true(有未读消息) | ||
return lastMessageMap[converseId] > ack[converseId]; | ||
}); | ||
|
||
return unreadList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters