From 5a0842273bf160a58d5376829e2515e769fdfe92 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Thu, 27 Jul 2023 19:17:24 +0800 Subject: [PATCH] refactor: useStreaming --- src/hooks/usePreload.ts | 23 +++++++++++------------ src/routes/home/index.tsx | 4 ++-- src/widget/useSSE.ts | 9 ++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/hooks/usePreload.ts b/src/hooks/usePreload.ts index f8339505..f93014b1 100644 --- a/src/hooks/usePreload.ts +++ b/src/hooks/usePreload.ts @@ -1,6 +1,5 @@ import { useEffect } from "react"; import dayjs from "dayjs"; - import initCache, { useRehydrate } from "@/app/cache"; import { useLazyGetFavoritesQuery, useLazyLoadMoreMessagesQuery } from "@/app/services/message"; import { useLazyGetServerVersionQuery, useLazyGetSystemCommonQuery } from "@/app/services/server"; @@ -9,15 +8,13 @@ import { useAppSelector } from "@/app/store"; import useLicense from "./useLicense"; import useStreaming from "./useStreaming"; -// type Props={ -// guest?:boolean -// } let preloadChannelMsgs = false; export default function usePreload() { const { isLoading: loadingLicense } = useLicense(false); const [preloadChannelMessages] = useLazyLoadMoreMessagesQuery(); const { rehydrate, rehydrated } = useRehydrate(); const { + ready, loginUid, token, isGuest, @@ -27,6 +24,7 @@ export default function usePreload() { enableContacts } = useAppSelector((store) => { return { + ready: store.ui.ready, channelIds: store.channels.ids, channelMessageData: store.channelMessage, loginUid: store.authData.user?.uid, @@ -36,7 +34,7 @@ export default function usePreload() { enableContacts: store.server.contact_verification_enable }; }); - const { setStreamingReady } = useStreaming(); + const { startStreaming } = useStreaming(); const [ getFavorites, { @@ -69,15 +67,14 @@ export default function usePreload() { initCache(); rehydrate(); getServerVersion(); - return () => { - setStreamingReady(false); - }; + // return ()=>{ + // stopStreaming() + // } }, []); // 在guest的时候 预取channel数据 useEffect(() => { if (isGuest && channelIds.length > 0 && !preloadChannelMsgs) { const tmps = channelIds.filter((cid) => !channelMessageData[cid]); - // console.log("preload", channelIds, tmps); tmps.forEach((id) => { if (id) { preloadChannelMessages({ id, limit: 50 }); @@ -98,11 +95,13 @@ export default function usePreload() { } }, [rehydrated, serverVersion, isGuest]); const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000)); - const canStreaming = !!loginUid && rehydrated && !!token && !tokenAlmostExpire; - // console.log("ttt", loginUid, rehydrated, token); + const canStreaming = !!loginUid && rehydrated && !!token && !tokenAlmostExpire && !ready; useEffect(() => { - setStreamingReady(canStreaming); + console.log("tttt", canStreaming); + if (canStreaming) { + startStreaming(); + } }, [canStreaming]); return { loading: diff --git a/src/routes/home/index.tsx b/src/routes/home/index.tsx index 1090f109..0e9d2573 100644 --- a/src/routes/home/index.tsx +++ b/src/routes/home/index.tsx @@ -32,7 +32,6 @@ function HomePage() { loginUser: { uid: loginUid }, guest, ui: { - ready, rememberedNavs: { chat: chatPath, user: userPath } } } = useAppSelector((store) => { @@ -49,8 +48,9 @@ function HomePage() { dispatch(updateRememberedNavs({ key: "chat", path: "/chat" })); } }, [isChatHomePath]); + console.log("ttss", success); - if (!success || !ready) { + if (!success) { return ; } const isSettingPage = pathname.startsWith("/setting"); diff --git a/src/widget/useSSE.ts b/src/widget/useSSE.ts index 4518675f..8463124c 100644 --- a/src/widget/useSSE.ts +++ b/src/widget/useSSE.ts @@ -16,17 +16,16 @@ export default function useSSE() { expireTime: store.authData.expireTime }; }); - const { setStreamingReady } = useStreaming(); + const { startStreaming } = useStreaming(); const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000)); const canStreaming = !!loginUid && !!token && !tokenAlmostExpire; // console.log("ttt", loginUid, rehydrated, token); useEffect(() => { - setStreamingReady(canStreaming); - return () => { - setStreamingReady(false); - }; + if (canStreaming) { + startStreaming(); + } }, [canStreaming]); return null;