refactor: useStreaming

This commit is contained in:
Tristan Yang
2023-07-27 19:17:24 +08:00
parent 2133c63d1f
commit 5a0842273b
3 changed files with 17 additions and 19 deletions
+11 -12
View File
@@ -1,6 +1,5 @@
import { useEffect } from "react"; import { useEffect } from "react";
import dayjs from "dayjs"; import dayjs from "dayjs";
import initCache, { useRehydrate } from "@/app/cache"; import initCache, { useRehydrate } from "@/app/cache";
import { useLazyGetFavoritesQuery, useLazyLoadMoreMessagesQuery } from "@/app/services/message"; import { useLazyGetFavoritesQuery, useLazyLoadMoreMessagesQuery } from "@/app/services/message";
import { useLazyGetServerVersionQuery, useLazyGetSystemCommonQuery } from "@/app/services/server"; import { useLazyGetServerVersionQuery, useLazyGetSystemCommonQuery } from "@/app/services/server";
@@ -9,15 +8,13 @@ import { useAppSelector } from "@/app/store";
import useLicense from "./useLicense"; import useLicense from "./useLicense";
import useStreaming from "./useStreaming"; import useStreaming from "./useStreaming";
// type Props={
// guest?:boolean
// }
let preloadChannelMsgs = false; let preloadChannelMsgs = false;
export default function usePreload() { export default function usePreload() {
const { isLoading: loadingLicense } = useLicense(false); const { isLoading: loadingLicense } = useLicense(false);
const [preloadChannelMessages] = useLazyLoadMoreMessagesQuery(); const [preloadChannelMessages] = useLazyLoadMoreMessagesQuery();
const { rehydrate, rehydrated } = useRehydrate(); const { rehydrate, rehydrated } = useRehydrate();
const { const {
ready,
loginUid, loginUid,
token, token,
isGuest, isGuest,
@@ -27,6 +24,7 @@ export default function usePreload() {
enableContacts enableContacts
} = useAppSelector((store) => { } = useAppSelector((store) => {
return { return {
ready: store.ui.ready,
channelIds: store.channels.ids, channelIds: store.channels.ids,
channelMessageData: store.channelMessage, channelMessageData: store.channelMessage,
loginUid: store.authData.user?.uid, loginUid: store.authData.user?.uid,
@@ -36,7 +34,7 @@ export default function usePreload() {
enableContacts: store.server.contact_verification_enable enableContacts: store.server.contact_verification_enable
}; };
}); });
const { setStreamingReady } = useStreaming(); const { startStreaming } = useStreaming();
const [ const [
getFavorites, getFavorites,
{ {
@@ -69,15 +67,14 @@ export default function usePreload() {
initCache(); initCache();
rehydrate(); rehydrate();
getServerVersion(); getServerVersion();
return () => { // return ()=>{
setStreamingReady(false); // stopStreaming()
}; // }
}, []); }, []);
// 在guest的时候 预取channel数据 // 在guest的时候 预取channel数据
useEffect(() => { useEffect(() => {
if (isGuest && channelIds.length > 0 && !preloadChannelMsgs) { if (isGuest && channelIds.length > 0 && !preloadChannelMsgs) {
const tmps = channelIds.filter((cid) => !channelMessageData[cid]); const tmps = channelIds.filter((cid) => !channelMessageData[cid]);
// console.log("preload", channelIds, tmps);
tmps.forEach((id) => { tmps.forEach((id) => {
if (id) { if (id) {
preloadChannelMessages({ id, limit: 50 }); preloadChannelMessages({ id, limit: 50 });
@@ -98,11 +95,13 @@ export default function usePreload() {
} }
}, [rehydrated, serverVersion, isGuest]); }, [rehydrated, serverVersion, isGuest]);
const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000)); const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000));
const canStreaming = !!loginUid && rehydrated && !!token && !tokenAlmostExpire; const canStreaming = !!loginUid && rehydrated && !!token && !tokenAlmostExpire && !ready;
// console.log("ttt", loginUid, rehydrated, token);
useEffect(() => { useEffect(() => {
setStreamingReady(canStreaming); console.log("tttt", canStreaming);
if (canStreaming) {
startStreaming();
}
}, [canStreaming]); }, [canStreaming]);
return { return {
loading: loading:
+2 -2
View File
@@ -32,7 +32,6 @@ function HomePage() {
loginUser: { uid: loginUid }, loginUser: { uid: loginUid },
guest, guest,
ui: { ui: {
ready,
rememberedNavs: { chat: chatPath, user: userPath } rememberedNavs: { chat: chatPath, user: userPath }
} }
} = useAppSelector((store) => { } = useAppSelector((store) => {
@@ -49,8 +48,9 @@ function HomePage() {
dispatch(updateRememberedNavs({ key: "chat", path: "/chat" })); dispatch(updateRememberedNavs({ key: "chat", path: "/chat" }));
} }
}, [isChatHomePath]); }, [isChatHomePath]);
console.log("ttss", success);
if (!success || !ready) { if (!success) {
return <Loading reload={true} fullscreen={true} />; return <Loading reload={true} fullscreen={true} />;
} }
const isSettingPage = pathname.startsWith("/setting"); const isSettingPage = pathname.startsWith("/setting");
+4 -5
View File
@@ -16,17 +16,16 @@ export default function useSSE() {
expireTime: store.authData.expireTime expireTime: store.authData.expireTime
}; };
}); });
const { setStreamingReady } = useStreaming(); const { startStreaming } = useStreaming();
const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000)); const tokenAlmostExpire = dayjs().isAfter(new Date(expireTime - 20 * 1000));
const canStreaming = !!loginUid && !!token && !tokenAlmostExpire; const canStreaming = !!loginUid && !!token && !tokenAlmostExpire;
// console.log("ttt", loginUid, rehydrated, token); // console.log("ttt", loginUid, rehydrated, token);
useEffect(() => { useEffect(() => {
setStreamingReady(canStreaming); if (canStreaming) {
return () => { startStreaming();
setStreamingReady(false); }
};
}, [canStreaming]); }, [canStreaming]);
return null; return null;