Skip to content

Commit

Permalink
editor - memento bugfixes during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jun 12, 2018
1 parent 51c5e12 commit fa7ded2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
38 changes: 26 additions & 12 deletions src/vs/workbench/browser/parts/editor/baseEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export abstract class BaseEditor extends Panel implements IEditor {

let editorMemento = BaseEditor.EDITOR_MEMENTOS.get(mementoKey);
if (!editorMemento) {
editorMemento = new EditorMemento(editorGroupService, this.getMemento(storageService), key, limit);
editorMemento = new EditorMemento(this.getId(), key, this.getMemento(storageService), limit, editorGroupService);
BaseEditor.EDITOR_MEMENTOS.set(mementoKey, editorMemento);
}

Expand All @@ -156,8 +156,12 @@ export abstract class BaseEditor extends Panel implements IEditor {

shutdown(): void {

// Save all editor mementos
BaseEditor.EDITOR_MEMENTOS.forEach(editorMemento => editorMemento.shutdown());
// Shutdown all editor memento for this editor type
BaseEditor.EDITOR_MEMENTOS.forEach(editorMemento => {
if (editorMemento.id === this.getId()) {
editorMemento.shutdown();
}
});

super.shutdown();
}
Expand All @@ -177,15 +181,20 @@ interface MapGroupToMemento<T> {

export class EditorMemento<T> implements IEditorMemento<T> {
private cache: LRUCache<string, MapGroupToMemento<T>>;
private isShutdown: boolean;
private cleanedUp = false;

constructor(
private editorGroupService: IEditorGroupsService,
private memento: object,
private _id: string,
private key: string,
private limit: number
private memento: object,
private limit: number,
private editorGroupService: IEditorGroupsService
) { }

get id(): string {
return this._id;
}

saveState(group: IEditorGroup, resource: URI, state: T): void;
saveState(group: IEditorGroup, editor: EditorInput, state: T): void;
saveState(group: IEditorGroup, resourceOrEditor: URI | EditorInput, state: T): void {
Expand Down Expand Up @@ -263,10 +272,18 @@ export class EditorMemento<T> implements IEditorMemento<T> {
}

shutdown(): void {
if (this.isShutdown) {
return; // only shutdown once
const cache = this.doLoad();

// Cleanup once during shutdown
if (!this.cleanedUp) {
this.cleanUp();
this.cleanedUp = true;
}

this.memento[this.key] = cache.toJSON();
}

private cleanUp(): void {
const cache = this.doLoad();

// Remove groups from states that no longer exist
Expand All @@ -282,8 +299,5 @@ export class EditorMemento<T> implements IEditorMemento<T> {
}
});
});

this.memento[this.key] = cache.toJSON();
this.isShutdown = true;
}
}
2 changes: 1 addition & 1 deletion src/vs/workbench/browser/parts/editor/textDiffEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
}

protected getEditorMemento<T>(storageService: IStorageService, editorGroupService: IEditorGroupsService, key: string, limit: number = 10): IEditorMemento<T> {
return new EditorMemento(editorGroupService, Object.create(null), key, limit); // do not persist in storage as diff editors are never persisted
return new EditorMemento(this.getId(), key, Object.create(null), limit, editorGroupService); // do not persist in storage as diff editors are never persisted
}

public getTitle(): string {
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/test/browser/parts/editor/baseEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ suite('Workbench base editor', () => {
}

const rawMemento = Object.create(null);
let memento = new EditorMemento<TestViewState>(editorGroupService, rawMemento, 'key', 3);
let memento = new EditorMemento<TestViewState>('id', 'key', rawMemento, 3, editorGroupService);

let res = memento.loadState(testGroup0, URI.file('/A'));
assert.ok(!res);
Expand Down Expand Up @@ -241,7 +241,7 @@ suite('Workbench base editor', () => {

memento.shutdown();

memento = new EditorMemento(editorGroupService, rawMemento, 'key', 3);
memento = new EditorMemento('id', 'key', rawMemento, 3, editorGroupService);
assert.ok(memento.loadState(testGroup0, URI.file('/C')));
assert.ok(memento.loadState(testGroup0, URI.file('/D')));
assert.ok(memento.loadState(testGroup0, URI.file('/E')));
Expand Down Expand Up @@ -282,7 +282,7 @@ suite('Workbench base editor', () => {
}

const rawMemento = Object.create(null);
let memento = new EditorMemento<TestViewState>(new TestEditorGroupsService(), rawMemento, 'key', 3);
let memento = new EditorMemento<TestViewState>('id', 'key', rawMemento, 3, new TestEditorGroupsService());

const testInputA = new TestEditorInput(URI.file('/A'));

Expand Down

0 comments on commit fa7ded2

Please sign in to comment.