mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-03-16 19:11:39 -04:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
99
src/vs/platform/registry/common/platform.ts
Normal file
99
src/vs/platform/registry/common/platform.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 Types = require('vs/base/common/types');
|
||||
import Assert = require('vs/base/common/assert');
|
||||
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
||||
export interface IRegistry {
|
||||
|
||||
/**
|
||||
* Adds the extension functions and properties defined by data to the
|
||||
* platform. The provided id must be unique.
|
||||
* @param id a unique identifier
|
||||
* @param data a contribution
|
||||
*/
|
||||
add(id: string, data: any): void;
|
||||
|
||||
/**
|
||||
* Returns true iff there is an extension with the provided id.
|
||||
* @param id an extension identifier
|
||||
*/
|
||||
knows(id: string): boolean;
|
||||
|
||||
/**
|
||||
* Returns the extension functions and properties defined by the specified key or null.
|
||||
* @param id an extension identifier
|
||||
*/
|
||||
as(id: string): any;
|
||||
as<T>(id: string): T;
|
||||
}
|
||||
|
||||
class RegistryImpl implements IRegistry {
|
||||
|
||||
private data: { [id: string]: any; };
|
||||
|
||||
constructor() {
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
public add(id: string, data: any): void {
|
||||
Assert.ok(Types.isString(id));
|
||||
Assert.ok(Types.isObject(data));
|
||||
Assert.ok(!this.data.hasOwnProperty(id), 'There is already an extension with this id');
|
||||
|
||||
this.data[id] = data;
|
||||
}
|
||||
|
||||
public knows(id: string): boolean {
|
||||
return this.data.hasOwnProperty(id);
|
||||
}
|
||||
|
||||
public as(id: string): any {
|
||||
return this.data[id] || null;
|
||||
}
|
||||
}
|
||||
|
||||
export const Registry = <IRegistry>new RegistryImpl();
|
||||
|
||||
/**
|
||||
* A base class for registries that leverage the instantiation service to create instances.
|
||||
*/
|
||||
export class BaseRegistry<T> {
|
||||
private toBeInstantiated: IConstructorSignature0<T>[] = [];
|
||||
private instances: T[] = [];
|
||||
private instantiationService: IInstantiationService;
|
||||
|
||||
public setInstantiationService(service: IInstantiationService): void {
|
||||
this.instantiationService = service;
|
||||
|
||||
while (this.toBeInstantiated.length > 0) {
|
||||
let entry = this.toBeInstantiated.shift();
|
||||
this.instantiate(entry);
|
||||
}
|
||||
}
|
||||
|
||||
private instantiate(ctor: IConstructorSignature0<T>): void {
|
||||
let instance = this.instantiationService.createInstance(ctor);
|
||||
this.instances.push(instance);
|
||||
}
|
||||
|
||||
_register(ctor: IConstructorSignature0<T>): void {
|
||||
if (this.instantiationService) {
|
||||
this.instantiate(ctor);
|
||||
} else {
|
||||
this.toBeInstantiated.push(ctor);
|
||||
}
|
||||
}
|
||||
|
||||
_getInstances(): T[] {
|
||||
return this.instances.slice(0);
|
||||
}
|
||||
|
||||
_setInstances(instances: T[]): void {
|
||||
this.instances = instances;
|
||||
}
|
||||
}
|
||||
52
src/vs/platform/registry/test/common/platform.test.ts
Normal file
52
src/vs/platform/registry/test/common/platform.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 assert = require('assert');
|
||||
import Platform = require('vs/platform/registry/common/platform');
|
||||
import Types = require('vs/base/common/types');
|
||||
|
||||
suite('Platform / Registry', () => {
|
||||
|
||||
test('registry - api', function () {
|
||||
assert.ok(Types.isFunction(Platform.Registry.add));
|
||||
assert.ok(Types.isFunction(Platform.Registry.as));
|
||||
assert.ok(Types.isFunction(Platform.Registry.knows));
|
||||
});
|
||||
|
||||
test('registry - mixin', function () {
|
||||
|
||||
Platform.Registry.add('foo', { bar: true });
|
||||
|
||||
assert.ok(Platform.Registry.knows('foo'));
|
||||
assert.ok(Platform.Registry.as('foo').bar);
|
||||
assert.equal(Platform.Registry.as('foo').bar, true);
|
||||
});
|
||||
|
||||
test('registry - knows, as', function () {
|
||||
|
||||
let ext = {};
|
||||
|
||||
Platform.Registry.add('knows,as', ext);
|
||||
|
||||
assert.ok(Platform.Registry.knows('knows,as'));
|
||||
assert.ok(!Platform.Registry.knows('knows,as1234'));
|
||||
|
||||
assert.ok(Platform.Registry.as('knows,as') === ext);
|
||||
assert.ok(Platform.Registry.as('knows,as1234') === null);
|
||||
});
|
||||
|
||||
test('registry - mixin, fails on duplicate ids', function () {
|
||||
|
||||
Platform.Registry.add('foo-dup', { bar: true });
|
||||
|
||||
try {
|
||||
Platform.Registry.add('foo-dup', { bar: false });
|
||||
assert.ok(false);
|
||||
} catch (e) {
|
||||
assert.ok(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user