mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 19:18:59 +01:00
simplify API
This commit is contained in:
@@ -265,6 +265,20 @@ export abstract class ExtHostFileSystemEventServiceShape {
|
||||
$onFileEvent(events: FileSystemEvents) { throw ni(); }
|
||||
}
|
||||
|
||||
export interface ObjectIdentifier {
|
||||
$ident: number;
|
||||
}
|
||||
|
||||
export namespace ObjectIdentifier {
|
||||
export function mixin<T>(obj: T, id: number): T & ObjectIdentifier {
|
||||
Object.defineProperty(obj, '$ident', { value: id, enumerable: true });
|
||||
return <T & ObjectIdentifier>obj;
|
||||
}
|
||||
export function get(obj: any): number {
|
||||
return obj['$ident'];
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class ExtHostHeapMonitorShape {
|
||||
$onGarbageCollection(ids: number[]): void { throw ni(); }
|
||||
}
|
||||
|
||||
@@ -13,50 +13,28 @@ export class ExtHostHeapMonitor extends ExtHostHeapMonitorShape {
|
||||
private _data: { [n: number]: any } = Object.create(null);
|
||||
private _callbacks: { [n: number]: Function } = Object.create(null);
|
||||
|
||||
private _mixinObjectIdentifier(obj: any): number {
|
||||
keep(obj:any, callback?:() => any): number {
|
||||
const id = ExtHostHeapMonitor._idPool++;
|
||||
|
||||
Object.defineProperties(obj, {
|
||||
'$heap_ident': {
|
||||
value: id,
|
||||
enumerable: true,
|
||||
configurable: false,
|
||||
writable: false
|
||||
},
|
||||
'$mid': {
|
||||
value: 3,
|
||||
enumerable: true,
|
||||
configurable: false,
|
||||
writable: false
|
||||
}
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
linkObjects(external: any, internal: any, callback?: () => any) {
|
||||
const id = this._mixinObjectIdentifier(external);
|
||||
this._data[id] = internal;
|
||||
this._data[id] = obj;
|
||||
if (typeof callback === 'function') {
|
||||
this._callbacks[id] = callback;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
getInternalObject<T>(external: any): T {
|
||||
const id = external.$heap_ident;
|
||||
if (typeof id === 'number') {
|
||||
return this._data[id];
|
||||
}
|
||||
delete(id: number): boolean {
|
||||
delete this._callbacks[id];
|
||||
return this._data[id];
|
||||
}
|
||||
|
||||
get<T>(id: number): T {
|
||||
return this._data[id];
|
||||
}
|
||||
|
||||
$onGarbageCollection(ids: number[]): void {
|
||||
for (const id of ids) {
|
||||
delete this._data[id];
|
||||
const callback = this._callbacks[id];
|
||||
if (callback) {
|
||||
delete this._callbacks[id];
|
||||
setTimeout(callback);
|
||||
}
|
||||
setTimeout(this._callbacks[id]);
|
||||
this.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import {ExtHostCommands} from 'vs/workbench/api/node/extHostCommands';
|
||||
import {ExtHostDiagnostics} from 'vs/workbench/api/node/extHostDiagnostics';
|
||||
import {IWorkspaceSymbolProvider, IWorkspaceSymbol} from 'vs/workbench/parts/search/common/search';
|
||||
import {asWinJsPromise, ShallowCancelThenPromise} from 'vs/base/common/async';
|
||||
import {MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape} from './extHost.protocol';
|
||||
import {MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier} from './extHost.protocol';
|
||||
import {regExpLeadsToEndlessLoop} from 'vs/base/common/strings';
|
||||
|
||||
// --- adapter
|
||||
@@ -497,9 +497,6 @@ class RenameAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
interface ISuggestion2 extends modes.ISuggestion {
|
||||
id: string;
|
||||
}
|
||||
|
||||
class SuggestAdapter {
|
||||
|
||||
@@ -546,9 +543,11 @@ class SuggestAdapter {
|
||||
|
||||
const item = list.items[i];
|
||||
const disposables: IDisposable[] = [];
|
||||
const suggestion = <ISuggestion2> TypeConverters.Suggest.from(item, disposables);
|
||||
const suggestion = TypeConverters.Suggest.from(item, disposables);
|
||||
|
||||
this._heapMonitor.linkObjects(suggestion, item, () => dispose(disposables));
|
||||
// assign identifier to suggestion
|
||||
const id = this._heapMonitor.keep(item, () => dispose(disposables));
|
||||
ObjectIdentifier.mixin(suggestion, id);
|
||||
|
||||
if (item.textEdit) {
|
||||
|
||||
@@ -572,9 +571,6 @@ class SuggestAdapter {
|
||||
suggestion.overwriteAfter = 0;
|
||||
}
|
||||
|
||||
// assign identifier to suggestion
|
||||
suggestion.id = String(i);
|
||||
|
||||
// store suggestion
|
||||
result.suggestions.push(suggestion);
|
||||
}
|
||||
@@ -589,12 +585,12 @@ class SuggestAdapter {
|
||||
return TPromise.as(suggestion);
|
||||
}
|
||||
|
||||
const item = this._heapMonitor.getInternalObject<CompletionItem>(suggestion);
|
||||
const item = this._heapMonitor.get<CompletionItem>(ObjectIdentifier.get(suggestion));
|
||||
if (!item) {
|
||||
return TPromise.as(suggestion);
|
||||
}
|
||||
return asWinJsPromise(token => this._provider.resolveCompletionItem(item, token)).then(resolvedItem => {
|
||||
return <ISuggestion2> TypeConverters.Suggest.from(resolvedItem || item, []);
|
||||
return TypeConverters.Suggest.from(resolvedItem || item, []);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import {IDisposable} from 'vs/base/common/lifecycle';
|
||||
import {IThreadService} from 'vs/workbench/services/thread/common/threadService';
|
||||
import {ExtHostContext} from './extHost.protocol';
|
||||
import {onDidGarbageCollectSignals, consumeSignals, trackGarbageCollection} from 'gc-signals';
|
||||
import {onDidGarbageCollectSignals, consumeSignals} from 'gc-signals';
|
||||
|
||||
export class MainThreadHeapMonitor {
|
||||
|
||||
@@ -29,10 +29,4 @@ export class MainThreadHeapMonitor {
|
||||
clearInterval(this._consumeHandle);
|
||||
this._subscription.dispose();
|
||||
}
|
||||
|
||||
trackObject(obj: any) {
|
||||
if (typeof obj.$heap_ident === 'number') {
|
||||
trackGarbageCollection(obj, obj.$heap_ident);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import {wireCancellationToken} from 'vs/base/common/async';
|
||||
import {CancellationToken} from 'vs/base/common/cancellation';
|
||||
import {Position as EditorPosition} from 'vs/editor/common/core/position';
|
||||
import {Range as EditorRange} from 'vs/editor/common/core/range';
|
||||
import {ExtHostContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape} from './extHost.protocol';
|
||||
import {ExtHostContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier} from './extHost.protocol';
|
||||
import {LanguageConfigurationRegistry, LanguageConfiguration} from 'vs/editor/common/modes/languageConfigurationRegistry';
|
||||
import {trackGarbageCollection} from 'gc-signals';
|
||||
|
||||
@@ -183,7 +183,7 @@ export class MainThreadLanguageFeatures extends MainThreadLanguageFeaturesShape
|
||||
provideCompletionItems: (model:IReadOnlyModel, position:EditorPosition, token:CancellationToken): Thenable<modes.ISuggestResult> => {
|
||||
return wireCancellationToken(token, this._proxy.$provideCompletionItems(handle, model.uri, position)).then(result => {
|
||||
for (const suggestion of result.suggestions) {
|
||||
trackGarbageCollection(suggestion, (<any>suggestion).$heap_ident);
|
||||
trackGarbageCollection(suggestion, ObjectIdentifier.get(suggestion));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user