1
- import { useStorage } from '@vueuse/core' ;
2
- import { ref , watch , type Ref } from 'vue' ;
1
+ /**
2
+ * CommonStore is a base class for all stores. It extends from BaseState,
3
+ * providing also a way to reset the state on logout automatically.
4
+ *
5
+ * This class is intended to be used by stores. It
6
+ * should not be used by plugins (check BaseState for that)
7
+ * since it has a dependency on the auth plugin.
8
+ */
3
9
import type { UnknownRecord } from 'type-fest' ;
4
- import { mergeExcludingUnknown } from '@/utils/data-manipulation' ;
5
- import { isFunc , isNil } from '@/utils/validation' ;
10
+ import { isBool } from '@/utils/validation' ;
11
+ import { remote } from '@/plugins/remote' ;
12
+ import { BaseState , type BaseStateParams } from '@/store/super/base-state' ;
6
13
7
- export interface CommonStoreParams < T > {
8
- defaultState : ( ) => T ;
9
- /**
10
- * Key to be used as an identifier
11
- */
12
- storeKey : string ;
13
- persistenceType ?: 'localStorage' | 'sessionStorage' ;
14
- resetOnLogout ?: boolean | ( ( ) => void ) ;
14
+ export interface CommonStoreParams < T > extends BaseStateParams < T > {
15
+ resetOnLogout ?: boolean | MaybePromise < T > ;
15
16
}
16
17
17
18
export abstract class CommonStore <
@@ -23,53 +24,18 @@ export abstract class CommonStore<
23
24
* Exposed properties are also writable.
24
25
*/
25
26
K extends keyof T = never
26
- > {
27
- private readonly _defaultState ;
28
- protected readonly _storeKey ;
29
- protected readonly _state : Ref < T > ;
30
- /**
31
- * Same as _state, but we use the type system to define which properties
32
- * we want to have accessible to consumers of the extended class.
33
- */
34
- public readonly state : Ref < Pick < T , K > > ;
35
-
36
- protected readonly _reset = ( ) : void => {
37
- Object . assign ( this . _state . value , this . _defaultState ( ) ) ;
38
- } ;
39
-
27
+ > extends BaseState < T , K > {
40
28
protected constructor ( {
41
29
defaultState,
42
30
storeKey,
43
31
persistenceType,
44
32
resetOnLogout
45
33
} : CommonStoreParams < T >
46
34
) {
47
- this . _storeKey = storeKey ;
48
- this . _defaultState = defaultState ;
49
-
50
- this . _state = isNil ( persistenceType ) || isNil ( storeKey )
51
- ? ref ( this . _defaultState ( ) ) as Ref < T >
52
- : useStorage ( storeKey , this . _defaultState ( ) , globalThis [ persistenceType ] , {
53
- mergeDefaults : ( storageValue , defaults ) =>
54
- mergeExcludingUnknown ( storageValue , defaults )
55
- } ) ;
56
- this . state = this . _state ;
35
+ super ( { defaultState, storeKey, persistenceType } ) ;
57
36
58
37
if ( resetOnLogout ) {
59
- // eslint-disable-next-line sonarjs/no-async-constructor
60
- void ( async ( ) => {
61
- const { remote } = await import ( '@/plugins/remote' ) ;
62
-
63
- watch ( remote . auth . currentUser ,
64
- ( ) => {
65
- if ( ! remote . auth . currentUser . value ) {
66
- const funcToRun = isFunc ( resetOnLogout ) ? resetOnLogout : this . _reset ;
67
-
68
- funcToRun ( ) ;
69
- }
70
- } , { flush : 'post' }
71
- ) ;
72
- } ) ( ) ;
38
+ remote . auth . onAfterLogout ( isBool ( resetOnLogout ) ? this . _reset : resetOnLogout ) ;
73
39
}
74
40
}
75
41
}
0 commit comments