Wired external change detection and update
This commit is contained in:
parent
d354a940c4
commit
dae0cd9516
15
newton/fs.js
15
newton/fs.js
@ -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");
|
const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json");
|
||||||
let window = null;
|
let window = null;
|
||||||
let watcher = null;
|
let watcher = null;
|
||||||
let skipWatchChangeUpdateOnce = false;
|
let skipOnceFileWatchChange = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ const saveSettingsConfigData = (data) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const saveFile = (fpath, content) => {
|
const saveFile = (fpath, content) => {
|
||||||
skipWatchChangeUpdateOnce = true;
|
skipOnceFileWatchChange = true;
|
||||||
|
|
||||||
fs.writeFile(fpath, content, (err) => {
|
fs.writeFile(fpath, content, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -76,9 +76,9 @@ const saveFile = (fpath, content) => {
|
|||||||
let watchers = watcher.getWatched();
|
let watchers = watcher.getWatched();
|
||||||
let targetDir = watchers[parentDir];
|
let targetDir = watchers[parentDir];
|
||||||
if (
|
if (
|
||||||
targetDir && !targetDir.includes( path.basename(fpath) )
|
!targetDir || !targetDir.includes( path.basename(fpath) )
|
||||||
) {
|
) {
|
||||||
skipWatchChangeUpdateOnce = false;
|
skipOnceFileWatchChange = false;
|
||||||
watcher.add(fpath);
|
watcher.add(fpath);
|
||||||
window.webContents.send('update-file-path', fpath);
|
window.webContents.send('update-file-path', fpath);
|
||||||
}
|
}
|
||||||
@ -143,13 +143,14 @@ const loadFilesWatcher = () => {
|
|||||||
watcher = chokidar.watch([], {});
|
watcher = chokidar.watch([], {});
|
||||||
|
|
||||||
watcher.on('change', (fpath) => {
|
watcher.on('change', (fpath) => {
|
||||||
if (skipWatchChangeUpdateOnce) {
|
if (skipOnceFileWatchChange) {
|
||||||
skipWatchChangeUpdateOnce = false;
|
skipOnceFileWatchChange = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug("File (changed) : ", fpath);
|
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) => {
|
}).on('unlink', (fpath) => {
|
||||||
console.debug("File (deleted) : ", fpath);
|
console.debug("File (deleted) : ", fpath);
|
||||||
window.webContents.send('file-deleted', fpath);
|
window.webContents.send('file-deleted', fpath);
|
||||||
|
@ -24,6 +24,6 @@ contextBridge.exposeInMainWorld('fs', {
|
|||||||
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),
|
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),
|
||||||
onUpdateFilePath: (callback) => ipcRenderer.on('update-file-path', (_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)),
|
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)),
|
onDeletedFile: (callback) => ipcRenderer.on('file-deleted', (_event, path) => callback(path)),
|
||||||
});
|
});
|
@ -149,10 +149,15 @@ export class EditorsComponent {
|
|||||||
this.setSession(file);
|
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();
|
let message = new ServiceMessage();
|
||||||
message.action = "file-changed";
|
message.action = "file-saved";
|
||||||
message.filePath = path;
|
message.filePath = path;
|
||||||
|
|
||||||
this.tabsService.sendMessage(message);
|
this.tabsService.sendMessage(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user