@@ -24,23 +24,39 @@ export class ResourceService {
24
24
this . resourceDAO . enableCache ( ) ;
25
25
}
26
26
27
- public async getResource ( uuid : string , url : string , _type : ResourceType ) : Promise < Resource | undefined > {
27
+ public async getResource (
28
+ uuid : string ,
29
+ url : string ,
30
+ _type : ResourceType ,
31
+ load : boolean
32
+ ) : Promise < Resource | undefined > {
28
33
const res = await this . getResourceModel ( url ) ;
29
34
if ( res ) {
30
35
return res ;
31
36
}
37
+ if ( load ) {
38
+ // 如果没有缓存,则尝试加载资源
39
+ try {
40
+ return await this . updateResource ( uuid , url , _type ) ;
41
+ } catch ( e : any ) {
42
+ this . logger . error ( "load resource error" , { url } , Logger . E ( e ) ) ;
43
+ }
44
+ } else {
45
+ // 如果没有缓存则不加载,则返回undefined,但是会在后台异步加载
46
+ this . updateResource ( uuid , url , _type ) ;
47
+ }
32
48
return undefined ;
33
49
}
34
50
35
- public async getScriptResources ( script : Script ) : Promise < { [ key : string ] : Resource } > {
51
+ public async getScriptResources ( script : Script , load : boolean ) : Promise < { [ key : string ] : Resource } > {
36
52
return {
37
- ...( ( await this . getResourceByType ( script , "require" ) ) || { } ) ,
38
- ...( ( await this . getResourceByType ( script , "require-css" ) ) || { } ) ,
39
- ...( ( await this . getResourceByType ( script , "resource" ) ) || { } ) ,
53
+ ...( ( await this . getResourceByType ( script , "require" , load ) ) || { } ) ,
54
+ ...( ( await this . getResourceByType ( script , "require-css" , load ) ) || { } ) ,
55
+ ...( ( await this . getResourceByType ( script , "resource" , load ) ) || { } ) ,
40
56
} ;
41
57
}
42
58
43
- async getResourceByType ( script : Script , type : ResourceType ) : Promise < { [ key : string ] : Resource } > {
59
+ async getResourceByType ( script : Script , type : ResourceType , load : boolean ) : Promise < { [ key : string ] : Resource } > {
44
60
if ( ! script . metadata [ type ] ) {
45
61
return { } ;
46
62
}
@@ -67,7 +83,7 @@ export class ResourceService {
67
83
const res = await this . updateResource ( script . uuid , path , type ) ;
68
84
ret [ resourceKey ] = res ;
69
85
} else {
70
- const res = await this . getResource ( script . uuid , path , type ) ;
86
+ const res = await this . getResource ( script . uuid , path , type , load ) ;
71
87
if ( res ) {
72
88
ret [ resourceKey ] = res ;
73
89
}
@@ -113,6 +129,7 @@ export class ResourceService {
113
129
return ret ;
114
130
}
115
131
132
+ // 检查资源是否存在,如果不存在则重新加载
116
133
async checkResource ( uuid : string , url : string , type : ResourceType ) {
117
134
let res = await this . getResourceModel ( url ) ;
118
135
if ( res ) {
@@ -294,8 +311,12 @@ export class ResourceService {
294
311
return await this . resourceDAO . update ( data . meta . url , res ) ;
295
312
}
296
313
314
+ requestGetScriptResources ( script : Script ) : Promise < { [ key : string ] : Resource } > {
315
+ return this . getScriptResources ( script , false ) ;
316
+ }
317
+
297
318
init ( ) {
298
- this . group . on ( "getScriptResources" , this . getScriptResources . bind ( this ) ) ;
319
+ this . group . on ( "getScriptResources" , this . requestGetScriptResources . bind ( this ) ) ;
299
320
this . group . on ( "deleteResource" , this . deleteResource . bind ( this ) ) ;
300
321
301
322
// 删除相关资源
0 commit comments