Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dispose previous local types when they change #1427

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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