refactor: app store and cache

This commit is contained in:
zerosoul
2022-03-18 16:30:38 +08:00
parent ad994f1eaf
commit 7bb8e9a914
26 changed files with 280 additions and 908 deletions
+7 -32
View File
@@ -1,16 +1,13 @@
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import { useEffect } from "react";
import { useSelector } from "react-redux";
// import { useNavigate } from "react-router-dom";
import initCache, { useRehydrate } from "../../app/cache";
import { useLazyGetContactsQuery } from "../../app/services/contact";
// import { useLazyInitStreamingQuery } from "../../app/services/streaming";
import { resetAuthData, setUid } from "../../app/slices/auth.data";
import { fullfillContacts } from "../../app/slices/contacts";
// import { useGetChannelsQuery } from "../../app/services/channel";
import { useLazyGetServerQuery } from "../../app/services/server";
import useStreaming from "../../common/hook/useStreaming";
import { KEY_UID } from "../../app/config";
// pollingInterval: 0,
// const querySetting = {
// refetchOnMountOrArgChange: true,
@@ -19,9 +16,7 @@ let request = null;
export default function usePreload() {
const { rehydrate, rehydrated } = useRehydrate();
// const [initStreaming, { isLoading: streaming }] = useLazyInitStreamingQuery();
const [checked, setChecked] = useState(false);
const dispatch = useDispatch();
const navigate = useNavigate();
// const navigate = useNavigate();
const store = useSelector((store) => store);
const { startStreaming, streaming, initializing } = useStreaming();
const [
@@ -60,34 +55,14 @@ export default function usePreload() {
}
}, [rehydrated]);
useEffect(() => {
if (checked && rehydrated && !initializing && !streaming) {
if (rehydrated && !initializing && !streaming) {
request = startStreaming(store);
}
}, [checked, rehydrated, store, streaming, initializing]);
}, [rehydrated, store, streaming, initializing]);
useEffect(() => {
const local_uid = localStorage.getItem(KEY_UID);
if (contacts) {
const matchedUser = contacts.find((c) => c.uid == local_uid);
console.log("wtf", contacts, matchedUser);
if (!matchedUser) {
// 用户已注销或被禁用
console.log("no matched user, redirect to login");
dispatch(resetAuthData());
navigate("/login");
} else {
const markedContacts = contacts.map((u) => {
return u.uid == matchedUser.uid ? { ...u, online: true } : u;
});
dispatch(setUid(matchedUser.uid));
dispatch(fullfillContacts(markedContacts));
setChecked(true);
}
}
}, [contacts]);
// console.log("loading", contactsLoading, serverLoading, !checked);
return {
loading: contactsLoading || serverLoading || !checked || !rehydrated,
loading: contactsLoading || serverLoading || !rehydrated,
error: contactsError && serverError,
success: contactsSuccess && serverSuccess,
data: {