Skip to content

Commit

Permalink
improvement: refactor useRefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglun committed Dec 20, 2024
1 parent 0e7b2bf commit 3f30551
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/Subscribes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 18 additions & 7 deletions src/components/Subscribes/useRefresh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
};
2 changes: 1 addition & 1 deletion src/layout/Local/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit 3f30551

Please sign in to comment.