0

我正在构建一个允许使用 Electron 和 Typescript 编辑文本文件的应用程序。

该应用程序有两个主要要求:

  1. 支持自动保存
  2. 当外部应用程序修改文本文件时,更改会反映在应用程序中。

对于第二个要求,我有一个问题:如何知道外部应用程序何时修改文件?

我正在尝试chokidar包,但由于自动保存功能,应用程序正在触发此事件,创建一个循环。

这是我用于保存和加载功能的代码,在预加载脚本中作为 API 公开:

export async function load(): Promise<string> {
    const exists = await checkIfFileExists(filePath);
    let data = "";
    if (exists) {
        const fileContent = await fs.promises.readFile(filePath, 'utf-8');
        data = fileContent;
    } else {
        await save(data);
    }
    return data;
}
    
export async function save(data: string): Promise<void> {
    await fs.promises.writeFile(filePath, data, 'utf-8');
}
4

0 回答 0