diff --git a/lib/selectors/chat-selectors.js b/lib/selectors/chat-selectors.js --- a/lib/selectors/chat-selectors.js +++ b/lib/selectors/chat-selectors.js @@ -79,14 +79,14 @@ ); } -type CreatedChatThreadItem = { +type ChatThreadItemLoader = { +threadInfo: ThreadInfo, +lastUpdatedAtLeastTimeIncludingSidebars: number, +lastUpdatedAtMostTimeIncludingSidebars: number, +initialChatThreadItem: ChatThreadItem, +getFinalChatThreadItem: () => Promise, }; -function useCreateChatThreadItem(): ThreadInfo => CreatedChatThreadItem { +function useGetChatThreadItemLoader(): ThreadInfo => ChatThreadItemLoader { const messageInfos = useSelector(messageInfoSelector); const sidebarInfos = useSidebarInfos(); const messageStore = useSelector(state => state.messageStore); @@ -189,40 +189,35 @@ function useChatThreadItems( threadInfos: $ReadOnlyArray, ): $ReadOnlyArray { - const getChatThreadItem = useCreateChatThreadItem(); - const createdChatThreadItems = React.useMemo( - () => threadInfos.map(getChatThreadItem), - [threadInfos, getChatThreadItem], + const getChatThreadItemLoader = useGetChatThreadItemLoader(); + const chatThreadItemLoaders = React.useMemo( + () => threadInfos.map(getChatThreadItemLoader), + [threadInfos, getChatThreadItemLoader], ); const initialChatThreadItems = React.useMemo( - () => - createdChatThreadItems.map( - createdChatThreadItem => createdChatThreadItem.initialChatThreadItem, - ), - [createdChatThreadItems], + () => chatThreadItemLoaders.map(loader => loader.initialChatThreadItem), + [chatThreadItemLoaders], ); const [chatThreadItems, setChatThreadItems] = React.useState< $ReadOnlyArray, >(initialChatThreadItems); - const prevCreatedChatThreadItemsRef = React.useRef(createdChatThreadItems); + const prevChatThreadItemLoadersRef = React.useRef(chatThreadItemLoaders); React.useEffect(() => { - if (createdChatThreadItems === prevCreatedChatThreadItemsRef.current) { + if (chatThreadItemLoaders === prevChatThreadItemLoadersRef.current) { return; } - prevCreatedChatThreadItemsRef.current = createdChatThreadItems; + prevChatThreadItemLoadersRef.current = chatThreadItemLoaders; setChatThreadItems(initialChatThreadItems); void (async () => { const finalChatThreadItems = await Promise.all( - createdChatThreadItems.map(createdChatThreadItem => - createdChatThreadItem.getFinalChatThreadItem(), - ), + chatThreadItemLoaders.map(loader => loader.getFinalChatThreadItem()), ); - if (createdChatThreadItems !== prevCreatedChatThreadItemsRef.current) { + if (chatThreadItemLoaders !== prevChatThreadItemLoadersRef.current) { // If these aren't equal, it indicates that the effect has fired again. // We should discard this result as it is now outdated. return; @@ -290,7 +285,7 @@ } }); })(); - }, [createdChatThreadItems, initialChatThreadItems]); + }, [chatThreadItemLoaders, initialChatThreadItems]); return React.useMemo(() => sortFunc(chatThreadItems), [chatThreadItems]); }