Skip to content

Commit 5c4ef70

Browse files
committed
优化 ScriptEditor
1 parent 1dff566 commit 5c4ef70

3 files changed

Lines changed: 275 additions & 338 deletions

File tree

src/app/service/service_worker/client.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import { type FileSystemType } from "@Packages/filesystem/factory";
1111
import { type ResourceBackup } from "@App/pkg/backup/struct";
1212
import { type VSCodeConnect } from "../offscreen/vscode-connect";
1313
import { type ScriptInfo } from "@App/pkg/utils/scriptInstall";
14-
import type { ScriptService, TCheckScriptUpdateOption, TOpenBatchUpdatePageOption } from "./script";
14+
import type {
15+
ScriptService,
16+
TCheckScriptUpdateOption,
17+
TOpenBatchUpdatePageOption,
18+
TScriptInstallParam,
19+
TScriptInstallReturn,
20+
} from "./script";
1521
import { encodeRValue, type TKeyValuePair } from "@App/pkg/utils/message_value";
1622
import { type TSetValuesParams } from "./value";
1723

@@ -40,15 +46,9 @@ export class ScriptClient extends Client {
4046
return this.do<[boolean, ScriptInfo, { byWebRequest?: boolean }]>("getInstallInfo", uuid);
4147
}
4248

43-
install(params: {
44-
script: Script;
45-
code: string;
46-
upsertBy?: InstallSource;
47-
createtime?: number;
48-
updatetime?: number;
49-
}): Promise<{ update: boolean }> {
49+
install(params: TScriptInstallParam): Promise<TScriptInstallReturn> {
5050
if (!params.upsertBy) params.upsertBy = "user";
51-
return this.doThrow("install", { ...params });
51+
return this.doThrow("install", { ...params } satisfies TScriptInstallParam);
5252
}
5353

5454
// delete(uuid: string) {

src/app/service/service_worker/script.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ export type TCheckScriptUpdateOption = Partial<
5454

5555
export type TOpenBatchUpdatePageOption = { q: string; dontCheckNow: boolean };
5656

57+
export type TScriptInstallParam = {
58+
script: Script; // 脚本信息(包含脚本的基础元数据)
59+
code: string; // 脚本源码内容
60+
upsertBy?: InstallSource; // 安装/更新来源(用于标识脚本来源渠道)
61+
createtime?: number; // 导入时指定的创建时间(时间戳,毫秒)
62+
updatetime?: number; // 导入时指定的最后更新时间(时间戳,毫秒)
63+
};
64+
65+
export type TScriptInstallReturn = {
66+
update: boolean; // 是否为更新操作(true 表示更新,false 表示新增)
67+
updatetime: number | undefined; // 实际生效的更新时间(时间戳,毫秒)
68+
};
69+
5770
export class ScriptService {
5871
logger: Logger;
5972
scriptCodeDAO: ScriptCodeDAO = new ScriptCodeDAO();
@@ -369,14 +382,8 @@ export class ScriptService {
369382
return this.mq.publish<TInstallScript>("installScript", { script, ...options });
370383
}
371384

372-
// 安装脚本 / 更新腳本
373-
async installScript(param: {
374-
script: Script;
375-
code: string;
376-
upsertBy?: InstallSource;
377-
createtime?: number;
378-
updatetime?: number;
379-
}) {
385+
// 安装脚本 / 更新脚本
386+
async installScript(param: TScriptInstallParam): Promise<TScriptInstallReturn> {
380387
param.upsertBy = param.upsertBy || "user";
381388
const { script, upsertBy, createtime, updatetime } = param;
382389
// 删 storage cache
@@ -427,10 +434,11 @@ export class ScriptService {
427434
]);
428435

429436
// 广播一下
430-
// Runtime 會負責更新 CompiledResource
437+
// Runtime 会负责更新 CompiledResource
431438
this.publishInstallScript(script, { update, upsertBy });
432439

433-
return { update };
440+
// 传回(由后台控制的)实际更新时间,让 editor 中的script能保持正确的更新时间
441+
return { update, updatetime: script.updatetime };
434442
})
435443
.catch((e: any) => {
436444
logger.error("install error", Logger.E(e));
@@ -1144,7 +1152,7 @@ export class ScriptService {
11441152
}
11451153

11461154
isInstalled({ name, namespace }: { name: string; namespace: string }): Promise<App.IsInstalledResponse> {
1147-
// 用於 window.external
1155+
// 用于 window.external
11481156
return this.scriptDAO.findByNameAndNamespace(name, namespace).then((script) => {
11491157
if (script) {
11501158
return {

0 commit comments

Comments
 (0)