Skip to content

Commit 4651ae4

Browse files
committed
⚡️ 优化脚本资源加载
1 parent eead31f commit 4651ae4

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/app/service/service_worker/resource.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,39 @@ export class ResourceService {
2424
this.resourceDAO.enableCache();
2525
}
2626

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> {
2833
const res = await this.getResourceModel(url);
2934
if (res) {
3035
return res;
3136
}
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+
}
3248
return undefined;
3349
}
3450

35-
public async getScriptResources(script: Script): Promise<{ [key: string]: Resource }> {
51+
public async getScriptResources(script: Script, load: boolean): Promise<{ [key: string]: Resource }> {
3652
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)) || {}),
4056
};
4157
}
4258

43-
async getResourceByType(script: Script, type: ResourceType): Promise<{ [key: string]: Resource }> {
59+
async getResourceByType(script: Script, type: ResourceType, load: boolean): Promise<{ [key: string]: Resource }> {
4460
if (!script.metadata[type]) {
4561
return {};
4662
}
@@ -67,7 +83,7 @@ export class ResourceService {
6783
const res = await this.updateResource(script.uuid, path, type);
6884
ret[resourceKey] = res;
6985
} else {
70-
const res = await this.getResource(script.uuid, path, type);
86+
const res = await this.getResource(script.uuid, path, type, load);
7187
if (res) {
7288
ret[resourceKey] = res;
7389
}
@@ -113,6 +129,7 @@ export class ResourceService {
113129
return ret;
114130
}
115131

132+
// 检查资源是否存在,如果不存在则重新加载
116133
async checkResource(uuid: string, url: string, type: ResourceType) {
117134
let res = await this.getResourceModel(url);
118135
if (res) {
@@ -294,8 +311,12 @@ export class ResourceService {
294311
return await this.resourceDAO.update(data.meta.url, res);
295312
}
296313

314+
requestGetScriptResources(script: Script): Promise<{ [key: string]: Resource }> {
315+
return this.getScriptResources(script, false);
316+
}
317+
297318
init() {
298-
this.group.on("getScriptResources", this.getScriptResources.bind(this));
319+
this.group.on("getScriptResources", this.requestGetScriptResources.bind(this));
299320
this.group.on("deleteResource", this.deleteResource.bind(this));
300321

301322
// 删除相关资源

src/app/service/service_worker/runtime.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export class RuntimeService {
425425
}),
426426
// 加载resource
427427
...enableScript.map(async (script) => {
428-
const resource = await this.resource.getScriptResources(script);
428+
const resource = await this.resource.getScriptResources(script, false);
429429
script.resource = resource;
430430
}),
431431
// 加载code相关的信息
@@ -648,7 +648,6 @@ export class RuntimeService {
648648
scriptMatch[key] = val;
649649
// 优化性能,将不需要的信息去掉
650650
// 而且可能会超过缓存的存储限制
651-
// @ts-ignore
652651
scriptMatch[key].code = "";
653652
scriptMatch[key].value = {};
654653
scriptMatch[key].resource = {};

src/app/service/service_worker/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class ScriptService {
307307

308308
ret.value = await this.valueService.getScriptValue(ret);
309309

310-
ret.resource = await this.resourceService.getScriptResources(ret);
310+
ret.resource = await this.resourceService.getScriptResources(ret, true);
311311

312312
ret.flag = randomString(16);
313313
const code = await this.getCode(ret.uuid);

0 commit comments

Comments
 (0)