Wired external change detection and update

This commit is contained in:
itdominator 2025-06-21 18:17:01 -05:00
parent d354a940c4
commit dae0cd9516
3 changed files with 16 additions and 10 deletions

View File

@ -12,7 +12,7 @@ const SETTINGS_CONFIG_PATH = path.join(CONFIG_PATH, "/settings.json");
const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json");
let window = null;
let watcher = null;
let skipWatchChangeUpdateOnce = false;
let skipOnceFileWatchChange = false;
@ -64,7 +64,7 @@ const saveSettingsConfigData = (data) => {
}
const saveFile = (fpath, content) => {
skipWatchChangeUpdateOnce = true;
skipOnceFileWatchChange = true;
fs.writeFile(fpath, content, (err) => {
if (err) {
@ -76,9 +76,9 @@ const saveFile = (fpath, content) => {
let watchers = watcher.getWatched();
let targetDir = watchers[parentDir];
if (
targetDir && !targetDir.includes( path.basename(fpath) )
!targetDir || !targetDir.includes( path.basename(fpath) )
) {
skipWatchChangeUpdateOnce = false;
skipOnceFileWatchChange = false;
watcher.add(fpath);
window.webContents.send('update-file-path', fpath);
}
@ -143,13 +143,14 @@ const loadFilesWatcher = () => {
watcher = chokidar.watch([], {});
watcher.on('change', (fpath) => {
if (skipWatchChangeUpdateOnce) {
skipWatchChangeUpdateOnce = false;
if (skipOnceFileWatchChange) {
skipOnceFileWatchChange = false;
return;
}
console.debug("File (changed) : ", fpath);
window.webContents.send('file-changed', fpath);
const data = getFileContents(fpath, false, false);
window.webContents.send('file-changed', fpath, data);
}).on('unlink', (fpath) => {
console.debug("File (deleted) : ", fpath);
window.webContents.send('file-deleted', fpath);

View File

@ -24,6 +24,6 @@ contextBridge.exposeInMainWorld('fs', {
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),
onUpdateFilePath: (callback) => ipcRenderer.on('update-file-path', (_event, paths) => callback(paths)),
onSavedFile: (callback) => ipcRenderer.on('file-saved', (_event, path) => callback(path)),
onChangedFile: (callback) => ipcRenderer.on('file-changed', (_event, path) => callback(path)),
onChangedFile: (callback) => ipcRenderer.on('file-changed', (_event, path, data) => callback(path, data)),
onDeletedFile: (callback) => ipcRenderer.on('file-deleted', (_event, path) => callback(path)),
});

View File

@ -149,10 +149,15 @@ export class EditorsComponent {
this.setSession(file);
});
window.fs.onChangedFile(async (path: string) => {
window.fs.onChangedFile(async (path: string, data: string) => {
let file = this.filesService.get(path);
file.session.setValue(data);
// Note: fake 'save' event to not show as changed iven external save happened...
let message = new ServiceMessage();
message.action = "file-changed";
message.action = "file-saved";
message.filePath = path;
this.tabsService.sendMessage(message);
});