Skip to content

Commit

Permalink
refactor: move file watcher to separate function, catch errors, and o…
Browse files Browse the repository at this point in the history
…nly read file once
  • Loading branch information
dsanders11 committed Aug 16, 2023
1 parent af25abe commit 636e784
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/main/electron-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ export class ElectronTypes {
.map(([window]) => window);
}

private notifyElectronTypesChanged(
dir: string,
file: string,
version: string,
) {
try {
const content = fs.readFileSync(file, 'utf8');

// Notify all windows watching that path
for (const window of this.getWindowsForLocalPath(dir)) {
ipcMainManager.send(
IpcEvents.ELECTRON_TYPES_CHANGED,
[content, version],
window.webContents,
);
}
} catch (err) {
console.debug(`Unable to read types from "${file}": ${err.message}`);
}
}

public async getElectronTypes(
window: BrowserWindow,
ver: RunnableVersion,
Expand All @@ -52,17 +73,9 @@ export class ElectronTypes {

// If no watcher for that path yet, create it
if (!this.watchers.has(dir)) {
const watcher = watch(file, () => {
// Watcher should notify all windows watching that path
const windows = this.getWindowsForLocalPath(dir);
for (const window of windows) {
ipcMainManager.send(
IpcEvents.ELECTRON_TYPES_CHANGED,
[fs.readFileSync(file, 'utf8'), version],
window.webContents,
);
}
});
const watcher = watch(file, () =>
this.notifyElectronTypesChanged(dir, file, version),
);
this.watchers.set(dir, watcher);
}
window.once('close', () => this.unwatch(window));
Expand Down

0 comments on commit 636e784

Please sign in to comment.