Wiring change detection to render process

This commit is contained in:
itdominator 2025-06-19 20:03:02 -05:00
parent 5c19d82834
commit c599013af4
6 changed files with 40 additions and 15 deletions

View File

@ -14,30 +14,41 @@ let window = null;
let watcher = null; let watcher = null;
const getIconPath = () => { const getIconPath = () => {
return path.join(CONFIG_PATH, "./icons/newton.png"); return path.join(CONFIG_PATH, "./icons/newton.png");
} }
const getSettingsConfigData = () => { const getSettingsConfigData = () => {
return getFileContents(SETTINGS_CONFIG_PATH); return getFileContents(
SETTINGS_CONFIG_PATH,
useRelativePath = false,
watchFile = false
);
} }
const getLspConfigData = () => { const getLspConfigData = () => {
const config = getFileContents(LSP_CONFIG_PATH, useRelativePath = true); return getFileContents(
return config.replaceAll("{user.home}", HOME_DIR); LSP_CONFIG_PATH,
useRelativePath = true,
watchFile = false
).replaceAll("{user.home}", HOME_DIR);
} }
const getFileContents = (_path, useRelativePath = false) => { const getFileContents = (_path, useRelativePath = false, watchFile = true) => {
console.log(`Getting Contents For: ${_path}`); console.log(`Getting Contents For: ${_path}`);
try { try {
if (!useRelativePath) { if (useRelativePath)
return fs.readFileSync(_path, 'utf8'); return fs.readFileSync(
} else { path.join(__dirname, _path),
let fpath = path.join(__dirname, _path); 'utf8'
watcher.add(fpath); );
return fs.readFileSync(fpath, 'utf8');
} if (watchFile)
watcher.add(_path);
return fs.readFileSync(_path, 'utf8');
} catch(err) { } catch(err) {
return `{"message": {"type": "error", "text": "Error: Could not read ${_path}"}}`; return `{"message": {"type": "error", "text": "Error: Could not read ${_path}"}}`;
} }
@ -107,8 +118,10 @@ const loadFilesWatcher = () => {
watcher.on('change', (fpath) => { watcher.on('change', (fpath) => {
console.debug("File (changed) : ", fpath); console.debug("File (changed) : ", fpath);
window.webContents.send('file-changed', fpath);
}).on('unlink', (fpath) => { }).on('unlink', (fpath) => {
console.debug("File (unlinked) : ", fpath); console.debug("File (deleted) : ", fpath);
window.webContents.send('file-deleted', fpath);
}); });
} }

View File

@ -89,7 +89,7 @@ app.whenReady().then(async () => {
app.quit(); app.quit();
} }
newton.settings.loadsettings(); newton.settings.loadSettings();
newton.fs.loadFilesWatcher(); newton.fs.loadFilesWatcher();
loadHandlers(); loadHandlers();

View File

@ -22,4 +22,6 @@ contextBridge.exposeInMainWorld('fs', {
closeFile: (path) => ipcRenderer.invoke("closeFile", path), closeFile: (path) => ipcRenderer.invoke("closeFile", path),
getPathForFile: (file) => webUtils.getPathForFile(file), getPathForFile: (file) => webUtils.getPathForFile(file),
onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)), onLoadFiles: (callback) => ipcRenderer.on('load-files', (_event, paths) => callback(paths)),
onChangedFile: (callback) => ipcRenderer.on('file-changed', (_event, path) => callback(path)),
onDeletedFile: (callback) => ipcRenderer.on('file-deleted', (_event, path) => callback(path)),
}); });

View File

@ -4,7 +4,7 @@ const { newtonFs } = require('./fs');
let config = {}; let config = {};
const loadsettings = () => { const loadSettings = () => {
config = JSON.parse( config = JSON.parse(
newtonFs.getSettingsConfigData() newtonFs.getSettingsConfigData()
); );
@ -38,7 +38,7 @@ const getIconPath = () => {
module.exports = { module.exports = {
settingsManager: { settingsManager: {
getIconPath: getIconPath, getIconPath: getIconPath,
loadsettings: loadsettings, loadSettings: loadSettings,
getConfig: getConfig, getConfig: getConfig,
saveConfig: saveConfig, saveConfig: saveConfig,
} }

View File

@ -134,6 +134,14 @@ export class EditorsComponent {
this.setSession(file); this.setSession(file);
}); });
window.fs.onChangedFile(async (path: string) => {
console.log(path);
});
window.fs.onDeletedFile(async (path: string) => {
console.log(path);
});
window.main.onMenuActions(async (action: string) => { window.main.onMenuActions(async (action: string) => {
let editorComponent = this.getActiveEditorComponent(); let editorComponent = this.getActiveEditorComponent();
let editor = editorComponent.editor; let editor = editorComponent.editor;

View File

@ -32,6 +32,8 @@ declare global {
closeFile: (arg0: any) => Promise<string>, closeFile: (arg0: any) => Promise<string>,
getPathForFile: any, getPathForFile: any,
onLoadFiles: (arg0: any) => Promise<string>, onLoadFiles: (arg0: any) => Promise<string>,
onChangedFile: (arg0: any) => Promise<string>,
onDeletedFile: (arg0: any) => Promise<string>,
} }
} }
} }