提交 619ef444 编写于 作者: J Johannes Rieken

move service collection to its own file

上级 f50f5270
......@@ -12,7 +12,8 @@ import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import * as objects from 'vs/base/common/objects';
import * as timer from 'vs/base/common/timer';
import {TPromise} from 'vs/base/common/winjs.base';
import {IInstantiationService, ServiceCollection} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {IKeybindingContextKey, IKeybindingScopeLocation, IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {CommonEditorConfiguration} from 'vs/editor/common/config/commonEditorConfig';
......
......@@ -25,7 +25,8 @@ import * as tree from 'vs/base/parts/tree/browser/tree';
import {DefaultController, LegacyRenderer} from 'vs/base/parts/tree/browser/treeDefaults';
import {Tree} from 'vs/base/parts/tree/browser/treeImpl';
import {IEditorService} from 'vs/platform/editor/common/editor';
import {IInstantiationService, ServiceCollection} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {DefaultConfig} from 'vs/editor/common/config/defaultConfig';
......
......@@ -5,7 +5,7 @@
'use strict';
import {TPromise} from 'vs/base/common/winjs.base';
import {binarySearch} from 'vs/base/common/arrays';
import ServiceCollection from './serviceCollection';
import * as descriptors from './descriptors';
// ----------------------- internal util -----------------------
......@@ -104,12 +104,6 @@ export interface IFunctionSignature8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
(accessor: ServicesAccessor, first: A1, second: A2, third: A3, forth: A4, fifth: A5, sixth: A6, seventh: A7, eigth: A8): R;
}
export interface IServiceCollection {
set<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | descriptors.SyncDescriptor<T>): void;
forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void;
has(id: ServiceIdentifier<any>): boolean;
}
export var IInstantiationService = createDecorator<IInstantiationService>('instantiationService');
export interface IInstantiationService {
......@@ -181,7 +175,7 @@ export interface IInstantiationService {
* Creates a child of this service which inherts all current services
* and adds/overwrites the given services
*/
createChild(services: IServiceCollection): IInstantiationService;
createChild(services: ServiceCollection): IInstantiationService;
/**
* Adds a service or a descriptor to the collection of services and
......@@ -223,55 +217,3 @@ export function createDecorator<T>(serviceId: string): { (...args: any[]): void;
// ret['type'] = undefined;
return <any>ret;
}
type Entry = [ServiceIdentifier<any>, any];
export class ServiceCollection implements IServiceCollection {
private _entries: Entry[] = [];
constructor(...entries:[ServiceIdentifier<any>, any][]) {
for (let entry of entries) {
this.set(entry[0], entry[1]);
}
}
set<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | descriptors.SyncDescriptor<T>): void {
const entry: Entry = [id, instanceOrDescriptor];
const idx = ~binarySearch(this._entries, entry, ServiceCollection._entryCompare);
if (idx < 0) {
throw new Error(`service with that identifier already registered`);
}
this._entries.splice(idx, 0, entry);
}
forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void {
for (let entry of this._entries) {
let [id, instanceOrDescriptor] = entry;
callback(id, instanceOrDescriptor);
}
}
has(id: ServiceIdentifier<any>): boolean {
return binarySearch(this._entries, <Entry>[id,], ServiceCollection._entryCompare) >= 0;
}
get<T>(id: ServiceIdentifier<T>): T | descriptors.SyncDescriptor<T> {
const idx = binarySearch(this._entries, <Entry> [id,], ServiceCollection._entryCompare);
if (idx >= 0) {
return this._entries[idx][1];
}
}
private static _entryCompare(a: Entry, b: Entry): number {
const _a = _util.getServiceId(a[0]);
const _b = _util.getServiceId(b[0]);
if (_a < _b) {
return -1;
} else if (_a > _b) {
return 1;
} else {
return 0;
}
}
}
......@@ -9,8 +9,9 @@ import {illegalArgument, illegalState, canceled} from 'vs/base/common/errors';
import {create} from 'vs/base/common/types';
import {forEach} from 'vs/base/common/collections';
import {Graph} from 'vs/base/common/graph';
import * as descriptors from './descriptors';
import * as instantiation from './instantiation';
import * as descriptors from 'vs/platform/instantiation/common/descriptors';
import * as instantiation from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import IInstantiationService = instantiation.IInstantiationService;
import ServiceIdentifier = instantiation.ServiceIdentifier;
......@@ -18,13 +19,13 @@ import ServiceIdentifier = instantiation.ServiceIdentifier;
/**
* Creates a new instance of an instantiation service.
*/
export function createInstantiationService(services?: instantiation.IServiceCollection | { [legacyId: string]: any }): IInstantiationService {
export function createInstantiationService(services?: ServiceCollection | { [legacyId: string]: any }): IInstantiationService {
let map:{ [legacyId: string]: any };
if (typeof services === 'undefined') {
map = Object.create(null);
} else if (typeof (<instantiation.IServiceCollection>services).forEach === 'function') {
} else if (services instanceof ServiceCollection) {
map = Object.create(null);
(<instantiation.IServiceCollection>services).forEach((id, service) => {
services.forEach((id, service) => {
map[instantiation._util.getServiceId(id)] = service;
});
} else if(typeof services === 'object') {
......@@ -239,7 +240,7 @@ class InstantiationService implements IInstantiationService {
this._servicesMap = new ServicesMap(services, lock);
}
createChild(services: instantiation.ServiceCollection): IInstantiationService {
createChild(services: ServiceCollection): IInstantiationService {
const result = new InstantiationService(Object.create(null), this._servicesMap.lock);
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {binarySearch} from 'vs/base/common/arrays';
import {ServiceIdentifier, _util} from 'vs/platform/instantiation/common/instantiation';
import {SyncDescriptor} from './descriptors';
type Entry = [ServiceIdentifier<any>, any];
export default class ServiceCollection {
private _entries: Entry[] = [];
constructor(...entries:[ServiceIdentifier<any>, any][]) {
for (let entry of entries) {
this.set(entry[0], entry[1]);
}
}
set<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | SyncDescriptor<T>): void {
const entry: Entry = [id, instanceOrDescriptor];
const idx = ~binarySearch(this._entries, entry, ServiceCollection._entryCompare);
if (idx < 0) {
throw new Error(`service with that identifier already registered`);
}
this._entries.splice(idx, 0, entry);
}
forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void {
for (let entry of this._entries) {
let [id, instanceOrDescriptor] = entry;
callback(id, instanceOrDescriptor);
}
}
has(id: ServiceIdentifier<any>): boolean {
return binarySearch(this._entries, <Entry>[id,], ServiceCollection._entryCompare) >= 0;
}
get<T>(id: ServiceIdentifier<T>): T | SyncDescriptor<T> {
const idx = binarySearch(this._entries, <Entry> [id,], ServiceCollection._entryCompare);
if (idx >= 0) {
return this._entries[idx][1];
}
}
private static _entryCompare(a: Entry, b: Entry): number {
const _a = _util.getServiceId(a[0]);
const _b = _util.getServiceId(b[0]);
if (_a < _b) {
return -1;
} else if (_a > _b) {
return 1;
} else {
return 0;
}
}
}
......@@ -8,7 +8,7 @@
import assert = require('assert');
import instantiation = require('vs/platform/instantiation/common/instantiation');
import instantiationService = require('vs/platform/instantiation/common/instantiationService');
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
let IService1 = instantiation.createDecorator<IService1>('service1');
......@@ -125,13 +125,13 @@ class ServiceLoop2 implements IService2 {
suite('Instantiation Service', () => {
test('service collection, cannot overwrite', function () {
let collection = new instantiation.ServiceCollection();
let collection = new ServiceCollection();
collection.set(IService1, null);
assert.throws(() => collection.set(IService1, null));
});
test('service collection, add/has', function () {
let collection = new instantiation.ServiceCollection();
let collection = new ServiceCollection();
collection.set(IService1, null);
assert.ok(collection.has(IService1));
......
......@@ -30,7 +30,8 @@ import {IPartService} from 'vs/workbench/services/part/common/partService';
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import {IContextMenuService} from 'vs/platform/contextview/browser/contextView';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService, ServiceCollection} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IProgressService} from 'vs/platform/progress/common/progress';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
......
......@@ -35,7 +35,8 @@ import {IPartService} from 'vs/workbench/services/part/common/partService';
import {Position, POSITIONS} from 'vs/platform/editor/common/editor';
import {IStorageService} from 'vs/platform/storage/common/storage';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService, ServiceCollection} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {IMessageService, IMessageWithAction, Severity} from 'vs/platform/message/common/message';
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IProgressService} from 'vs/platform/progress/common/progress';
......
......@@ -31,7 +31,8 @@ import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/c
import {IStorageService} from 'vs/platform/storage/common/storage';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService, ServiceCollection} from 'vs/platform/instantiation/common/instantiation';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
import ServiceCollection from 'vs/platform/instantiation/common/serviceCollection';
import {IMessageService} from 'vs/platform/message/common/message';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {IModeService} from 'vs/editor/common/services/modeService';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册