@@ -16,36 +16,58 @@ type ImageStore = {
16
16
show : Record < string , string > ;
17
17
season : Record < string , string > ;
18
18
episode : Record < string , string > ;
19
+ person : Record < string , string > ;
19
20
} ;
20
21
21
22
type ImageQuery = {
22
23
id : number ;
23
24
season ?: number ;
24
25
episode ?: number ;
25
- type : 'movie' | 'show' | 'season' | 'episode' ;
26
+ type : keyof ImageStore ;
26
27
} ;
27
28
28
- type ImagePayload = { posters ?: TmdbImage [ ] ; stills ?: TmdbImage [ ] } ;
29
+ type ImagePayload = { posters ?: TmdbImage [ ] ; stills ?: TmdbImage [ ] ; profiles ?: TmdbImage [ ] } ;
30
+
31
+ const EmptyImageStore : ImageStore = {
32
+ movie : { } ,
33
+ show : { } ,
34
+ season : { } ,
35
+ episode : { } ,
36
+ person : { } ,
37
+ } ;
29
38
30
39
export const useImageStore = defineStore ( 'data.image' , ( ) => {
31
40
const tmdbConfig = ref < TmdbConfiguration > ( ) ;
32
- const images = reactive < ImageStore > ( {
33
- movie : { } ,
34
- show : { } ,
35
- season : { } ,
36
- episode : { } ,
37
- } ) ;
38
-
39
- const imageSizes = computed ( ( ) => ( {
40
- poster : tmdbConfig . value ?. images ?. poster_sizes ,
41
- still : tmdbConfig . value ?. images ?. still_sizes ,
42
- } ) ) ;
43
-
44
- const syncSaveImageStore = debounce ( ( _images = images ) => storage . sync . set ( `data.image-store` , _images ) , 1000 ) ;
41
+ const images = reactive < ImageStore > ( EmptyImageStore ) ;
42
+
43
+ const syncSaveImageStore = debounce (
44
+ ( _images = images ) =>
45
+ Promise . all ( [
46
+ storage . sync . set ( `data.image-store.movie` , _images . movie ) ,
47
+ storage . sync . set ( `data.image-store.show` , _images . show ) ,
48
+ storage . sync . set ( `data.image-store.season` , _images . season ) ,
49
+ storage . sync . set ( `data.image-store.episode` , _images . episode ) ,
50
+ storage . sync . set ( `data.image-store.person` , _images . person ) ,
51
+ ] ) ,
52
+ 1000 ,
53
+ ) ;
45
54
46
55
const syncRestoreImageStore = async ( seed ?: Partial < ImageStore > ) => {
47
- const restored = await storage . sync . get < ImageStore > ( `data.image-store` ) ;
48
- if ( restored ) Object . assign ( images , { ...seed , ...restored } ) ;
56
+ const [ movie , show , season , episode , person ] = await Promise . all ( [
57
+ storage . sync . get < Record < string , string > > ( `data.image-store.movie` ) ,
58
+ storage . sync . get < Record < string , string > > ( `data.image-store.show` ) ,
59
+ storage . sync . get < Record < string , string > > ( `data.image-store.season` ) ,
60
+ storage . sync . get < Record < string , string > > ( `data.image-store.episode` ) ,
61
+ storage . sync . get < Record < string , string > > ( `data.image-store.person` ) ,
62
+ ] ) ;
63
+ if ( seed ) Object . assign ( images , seed ) ;
64
+ if ( movie ) Object . assign ( images . movie , movie ) ;
65
+ if ( show ) Object . assign ( images . show , show ) ;
66
+ if ( season ) Object . assign ( images . season , season ) ;
67
+ if ( episode ) Object . assign ( images . episode , episode ) ;
68
+ if ( person ) Object . assign ( images . person , person ) ;
69
+
70
+ console . info ( 'Restored Image Store' , images ) ;
49
71
} ;
50
72
51
73
const initImageStore = async ( config ?: TmdbConfiguration ) => {
@@ -54,6 +76,11 @@ export const useImageStore = defineStore('data.image', () => {
54
76
return syncRestoreImageStore ( ) ;
55
77
} ;
56
78
79
+ const imageSizes = computed ( ( ) => ( {
80
+ poster : tmdbConfig . value ?. images ?. poster_sizes ,
81
+ still : tmdbConfig . value ?. images ?. still_sizes ,
82
+ } ) ) ;
83
+
57
84
const queue : Record < string , Promise < ImagePayload > > = { } ;
58
85
59
86
const queueRequest = async ( key : string , request : ( ) => Promise < ImagePayload > ) => {
@@ -65,6 +92,8 @@ export const useImageStore = defineStore('data.image', () => {
65
92
let payload : ImagePayload ;
66
93
if ( type === 'movie' ) {
67
94
payload = await queueRequest ( `${ type } -${ id } ` , ( ) => TraktService . posters . movie ( id ) ) ;
95
+ } else if ( type === 'person' ) {
96
+ payload = await queueRequest ( `${ type } -${ id } ` , ( ) => TraktService . posters . person ( id ) ) ;
68
97
} else if ( type === 'show' ) {
69
98
payload = await queueRequest ( `${ type } -${ id } ` , ( ) => TraktService . posters . show ( id ) ) ;
70
99
} else if ( type === 'season' && season ) {
@@ -73,7 +102,7 @@ export const useImageStore = defineStore('data.image', () => {
73
102
payload = await queueRequest ( `${ type } -${ id } -${ season } -${ episode } ` , ( ) => TraktService . posters . episode ( id , season , episode ) ) ;
74
103
} else throw new Error ( 'Unsupported type or missing parameters for fetchImageUrl' ) ;
75
104
76
- const fetchedImages = payload . posters ?? payload . stills ;
105
+ const fetchedImages = payload . posters ?? payload . stills ?? payload . profiles ;
77
106
if ( ! fetchedImages ?. length ) {
78
107
console . warn ( 'No images found for' , { id, season, episode, type } ) ;
79
108
return ;
0 commit comments