@@ -7,9 +7,9 @@ import { type UpdateManifest, installUpdate } from '@tauri-apps/api/updater'
7
7
import { relaunch } from '@tauri-apps/api/process'
8
8
import { computedEager } from '@vueuse/core'
9
9
import { type Thread , getNotifications , markNotificationAsRead , unsubscribeNotification } from '../api/notifications'
10
- import { ColorPreference , InvokeCommand , Page , notificationApiMutex , prefersDark } from '../constants'
10
+ import { CheckedNotificationProcess , ColorPreference , InvokeCommand , Page , notificationApiMutex , prefersDark } from '../constants'
11
11
import { AppStorage } from '../storage'
12
- import type { AppStorageContext , NotificationList , Option , PageState } from '../types'
12
+ import type { NotificationList , Option , PageState } from '../types'
13
13
import { filterNewThreads , isRepository , isThread , toNotificationList } from '../utils/notification'
14
14
15
15
export const useStore = defineStore ( 'store' , ( ) => {
@@ -124,62 +124,40 @@ export const useStore = defineStore('store', () => {
124
124
return notifications . value . findIndex ( ( { id } ) => id === thread . id )
125
125
}
126
126
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 )
134
-
135
- checkedItems . value = [ ]
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' ) !
136
133
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 ) )
134
+ checkedThreads . forEach ( item => removeNotificationById ( item . id ) )
152
135
triggerRef ( notifications )
153
- }
154
- }
155
-
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 )
160
136
161
- checkedItems . value = [ ]
162
-
163
- checkedThreads . forEach ( item => removeNotificationById ( item . id ) )
164
- triggerRef ( notifications )
137
+ checkedItems . value = [ ]
165
138
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
- }
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
+ } )
183
161
}
184
162
185
163
const newRelease = ref < Option < UpdateManifest > > ( null )
@@ -229,12 +207,11 @@ export const useStore = defineStore('store', () => {
229
207
installingUpate,
230
208
theme,
231
209
updateAndRestart,
232
- unsubscribeCheckedNotifications,
233
210
removeNotificationById,
234
211
findThreadIndex,
235
- markCheckedNotificationsAsRead,
236
212
setPage,
237
213
fetchNotifications,
214
+ processCheckedNotifications,
238
215
logout,
239
216
}
240
217
} )
0 commit comments