Skip to content

Commit 5e91a31

Browse files
committed
🐛 修复脚本同步删除问题 #1158
1 parent e2db5b9 commit 5e91a31

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/app/service/queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type TInstallScriptParams = {
2020

2121
export type TInstallScript = { script: TInstallScriptParams; update: boolean; upsertBy?: InstallSource };
2222

23-
export type TDeleteScript = { uuid: string; storageName: string; type: SCRIPT_TYPE };
23+
export type TDeleteScript = { uuid: string; storageName: string; type: SCRIPT_TYPE; deleteBy?: InstallSource };
2424

2525
export type TSortedScript = { uuid: string; sort: number };
2626

src/app/service/service_worker/script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export class ScriptService {
413413
});
414414
}
415415

416-
async deleteScript(uuid: string) {
416+
async deleteScript(uuid: string, deleteBy?: InstallSource) {
417417
let logger = this.logger.with({ uuid });
418418
const script = await this.scriptDAO.get(uuid);
419419
if (!script) {
@@ -428,7 +428,7 @@ export class ScriptService {
428428
await this.scriptCodeDAO.delete(uuid);
429429
await this.compiledResourceDAO.delete(uuid);
430430
logger.info("delete success");
431-
const data = [{ uuid, storageName, type: script.type }] as TDeleteScript[];
431+
const data = [{ uuid, storageName, type: script.type, deleteBy }] as TDeleteScript[];
432432
this.mq.publish("deleteScripts", data);
433433
return true;
434434
})

src/app/service/service_worker/synchronize.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,12 @@ export class SynchronizeService {
382382
// 对比脚本列表和文件列表,进行同步
383383
const result: Promise<void>[] = [];
384384
const updateScript: Map<string, boolean> = new Map();
385+
// 需要是同步操作,后续上传剩下的脚本
386+
// 最后使用 Promise.allSettled 进行等待
385387
uuidMap.forEach((file, uuid) => {
386388
const script = scriptMap.get(uuid);
387389
if (script) {
390+
scriptMap.delete(uuid);
388391
// 脚本存在但是文件不存在,则读取.meta.json内容判断是否需要删除脚本
389392
if (!file.script) {
390393
result.push(
@@ -394,26 +397,23 @@ export class SynchronizeService {
394397
const metaJson = (await meta.read("string")) as string;
395398
const metaObj = JSON.parse(metaJson) as SyncMeta;
396399
if (metaObj.isDeleted) {
397-
if (script) {
398-
this.script.deleteScript(script.uuid);
399-
InfoNotification(
400-
i18n.t("notification.script_sync_delete"),
401-
i18n.t("notification.script_sync_delete_desc", { scriptName: i18nName(script) })
402-
);
403-
}
404-
scriptMap.delete(uuid);
400+
// 删除脚本
401+
this.script.deleteScript(script.uuid, "sync");
402+
InfoNotification(
403+
i18n.t("notification.script_sync_delete"),
404+
i18n.t("notification.script_sync_delete_desc", { scriptName: i18nName(script) })
405+
);
405406
} else {
406-
// 否则认为是一个无效的.meta文件,进行删除
407+
// 否则认为是一个无效的.meta文件进行删除,并进行同步
407408
await fs.delete(file.meta!.name);
409+
result.push(this.pushScript(fs, script));
408410
}
409411
})()
410412
);
411413
return;
412414
}
413415
// 过滤掉无变动的文件
414416
if (fileDigestMap[file.script!.name] === file.script!.digest) {
415-
// 删除了之后,剩下的就是需要上传的脚本了
416-
scriptMap.delete(uuid);
417417
return;
418418
}
419419
const updatetime = script.updatetime || script.createtime;
@@ -427,10 +427,9 @@ export class SynchronizeService {
427427
updateScript.set(uuid, true);
428428
result.push(this.pullScript(fs, file as SyncFiles, cloudStatus[uuid], script));
429429
}
430-
scriptMap.delete(uuid);
431430
return;
432431
}
433-
// 如果脚本不存在,且文件存在,则安装脚本
432+
// 如果脚本不存在,但文件存在,则安装脚本
434433
if (file.script) {
435434
if (!file.meta) {
436435
// 如果.meta文件不存在,则删除脚本文件,并跳过
@@ -679,7 +678,10 @@ export class SynchronizeService {
679678
const config = await this.systemConfig.getCloudSync();
680679
if (config.enable) {
681680
this.buildFileSystem(config).then(async (fs) => {
682-
for (const { uuid } of data) {
681+
for (const { uuid, deleteBy } of data) {
682+
if (deleteBy === "sync") {
683+
continue;
684+
}
683685
await this.deleteCloudScript(fs, uuid, config.syncDelete);
684686
}
685687
});

0 commit comments

Comments
 (0)