1
1
import { compareDateObject , toDateObject } from '@dvcol/common-utils/common/date' ;
2
2
import { defineStore , storeToRefs } from 'pinia' ;
3
3
4
- import { ref , watch } from 'vue' ;
4
+ import { computed , ref , watch } from 'vue' ;
5
5
6
6
import type { RecursiveType } from '@dvcol/common-utils/common' ;
7
7
import type { TraktSyncActivities } from '@dvcol/trakt-http-client/models' ;
8
8
9
+ import { PollingIntervals } from '~/models/polling.model' ;
9
10
import { Logger } from '~/services/logger.service' ;
10
11
import { NotificationService } from '~/services/notification.service' ;
11
12
import { TraktService } from '~/services/trakt.service' ;
12
- import { useUserSettingsStore } from '~/stores/settings/user.store' ;
13
+ import { useAuthSettingsStoreRefs } from '~/stores/settings/auth.store' ;
14
+ import { useUserSettingsStore , useUserSettingsStoreRefs } from '~/stores/settings/user.store' ;
13
15
import { storage } from '~/utils/browser/browser-storage.utils' ;
14
16
17
+ type ActivityStoreState = {
18
+ activity ?: TraktSyncActivities ;
19
+ polling ?: number ;
20
+ } ;
21
+
15
22
const ActivityStoreConstants = {
16
23
Store : 'data.activity' ,
24
+ /** 30 seconds */
25
+ DefaultPolling : PollingIntervals . ThirtySeconds ,
17
26
} as const ;
18
27
19
28
export const useActivityStore = defineStore ( ActivityStoreConstants . Store , ( ) => {
20
29
const activity = ref < TraktSyncActivities > ( ) ;
21
30
const loading = ref ( false ) ;
31
+ const polling = ref ( ActivityStoreConstants . DefaultPolling ) ;
22
32
23
33
const clearState = ( ) => {
24
34
activity . value = undefined ;
25
35
} ;
26
36
27
- const saveState = async ( ) => storage . local . set ( ActivityStoreConstants . Store , activity . value ) ;
37
+ const saveState = async ( ) =>
38
+ storage . local . set < ActivityStoreState > ( ActivityStoreConstants . Store , {
39
+ activity : activity . value ,
40
+ polling : polling . value ,
41
+ } ) ;
28
42
const restoreState = async ( ) => {
29
- const state = await storage . local . get < TraktSyncActivities > ( ActivityStoreConstants . Store ) ;
30
- if ( state ) activity . value = state ;
43
+ const state = await storage . local . get < ActivityStoreState > ( ActivityStoreConstants . Store ) ;
44
+ if ( state ?. activity ) activity . value = state . activity ;
45
+ if ( state ?. polling !== undefined ) polling . value = state . polling ;
31
46
} ;
32
47
33
48
const fetchActivity = async ( ) => {
@@ -47,8 +62,11 @@ export const useActivityStore = defineStore(ActivityStoreConstants.Store, () =>
47
62
} ;
48
63
49
64
const { refreshUserSettings } = useUserSettingsStore ( ) ;
65
+ const { isAuthenticated } = useAuthSettingsStoreRefs ( ) ;
66
+ const { user } = useUserSettingsStoreRefs ( ) ;
67
+ const interval = ref < ReturnType < typeof setInterval > > ( ) ;
50
68
51
- const initActivityStore = async ( fetch ?: boolean ) => {
69
+ const initActivityStore = async ( ) => {
52
70
await restoreState ( ) ;
53
71
54
72
watch ( activity , ( next , prev ) => {
@@ -108,10 +126,48 @@ export const useActivityStore = defineStore(ActivityStoreConstants.Store, () =>
108
126
}
109
127
} ) ;
110
128
111
- if ( fetch ) await fetchActivity ( ) ;
129
+ watch (
130
+ polling ,
131
+ async ( ) => {
132
+ if ( interval . value ) clearInterval ( interval . value ) ;
133
+ if ( ! polling . value ) return ;
134
+ if ( isAuthenticated . value ) await fetchActivity ( ) ;
135
+ interval . value = setInterval ( ( ) => {
136
+ if ( ! isAuthenticated . value ) return ;
137
+ return fetchActivity ( ) ;
138
+ } , polling . value ) ;
139
+ Logger . debug ( 'Activity polling interval set to' , polling . value ) ;
140
+ } ,
141
+ {
142
+ immediate : true ,
143
+ } ,
144
+ ) ;
145
+
146
+ watch ( user , async ( ) => {
147
+ if ( ! isAuthenticated . value ) return ;
148
+ await fetchActivity ( ) ;
149
+ } ) ;
150
+
151
+ if ( polling . value || ! isAuthenticated . value ) return ;
152
+ return fetchActivity ( ) ;
112
153
} ;
113
154
114
- return { activity, loading, fetchActivity, clearState, saveState, restoreState, initActivityStore } ;
155
+ return {
156
+ activity,
157
+ polling : computed ( {
158
+ get : ( ) => polling . value ,
159
+ set : ( value : number = ActivityStoreConstants . DefaultPolling ) => {
160
+ polling . value = value ;
161
+ saveState ( ) . catch ( e => Logger . error ( 'Failed to save watching state' , e ) ) ;
162
+ } ,
163
+ } ) ,
164
+ loading,
165
+ fetchActivity,
166
+ clearState,
167
+ saveState,
168
+ restoreState,
169
+ initActivityStore,
170
+ } ;
115
171
} ) ;
116
172
117
173
export const useActivityStoreRefs = ( ) => storeToRefs ( useActivityStore ( ) ) ;
0 commit comments