1
1
import { LiveData , Store } from '@toeverything/infra' ;
2
2
import { map } from 'rxjs' ;
3
3
4
- import { AuthService , type WorkspaceServerService } from '../../cloud' ;
5
4
import type { WorkspaceDBService } from '../../db' ;
6
- import type { WorkspaceService } from '../../workspace' ;
7
5
import type { FavoriteSupportTypeUnion } from '../constant' ;
8
6
import { isFavoriteSupportType } from '../constant' ;
9
7
@@ -14,41 +12,18 @@ export interface FavoriteRecord {
14
12
}
15
13
16
14
export class FavoriteStore extends Store {
17
- authService = this . workspaceServerService . server ?. scope . get ( AuthService ) ;
18
- constructor (
19
- private readonly workspaceDBService : WorkspaceDBService ,
20
- private readonly workspaceService : WorkspaceService ,
21
- private readonly workspaceServerService : WorkspaceServerService
22
- ) {
15
+ constructor ( private readonly workspaceDBService : WorkspaceDBService ) {
23
16
super ( ) ;
24
17
}
25
18
26
- private get userdataDB$ ( ) {
27
- // if is local workspace or no account, use __local__ userdata
28
- // sometimes we may have cloud workspace but no account for a short time, we also use __local__ userdata
29
- if (
30
- this . workspaceService . workspace . meta . flavour === 'local' ||
31
- ! this . authService
32
- ) {
33
- return new LiveData ( this . workspaceDBService . userdataDB ( '__local__' ) ) ;
34
- } else {
35
- return this . authService . session . account$ . map ( account => {
36
- if ( ! account ) {
37
- return this . workspaceDBService . userdataDB ( '__local__' ) ;
38
- }
39
- return this . workspaceDBService . userdataDB ( account . id ) ;
40
- } ) ;
41
- }
42
- }
43
-
44
19
watchIsLoading ( ) {
45
- return this . userdataDB$
20
+ return this . workspaceDBService . userdataDB$
46
21
. map ( db => LiveData . from ( db . favorite . isLoading$ , false ) )
47
22
. flat ( ) ;
48
23
}
49
24
50
25
watchFavorites ( ) {
51
- return this . userdataDB$
26
+ return this . workspaceDBService . userdataDB$
52
27
. map ( db => LiveData . from ( db . favorite . find$ ( ) , [ ] ) )
53
28
. flat ( )
54
29
. map ( raw => {
@@ -63,7 +38,7 @@ export class FavoriteStore extends Store {
63
38
id : string ,
64
39
index : string
65
40
) : FavoriteRecord {
66
- const db = this . userdataDB$ . value ;
41
+ const db = this . workspaceDBService . userdataDB$ . value ;
67
42
const raw = db . favorite . create ( {
68
43
key : this . encodeKey ( type , id ) ,
69
44
index,
@@ -72,17 +47,17 @@ export class FavoriteStore extends Store {
72
47
}
73
48
74
49
reorderFavorite ( type : FavoriteSupportTypeUnion , id : string , index : string ) {
75
- const db = this . userdataDB$ . value ;
50
+ const db = this . workspaceDBService . userdataDB$ . value ;
76
51
db . favorite . update ( this . encodeKey ( type , id ) , { index } ) ;
77
52
}
78
53
79
54
removeFavorite ( type : FavoriteSupportTypeUnion , id : string ) {
80
- const db = this . userdataDB$ . value ;
55
+ const db = this . workspaceDBService . userdataDB$ . value ;
81
56
db . favorite . delete ( this . encodeKey ( type , id ) ) ;
82
57
}
83
58
84
59
watchFavorite ( type : FavoriteSupportTypeUnion , id : string ) {
85
- const db = this . userdataDB$ . value ;
60
+ const db = this . workspaceDBService . userdataDB$ . value ;
86
61
return LiveData . from < FavoriteRecord | undefined > (
87
62
db . favorite
88
63
. get$ ( this . encodeKey ( type , id ) )
0 commit comments