Skip to content

Commit

Permalink
fix: dispose previous local types when they change (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Aug 2, 2023
1 parent 4715e56 commit 08af63c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/renderer/electron-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ELECTRON_DTS = 'electron.d.ts';
*/
export class ElectronTypes {
private disposables: MonacoType.IDisposable[] = [];
private electronTypesDisposable: MonacoType.IDisposable | undefined;
private watcher: fs.FSWatcher | undefined;

constructor(
Expand Down Expand Up @@ -96,13 +97,16 @@ export class ElectronTypes {
}

private setTypesFromFile(file: string, version: string) {
// Dispose of any previous Electron types so there's only ever one
this.electronTypesDisposable?.dispose();
this.electronTypesDisposable = undefined;

try {
console.log(`Updating Monaco with "${ELECTRON_DTS}@${version}"`);
const lib =
this.electronTypesDisposable =
this.monaco.languages.typescript.javascriptDefaults.addExtraLib(
fs.readFileSync(file, 'utf8'),
);
this.disposables.push(lib);
} catch (err) {
console.debug(`Unable to read types from "${file}": ${err.message}`);
}
Expand All @@ -122,6 +126,9 @@ export class ElectronTypes {
}

private dispose() {
this.electronTypesDisposable?.dispose();
this.electronTypesDisposable = undefined;

if (this.disposables.length > 0) {
for (const disposable of this.disposables) {
disposable.dispose();
Expand Down
2 changes: 2 additions & 0 deletions tests/renderer/electron-types-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ describe('ElectronTypes', () => {
await electronTypes.setVersion(localVersion);
expect(addExtraLib).toHaveBeenCalledWith(oldTypes);

expect(disposable.dispose).not.toHaveBeenCalled();
const newTypes = saveTypesFile('some changed types');
expect(newTypes).not.toEqual(oldTypes);
await waitFor(() => addExtraLib.mock.calls.length > 1);
expect(addExtraLib).toHaveBeenCalledWith(newTypes);
expect(disposable.dispose).toHaveBeenCalledTimes(1);
});

it('stops watching old types files when the version changes', async () => {
Expand Down

0 comments on commit 08af63c

Please sign in to comment.