Skip to content

Commit a1859ed

Browse files
committed
Revert "Cleaner code for requests"
This reverts commit dfb4bad.
1 parent dfb4bad commit a1859ed

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

src/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { Icons } from './components/Icons'
44

55
export const prefersDark = useMediaQuery('(prefers-color-scheme: dark)')
66

7-
export enum CheckedNotificationProcess {
8-
Unsubscribe,
9-
MarkAsRead,
10-
}
11-
127
export enum ColorPreference {
138
System = 'system',
149
Light = 'light',

src/pages/HomePage.vue

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { isRepository, isThread } from '../utils/notification'
1818
import Popover from '../components/Popover.vue'
1919
import MenuItems, { menuItem } from '../components/MenuItems.vue'
2020
import { type UseKeyOptions, useKey } from '../composables/useKey'
21-
import { CheckedNotificationProcess, notificationApiMutex } from '../constants'
21+
import { notificationApiMutex } from '../constants'
2222
import { everySome } from '../utils/array'
2323
2424
const store = useStore()
@@ -150,7 +150,7 @@ async function handleSelectMarkAsRead(triggeredByKeyboard = false) {
150150
if (
151151
(triggeredByKeyboard && store.checkedItems.length > 0)
152152
|| (contextMenuThread.value && isChecked(contextMenuThread.value))) {
153-
store.processCheckedNotifications(CheckedNotificationProcess.MarkAsRead)
153+
store.markCheckedNotificationsAsRead(AppStorage.get('accessToken')!)
154154
return
155155
}
156156
@@ -182,9 +182,9 @@ async function handleSelectOpen(triggeredByKeyboard = false) {
182182
store.checkedItems.forEach(handleOpenNotification)
183183
184184
if (AppStorage.get('markAsReadOnOpen'))
185-
store.processCheckedNotifications(CheckedNotificationProcess.MarkAsRead)
186-
else
187-
store.checkedItems = []
185+
store.markCheckedNotificationsAsRead(AppStorage.get('accessToken')!)
186+
187+
store.checkedItems = []
188188
return
189189
}
190190
@@ -198,7 +198,12 @@ async function handleSelectUnsubscribe(triggeredByKeyboard = false) {
198198
if (
199199
(triggeredByKeyboard && store.checkedItems.length > 0)
200200
|| (contextMenuThread.value && isChecked(contextMenuThread.value))) {
201-
store.processCheckedNotifications(CheckedNotificationProcess.Unsubscribe)
201+
try {
202+
await store.unsubscribeCheckedNotifications(AppStorage.get('accessToken')!)
203+
}
204+
catch (error) {
205+
console.log(error)
206+
}
202207
return
203208
}
204209

src/stores/store.ts

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { type UpdateManifest, installUpdate } from '@tauri-apps/api/updater'
77
import { relaunch } from '@tauri-apps/api/process'
88
import { computedEager } from '@vueuse/core'
99
import { type Thread, getNotifications, markNotificationAsRead, unsubscribeNotification } from '../api/notifications'
10-
import { CheckedNotificationProcess, ColorPreference, InvokeCommand, Page, notificationApiMutex, prefersDark } from '../constants'
10+
import { ColorPreference, InvokeCommand, Page, notificationApiMutex, prefersDark } from '../constants'
1111
import { AppStorage } from '../storage'
12-
import type { NotificationList, Option, PageState } from '../types'
12+
import type { AppStorageContext, NotificationList, Option, PageState } from '../types'
1313
import { filterNewThreads, isRepository, isThread, toNotificationList } from '../utils/notification'
1414

1515
export const useStore = defineStore('store', () => {
@@ -124,40 +124,62 @@ export const useStore = defineStore('store', () => {
124124
return notifications.value.findIndex(({ id }) => id === thread.id)
125125
}
126126

127-
function processCheckedNotifications(process: CheckedNotificationProcess) {
128-
return notificationApiMutex.runExclusive(async () => {
129-
const deletedThreads: Thread['id'][] = []
130-
const checkedThreads = checkedItems.value
131-
const snapshot = notifications.value.slice(0)
132-
const accessToken = AppStorage.get('accessToken')!
127+
async function markCheckedNotificationsAsRead(accessToken: NonNullable<AppStorageContext['accessToken']>) {
128+
const deletedThreads: Thread['id'][] = []
129+
const checkedThreads = checkedItems.value
130+
const snapshot = notifications.value.slice(0)
131+
132+
checkedThreads.forEach(item => removeNotificationById(item.id))
133+
triggerRef(notifications)
133134

134-
checkedThreads.forEach(item => removeNotificationById(item.id))
135+
checkedItems.value = []
136+
137+
try {
138+
await notificationApiMutex.runExclusive(() => pAll(
139+
checkedThreads.map(thread => async () => {
140+
await markNotificationAsRead(thread.id, accessToken)
141+
deletedThreads.push(thread.id)
142+
}),
143+
{
144+
stopOnError: false,
145+
concurrency: 7,
146+
},
147+
))
148+
}
149+
catch (error) {
150+
notifications.value = snapshot
151+
deletedThreads.forEach(id => removeNotificationById(id))
135152
triggerRef(notifications)
153+
}
154+
}
136155

137-
checkedItems.value = []
156+
async function unsubscribeCheckedNotifications(accessToken: NonNullable<AppStorageContext['accessToken']>) {
157+
const deletedThreads: Thread['id'][] = []
158+
const checkedThreads = checkedItems.value
159+
const snapshot = notifications.value.slice(0)
138160

139-
try {
140-
await pAll(
141-
checkedThreads.map(thread => async () => {
142-
if (process === CheckedNotificationProcess.MarkAsRead)
143-
await markNotificationAsRead(thread.id, accessToken)
144-
else if (process === CheckedNotificationProcess.Unsubscribe)
145-
await unsubscribeNotification(thread.id, accessToken)
146-
147-
deletedThreads.push(thread.id)
148-
}),
149-
{
150-
stopOnError: false,
151-
concurrency: 7,
152-
},
153-
)
154-
}
155-
catch (error) {
156-
notifications.value = snapshot
157-
deletedThreads.forEach(id => removeNotificationById(id))
158-
triggerRef(notifications)
159-
}
160-
})
161+
checkedItems.value = []
162+
163+
checkedThreads.forEach(item => removeNotificationById(item.id))
164+
triggerRef(notifications)
165+
166+
try {
167+
await notificationApiMutex.runExclusive(() => pAll(
168+
checkedThreads.map(thread => async () => {
169+
await unsubscribeNotification(thread.id, accessToken)
170+
deletedThreads.push(thread.id)
171+
}),
172+
{
173+
stopOnError: false,
174+
concurrency: 7,
175+
},
176+
))
177+
}
178+
catch (error) {
179+
notifications.value = snapshot
180+
deletedThreads.forEach(id => removeNotificationById(id))
181+
triggerRef(notifications)
182+
}
161183
}
162184

163185
const newRelease = ref<Option<UpdateManifest>>(null)
@@ -207,11 +229,12 @@ export const useStore = defineStore('store', () => {
207229
installingUpate,
208230
theme,
209231
updateAndRestart,
232+
unsubscribeCheckedNotifications,
210233
removeNotificationById,
211234
findThreadIndex,
235+
markCheckedNotificationsAsRead,
212236
setPage,
213237
fetchNotifications,
214-
processCheckedNotifications,
215238
logout,
216239
}
217240
})

0 commit comments

Comments
 (0)