Skip to content

Commit

Permalink
Fix plot client disposed too early
Browse files Browse the repository at this point in the history
Use editor input dispose to determine the editor is finished with the plot client
  • Loading branch information
timtmok committed Aug 30, 2024
1 parent b72be8f commit e1ad439
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { WebviewPlotClient } from 'vs/workbench/contrib/positronPlots/browser/we
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { URI } from 'vs/base/common/uri';
import { PositronPlotCommProxy } from 'vs/workbench/services/languageRuntime/common/positronPlotCommProxy';
import { EditorCloseContext } from 'vs/workbench/common/editor';

/** The maximum number of recent executions to store. */
const MaxRecentExecutions = 10;
Expand Down Expand Up @@ -1051,20 +1050,6 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
}),
});

this._editorService.onDidCloseEditor(editor => {
// Dispose the plot client for the editor when the editor matches the plot ID
// and the editor was closed, not moved or replaced.
if (editorPane?.getId() === editor.editor.editorId
&& editor.editor.resource?.path === plotId
&& editor.context === EditorCloseContext.UNKNOWN) {
const editorPlot = this._editorPlots.get(plotId);
if (editorPlot instanceof PlotClientInstance) {
this._editorPlots.delete(plotId);
this.unregisterPlotClient(editorPlot);
}
}
});

if (!editorPane) {
throw new Error('Failed to open editor');
}
Expand All @@ -1074,7 +1059,7 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
return this._editorPlots.get(id);
}

private unregisterPlotClient(plotClient: IPositronPlotClient) {
public unregisterPlotClient(plotClient: IPositronPlotClient) {
if (plotClient instanceof PlotClientInstance) {
const plotId = plotClient.id;
const plotClients = this._plotClientsByComm.get(plotId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { URI } from 'vs/base/common/uri';
import { IUntypedEditorInput } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { IPositronPlotsService } from 'vs/workbench/services/positronPlots/common/positronPlots';

export class PositronPlotsEditorInput extends EditorInput {
static readonly TypeID: string = 'workbench.input.positronPlots';
Expand All @@ -16,9 +17,14 @@ export class PositronPlotsEditorInput extends EditorInput {

constructor(
readonly resource: URI,
@IPositronPlotsService private readonly _positronPlotsService: IPositronPlotsService
) { super(); }

override dispose(): void {
const plotClient = this._positronPlotsService.getEditorInstance(this.resource.path);
if (plotClient) {
this._positronPlotsService.unregisterPlotClient(plotClient);
}
super.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export interface IPositronPlotsService {

getEditorInstance(id: string): IPositronPlotClient | undefined;

unregisterPlotClient(plotClient: IPositronPlotClient): void;

/**
* Placeholder for service initialization.
*/
Expand Down

0 comments on commit e1ad439

Please sign in to comment.