From 3f305513ae0029532bfc930f43f9616ed57ac48b Mon Sep 17 00:00:00 2001 From: zhanglun Date: Fri, 20 Dec 2024 18:04:08 +0800 Subject: [PATCH] improvement: refactor useRefresh --- src/components/Subscribes/index.tsx | 2 +- src/components/Subscribes/useRefresh.tsx | 25 +++++++++++++++++------- src/layout/Local/index.tsx | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/components/Subscribes/index.tsx b/src/components/Subscribes/index.tsx index 64484206a..c500bfad4 100644 --- a/src/components/Subscribes/index.tsx +++ b/src/components/Subscribes/index.tsx @@ -54,7 +54,7 @@ const ChannelList = (): JSX.Element => { const [deleteFolderStatus, setDeleteFolderStatus] = useModal(); const [editFeedStatus, setEditFeedStatus] = useModal(); const [showStatus, setModalStatus] = useModal(); - const [feedList, setFeedList, getSubscribes, refreshing, setRefreshing, done, setDone, startRefresh] = useRefresh(); + const { subscribes: feedList, getSubscribes, refreshing, done } = useRefresh(); const store = useBearStore((state) => ({ feed: state.feed, setFeed: state.setFeed, diff --git a/src/components/Subscribes/useRefresh.tsx b/src/components/Subscribes/useRefresh.tsx index 893fc0190..47a20900b 100644 --- a/src/components/Subscribes/useRefresh.tsx +++ b/src/components/Subscribes/useRefresh.tsx @@ -54,13 +54,25 @@ export const useRefresh = () => { setRefreshing(true); store.getUserConfig().then((config: UserConfig) => { - console.log("set last sync time"); + if (!config) return; + store.setLastSyncTime(new Date()); const { threads = 5 } = config; const limit = pLimit(threads); + const errors = []; const fns = (store.subscribes || []).map((channel: any) => { - return limit(() => loadAndUpdate(channel)); + return limit(() => { + try { + return loadAndUpdate(channel); + } catch (err) { + errors.push({ + channel, + error: err, + }); + return Promise.resolve(); // 继续执行下一个请求 + } + }); }); Promise.all(fns) @@ -91,7 +103,7 @@ export const useRefresh = () => { console.log("%c Line:93 🥕 subscribes", "color:#33a5ff", store.subscribes); if (!store.subscribes || store.subscribes.length === 0) { - return ; + return; } // if (!store.userConfig.last_sync_time) { @@ -115,14 +127,13 @@ export const useRefresh = () => { }; }, [store.subscribes, store.userConfig.update_interval]); - return [ - store.subscribes, - store.getSubscribes, + return { + subscribes: store.subscribes, getSubscribes, refreshing, setRefreshing, done, setDone, startRefresh, - ] as const; + } as const; }; diff --git a/src/layout/Local/index.tsx b/src/layout/Local/index.tsx index 3790b057a..1fb47d639 100644 --- a/src/layout/Local/index.tsx +++ b/src/layout/Local/index.tsx @@ -36,7 +36,7 @@ export function LocalPage() { feed: state.feed, updateSettingDialogStatus: state.updateSettingDialogStatus, })); - const [feedList, setFeedList, getSubscribes, refreshing, setRefreshing, done, setDone, startRefresh] = useRefresh(); + const { getSubscribes, refreshing, startRefresh } = useRefresh(); const [addFolderDialogStatus, setAddFolderDialogStatus] = useModal(); useEffect(() => {