mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-03-28 15:50:29 -04:00
Merge from master
This commit is contained in:
@@ -2,137 +2,140 @@
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { illegalState } from 'vs/base/common/errors';
|
||||
import { create } from 'vs/base/common/types';
|
||||
import * as assert from 'vs/base/common/assert';
|
||||
import { Graph } from 'vs/base/common/graph';
|
||||
import { Graph } from 'vs/platform/instantiation/common/graph';
|
||||
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
|
||||
import { ServiceIdentifier, IInstantiationService, ServicesAccessor, _util, optional } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
|
||||
|
||||
// TRACING
|
||||
const _enableTracing = false;
|
||||
|
||||
export class InstantiationService implements IInstantiationService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private _services: ServiceCollection;
|
||||
private _strict: boolean;
|
||||
protected readonly _services: ServiceCollection;
|
||||
protected readonly _strict: boolean;
|
||||
protected readonly _parent?: InstantiationService;
|
||||
|
||||
constructor(services: ServiceCollection = new ServiceCollection(), strict: boolean = false) {
|
||||
constructor(services: ServiceCollection = new ServiceCollection(), strict: boolean = false, parent?: InstantiationService) {
|
||||
this._services = services;
|
||||
this._strict = strict;
|
||||
this._parent = parent;
|
||||
|
||||
this._services.set(IInstantiationService, this);
|
||||
}
|
||||
|
||||
createChild(services: ServiceCollection): IInstantiationService {
|
||||
this._services.forEach((id, thing) => {
|
||||
if (services.has(id)) {
|
||||
return;
|
||||
}
|
||||
// If we copy descriptors we might end up with
|
||||
// multiple instances of the same service
|
||||
if (thing instanceof SyncDescriptor) {
|
||||
thing = this._createAndCacheServiceInstance(id, thing);
|
||||
}
|
||||
services.set(id, thing);
|
||||
});
|
||||
return new InstantiationService(services, this._strict);
|
||||
return new InstantiationService(services, this._strict, this);
|
||||
}
|
||||
|
||||
invokeFunction<R>(signature: (accessor: ServicesAccessor, ...more: any[]) => R, ...args: any[]): R {
|
||||
let accessor: ServicesAccessor;
|
||||
invokeFunction<R, TS extends any[]=[]>(fn: (accessor: ServicesAccessor, ...args: TS) => R, ...args: TS): R {
|
||||
let _trace = Trace.traceInvocation(fn);
|
||||
let _done = false;
|
||||
try {
|
||||
accessor = {
|
||||
let accessor = {
|
||||
get: <T>(id: ServiceIdentifier<T>, isOptional?: typeof optional) => {
|
||||
const result = this._getOrCreateServiceInstance(id);
|
||||
|
||||
if (_done) {
|
||||
throw illegalState('service accessor is only valid during the invocation of its target method');
|
||||
}
|
||||
|
||||
const result = this._getOrCreateServiceInstance(id, _trace);
|
||||
if (!result && isOptional !== optional) {
|
||||
throw new Error(`[invokeFunction] unknown service '${id}'`);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
return signature.apply(undefined, [accessor].concat(args));
|
||||
return fn.apply(undefined, [accessor].concat(args));
|
||||
} finally {
|
||||
accessor.get = function () {
|
||||
throw illegalState('service accessor is only valid during the invocation of its target method');
|
||||
};
|
||||
_done = true;
|
||||
_trace.stop();
|
||||
}
|
||||
}
|
||||
|
||||
createInstance(param: any, ...rest: any[]): any {
|
||||
|
||||
if (param instanceof SyncDescriptor) {
|
||||
// sync
|
||||
return this._createInstance(param, rest);
|
||||
|
||||
createInstance(ctorOrDescriptor: any | SyncDescriptor<any>, ...rest: any[]): any {
|
||||
let _trace: Trace;
|
||||
let result: any;
|
||||
if (ctorOrDescriptor instanceof SyncDescriptor) {
|
||||
_trace = Trace.traceCreation(ctorOrDescriptor.ctor);
|
||||
result = this._createInstance(ctorOrDescriptor.ctor, ctorOrDescriptor.staticArguments.concat(rest), _trace);
|
||||
} else {
|
||||
// sync, just ctor
|
||||
return this._createInstance(new SyncDescriptor(param), rest);
|
||||
_trace = Trace.traceCreation(ctorOrDescriptor);
|
||||
result = this._createInstance(ctorOrDescriptor, rest, _trace);
|
||||
}
|
||||
_trace.stop();
|
||||
return result;
|
||||
}
|
||||
|
||||
private _createInstance<T>(desc: SyncDescriptor<T>, args: any[]): T {
|
||||
|
||||
// arguments given by createInstance-call and/or the descriptor
|
||||
let staticArgs = desc.staticArguments.concat(args);
|
||||
private _createInstance<T>(ctor: any, args: any[] = [], _trace: Trace): T {
|
||||
|
||||
// arguments defined by service decorators
|
||||
let serviceDependencies = _util.getServiceDependencies(desc.ctor).sort((a, b) => a.index - b.index);
|
||||
let serviceDependencies = _util.getServiceDependencies(ctor).sort((a, b) => a.index - b.index);
|
||||
let serviceArgs: any[] = [];
|
||||
for (const dependency of serviceDependencies) {
|
||||
let service = this._getOrCreateServiceInstance(dependency.id);
|
||||
let service = this._getOrCreateServiceInstance(dependency.id, _trace);
|
||||
if (!service && this._strict && !dependency.optional) {
|
||||
throw new Error(`[createInstance] ${desc.ctor.name} depends on UNKNOWN service ${dependency.id}.`);
|
||||
throw new Error(`[createInstance] ${ctor.name} depends on UNKNOWN service ${dependency.id}.`);
|
||||
}
|
||||
serviceArgs.push(service);
|
||||
}
|
||||
|
||||
let firstServiceArgPos = serviceDependencies.length > 0 ? serviceDependencies[0].index : staticArgs.length;
|
||||
let firstServiceArgPos = serviceDependencies.length > 0 ? serviceDependencies[0].index : args.length;
|
||||
|
||||
// check for argument mismatches, adjust static args if needed
|
||||
if (staticArgs.length !== firstServiceArgPos) {
|
||||
console.warn(`[createInstance] First service dependency of ${desc.ctor.name} at position ${
|
||||
firstServiceArgPos + 1} conflicts with ${staticArgs.length} static arguments`);
|
||||
if (args.length !== firstServiceArgPos) {
|
||||
console.warn(`[createInstance] First service dependency of ${ctor.name} at position ${
|
||||
firstServiceArgPos + 1} conflicts with ${args.length} static arguments`);
|
||||
|
||||
let delta = firstServiceArgPos - staticArgs.length;
|
||||
let delta = firstServiceArgPos - args.length;
|
||||
if (delta > 0) {
|
||||
staticArgs = staticArgs.concat(new Array(delta));
|
||||
args = args.concat(new Array(delta));
|
||||
} else {
|
||||
staticArgs = staticArgs.slice(0, firstServiceArgPos);
|
||||
args = args.slice(0, firstServiceArgPos);
|
||||
}
|
||||
}
|
||||
|
||||
// // check for missing args
|
||||
// for (let i = 0; i < serviceArgs.length; i++) {
|
||||
// if (!serviceArgs[i]) {
|
||||
// console.warn(`${desc.ctor.name} MISSES service dependency ${serviceDependencies[i].id}`, new Error().stack);
|
||||
// }
|
||||
// }
|
||||
|
||||
// now create the instance
|
||||
const argArray = [desc.ctor];
|
||||
argArray.push(...staticArgs);
|
||||
argArray.push(...serviceArgs);
|
||||
|
||||
return <T>create.apply(null, argArray);
|
||||
return <T>create.apply(null, [ctor].concat(args, serviceArgs));
|
||||
}
|
||||
|
||||
private _getOrCreateServiceInstance<T>(id: ServiceIdentifier<T>): T {
|
||||
let thing = this._services.get(id);
|
||||
if (thing instanceof SyncDescriptor) {
|
||||
return this._createAndCacheServiceInstance(id, thing);
|
||||
private _setServiceInstance<T>(id: ServiceIdentifier<T>, instance: T): void {
|
||||
if (this._services.get(id) instanceof SyncDescriptor) {
|
||||
this._services.set(id, instance);
|
||||
} else if (this._parent) {
|
||||
this._parent._setServiceInstance(id, instance);
|
||||
} else {
|
||||
throw new Error('illegalState - setting UNKNOWN service instance');
|
||||
}
|
||||
}
|
||||
|
||||
private _getServiceInstanceOrDescriptor<T>(id: ServiceIdentifier<T>): T | SyncDescriptor<T> {
|
||||
let instanceOrDesc = this._services.get(id);
|
||||
if (!instanceOrDesc && this._parent) {
|
||||
return this._parent._getServiceInstanceOrDescriptor(id);
|
||||
} else {
|
||||
return instanceOrDesc;
|
||||
}
|
||||
}
|
||||
|
||||
private _getOrCreateServiceInstance<T>(id: ServiceIdentifier<T>, _trace: Trace): T {
|
||||
let thing = this._getServiceInstanceOrDescriptor(id);
|
||||
if (thing instanceof SyncDescriptor) {
|
||||
return this._createAndCacheServiceInstance(id, thing, _trace.branch(id, true));
|
||||
} else {
|
||||
_trace.branch(id, false);
|
||||
return thing;
|
||||
}
|
||||
}
|
||||
|
||||
private _createAndCacheServiceInstance<T>(id: ServiceIdentifier<T>, desc: SyncDescriptor<T>): T {
|
||||
assert.ok(this._services.get(id) instanceof SyncDescriptor);
|
||||
|
||||
const graph = new Graph<{ id: ServiceIdentifier<any>, desc: SyncDescriptor<any> }>(data => data.id.toString());
|
||||
private _createAndCacheServiceInstance<T>(id: ServiceIdentifier<T>, desc: SyncDescriptor<T>, _trace: Trace): T {
|
||||
type Triple = { id: ServiceIdentifier<any>, desc: SyncDescriptor<any>, _trace: Trace };
|
||||
const graph = new Graph<Triple>(data => data.id.toString());
|
||||
|
||||
function throwCycleError() {
|
||||
const err = new Error('[createInstance] cyclic dependency between services');
|
||||
@@ -141,9 +144,9 @@ export class InstantiationService implements IInstantiationService {
|
||||
}
|
||||
|
||||
let count = 0;
|
||||
const stack = [{ id, desc }];
|
||||
const stack = [{ id, desc, _trace }];
|
||||
while (stack.length) {
|
||||
const item = stack.pop();
|
||||
const item = stack.pop()!;
|
||||
graph.lookupOrInsertNode(item);
|
||||
|
||||
// TODO@joh use the graph to find a cycle
|
||||
@@ -152,17 +155,17 @@ export class InstantiationService implements IInstantiationService {
|
||||
throwCycleError();
|
||||
}
|
||||
|
||||
// check all dependencies for existence and if the need to be created first
|
||||
// check all dependencies for existence and if they need to be created first
|
||||
let dependencies = _util.getServiceDependencies(item.desc.ctor);
|
||||
for (let dependency of dependencies) {
|
||||
|
||||
let instanceOrDesc = this._services.get(dependency.id);
|
||||
if (!instanceOrDesc) {
|
||||
let instanceOrDesc = this._getServiceInstanceOrDescriptor(dependency.id);
|
||||
if (!instanceOrDesc && !dependency.optional) {
|
||||
console.warn(`[createInstance] ${id} depends on ${dependency.id} which is NOT registered.`);
|
||||
}
|
||||
|
||||
if (instanceOrDesc instanceof SyncDescriptor) {
|
||||
const d = { id: dependency.id, desc: instanceOrDesc };
|
||||
const d = { id: dependency.id, desc: instanceOrDesc, _trace: item._trace.branch(dependency.id, true) };
|
||||
graph.insertEdge(item, d);
|
||||
stack.push(d);
|
||||
}
|
||||
@@ -175,20 +178,110 @@ export class InstantiationService implements IInstantiationService {
|
||||
// if there is no more roots but still
|
||||
// nodes in the graph we have a cycle
|
||||
if (roots.length === 0) {
|
||||
if (graph.length !== 0) {
|
||||
if (!graph.isEmpty()) {
|
||||
throwCycleError();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
for (let root of roots) {
|
||||
for (let { data } of roots) {
|
||||
// create instance and overwrite the service collections
|
||||
const instance = this._createInstance(root.data.desc, []);
|
||||
this._services.set(root.data.id, instance);
|
||||
graph.removeNode(root.data);
|
||||
const instance = this._createServiceInstanceWithOwner(data.id, data.desc.ctor, data.desc.staticArguments, false, data._trace);
|
||||
this._setServiceInstance(data.id, instance);
|
||||
graph.removeNode(data);
|
||||
}
|
||||
}
|
||||
|
||||
return <T>this._services.get(id);
|
||||
return <T>this._getServiceInstanceOrDescriptor(id);
|
||||
}
|
||||
|
||||
private _createServiceInstanceWithOwner<T>(id: ServiceIdentifier<T>, ctor: any, args: any[] = [], supportsDelayedInstantiation: boolean, _trace: Trace): T {
|
||||
if (this._services.get(id) instanceof SyncDescriptor) {
|
||||
return this._createServiceInstance(ctor, args, supportsDelayedInstantiation, _trace);
|
||||
} else if (this._parent) {
|
||||
return this._parent._createServiceInstanceWithOwner(id, ctor, args, supportsDelayedInstantiation, _trace);
|
||||
} else {
|
||||
throw new Error('illegalState - creating UNKNOWN service instance');
|
||||
}
|
||||
}
|
||||
|
||||
protected _createServiceInstance<T>(ctor: any, args: any[] = [], supportsDelayedInstantiation: boolean, _trace: Trace): T {
|
||||
return this._createInstance(ctor, args, _trace);
|
||||
}
|
||||
}
|
||||
|
||||
//#region -- tracing ---
|
||||
|
||||
const enum TraceType {
|
||||
Creation, Invocation, Branch
|
||||
}
|
||||
|
||||
// {{SQL CARBON EDIT}} - Export trace
|
||||
export class Trace {
|
||||
|
||||
private static _None = new class extends Trace {
|
||||
constructor() { super(-1, null); }
|
||||
stop() { }
|
||||
branch() { return this; }
|
||||
};
|
||||
|
||||
static traceInvocation(ctor: any): Trace {
|
||||
return !_enableTracing ? Trace._None : new Trace(TraceType.Invocation, ctor.name || (ctor.toString() as string).substring(0, 42).replace(/\n/g, ''));
|
||||
}
|
||||
|
||||
static traceCreation(ctor: any): Trace {
|
||||
return !_enableTracing ? Trace._None : new Trace(TraceType.Creation, ctor.name);
|
||||
}
|
||||
|
||||
private static _totals: number = 0;
|
||||
private readonly _start: number = Date.now();
|
||||
private readonly _dep: [ServiceIdentifier<any>, boolean, Trace?][] = [];
|
||||
|
||||
private constructor(
|
||||
readonly type: TraceType,
|
||||
readonly name: string | null
|
||||
) { }
|
||||
|
||||
branch(id: ServiceIdentifier<any>, first: boolean): Trace {
|
||||
let child = new Trace(TraceType.Branch, id.toString());
|
||||
this._dep.push([id, first, child]);
|
||||
return child;
|
||||
}
|
||||
|
||||
stop() {
|
||||
let dur = Date.now() - this._start;
|
||||
Trace._totals += dur;
|
||||
|
||||
let causedCreation = false;
|
||||
|
||||
function printChild(n: number, trace: Trace) {
|
||||
let res: string[] = [];
|
||||
let prefix = new Array(n + 1).join('\t');
|
||||
for (const [id, first, child] of trace._dep) {
|
||||
if (first && child) {
|
||||
causedCreation = true;
|
||||
res.push(`${prefix}CREATES -> ${id}`);
|
||||
let nested = printChild(n + 1, child);
|
||||
if (nested) {
|
||||
res.push(nested);
|
||||
}
|
||||
} else {
|
||||
res.push(`${prefix}uses -> ${id}`);
|
||||
}
|
||||
}
|
||||
return res.join('\n');
|
||||
}
|
||||
|
||||
let lines = [
|
||||
`${this.type === TraceType.Creation ? 'CREATE' : 'CALL'} ${this.name}`,
|
||||
`${printChild(1, this)}`,
|
||||
`DONE, took ${dur.toFixed(2)}ms (grand total ${Trace._totals.toFixed(2)}ms)`
|
||||
];
|
||||
|
||||
if (dur > 2 || causedCreation) {
|
||||
console.log(lines.join('\n'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
Reference in New Issue
Block a user