From 6636ad9d080f655f21822b3a9cc1d95f9373f207 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 12 Oct 2018 12:03:45 +0200 Subject: [PATCH] beware of sync/async promises related to #60163 --- src/vs/base/common/lifecycle.ts | 4 ++-- src/vs/base/test/common/lifecycle.test.ts | 2 +- .../common/textModelResolverService.ts | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/vs/base/common/lifecycle.ts b/src/vs/base/common/lifecycle.ts index 405752fdb57..4b3cd41b713 100644 --- a/src/vs/base/common/lifecycle.ts +++ b/src/vs/base/common/lifecycle.ts @@ -80,7 +80,7 @@ export abstract class ReferenceCollection { const { object } = reference; const dispose = once(() => { if (--reference.counter === 0) { - this.destroyReferencedObject(reference.object); + this.destroyReferencedObject(key, reference.object); delete this.references[key]; } }); @@ -91,7 +91,7 @@ export abstract class ReferenceCollection { } protected abstract createReferencedObject(key: string): T; - protected abstract destroyReferencedObject(object: T): void; + protected abstract destroyReferencedObject(key: string, object: T): void; } export class ImmortalReference implements IReference { diff --git a/src/vs/base/test/common/lifecycle.test.ts b/src/vs/base/test/common/lifecycle.test.ts index f82028fac1b..31269d7f2cb 100644 --- a/src/vs/base/test/common/lifecycle.test.ts +++ b/src/vs/base/test/common/lifecycle.test.ts @@ -54,7 +54,7 @@ suite('Reference Collection', () => { private _count = 0; get count() { return this._count; } protected createReferencedObject(key: string): number { this._count++; return key.length; } - protected destroyReferencedObject(object: number): void { this._count--; } + protected destroyReferencedObject(key: string, object: number): void { this._count--; } } test('simple', () => { diff --git a/src/vs/workbench/services/textmodelResolver/common/textModelResolverService.ts b/src/vs/workbench/services/textmodelResolver/common/textModelResolverService.ts index fe2b81b64cc..9c904527235 100644 --- a/src/vs/workbench/services/textmodelResolver/common/textModelResolverService.ts +++ b/src/vs/workbench/services/textmodelResolver/common/textModelResolverService.ts @@ -21,6 +21,7 @@ import { IFileService } from 'vs/platform/files/common/files'; class ResourceModelCollection extends ReferenceCollection> { private providers: { [scheme: string]: ITextModelContentProvider[] } = Object.create(null); + private modelsToDispose = new Set(); constructor( @IInstantiationService private instantiationService: IInstantiationService, @@ -31,6 +32,8 @@ class ResourceModelCollection extends ReferenceCollection { + this.modelsToDispose.delete(key); + const resource = URI.parse(key); if (this.fileService.canHandleResource(resource)) { return this.textFileService.models.loadOrCreate(resource, { reason: LoadReason.REFERENCE }); @@ -39,12 +42,16 @@ class ResourceModelCollection extends ReferenceCollection this.instantiationService.createInstance(ResourceEditorModel, resource)); } - destroyReferencedObject(modelPromise: TPromise): void { + destroyReferencedObject(key: string, modelPromise: TPromise): void { + this.modelsToDispose.add(key); + modelPromise.then(model => { - if (model instanceof TextFileEditorModel) { - this.textFileService.models.disposeModel(model); - } else { - model.dispose(); + if (this.modelsToDispose.has(key)) { + if (model instanceof TextFileEditorModel) { + this.textFileService.models.disposeModel(model); + } else { + model.dispose(); + } } }, err => { // ignore