提交 6636ad9d 编写于 作者: J Joao Moreno

beware of sync/async promises

related to #60163
上级 26e39253
......@@ -80,7 +80,7 @@ export abstract class ReferenceCollection<T> {
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<T> {
}
protected abstract createReferencedObject(key: string): T;
protected abstract destroyReferencedObject(object: T): void;
protected abstract destroyReferencedObject(key: string, object: T): void;
}
export class ImmortalReference<T> implements IReference<T> {
......
......@@ -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', () => {
......
......@@ -21,6 +21,7 @@ import { IFileService } from 'vs/platform/files/common/files';
class ResourceModelCollection extends ReferenceCollection<TPromise<ITextEditorModel>> {
private providers: { [scheme: string]: ITextModelContentProvider[] } = Object.create(null);
private modelsToDispose = new Set<string>();
constructor(
@IInstantiationService private instantiationService: IInstantiationService,
......@@ -31,6 +32,8 @@ class ResourceModelCollection extends ReferenceCollection<TPromise<ITextEditorMo
}
createReferencedObject(key: string): TPromise<ITextEditorModel> {
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<TPromise<ITextEditorMo
return this.resolveTextModelContent(key).then(() => this.instantiationService.createInstance(ResourceEditorModel, resource));
}
destroyReferencedObject(modelPromise: TPromise<ITextEditorModel>): void {
destroyReferencedObject(key: string, modelPromise: TPromise<ITextEditorModel>): 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册