improve: notification count uses Promise.all instead of waterfall

This commit is contained in:
James Xylight committed 2025-02-16 14:59:08 -08:00
1 parent 0d6d7599aa
commit a8669e7c45
1 file changed
+31 -27
+31 -27
View File
@@ -173,7 +173,10 @@ $effect.root(() => {
}) })
$effect(() => { $effect(() => {
if (profile.data.id || profileData) fetchUserData(profile).then((res) => {}) if (profile.data.id || profileData)
fetchUserData(profile).then((res) => {
checkInbox()
})
}) })
}) })
@@ -319,37 +322,38 @@ export function moveProfile(id: number, up: boolean) {
} }
async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) { async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) {
const unreads = await getClient().getUnreadCount() const unreadsPromise = getClient()
.getUnreadCount()
.then((res) => res.mentions + res.private_messages + res.replies)
.catch(() => 0)
let reports: number = 0 const reportsPromise = mod
let applications: number = 0 ? getClient()
.getReportCount({})
.then(
(res) =>
res.comment_reports +
res.post_reports +
(res.private_message_reports ?? 0),
)
.catch(() => 0)
: new Promise<number>((res) => res(0))
if (mod) { const applicationsPromise = admin
try { ? getClient()
const reportRes = await getClient().getReportCount({}) .getUnreadRegistrationApplicationCount()
.then((res) => res.registration_applications)
.catch(() => 0)
: new Promise<number>((res) => res(0))
reports = const [unreads, reports, applications] = await Promise.all([
reportRes.comment_reports + unreadsPromise,
reportRes.post_reports + reportsPromise,
(reportRes.private_message_reports ?? 0) applicationsPromise,
} catch (e) { ])
// doesn't matter
}
}
if (admin) {
try {
const applicationRes =
await getClient().getUnreadRegistrationApplicationCount()
applications = applicationRes.registration_applications
} catch (e) {
// doesn't matter
}
}
return { return {
unreads: unreads.mentions + unreads.private_messages + unreads.replies, unreads: unreads,
reports: reports, reports: reports,
applications: applications, applications: applications,
} }