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 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:
+2 -2
View File
@@ -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 <Loading reload={true} fullscreen={true} />;
}
const isSettingPage = pathname.startsWith("/setting");
+4 -5
View File
@@ -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;