提交 a4ead5c9 编写于 作者: J Johannes Rieken

make service collection map-ish

上级 d1ea6b2a
......@@ -105,7 +105,7 @@ export interface IFunctionSignature8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
}
export interface IServiceCollection {
add<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | descriptors.SyncDescriptor<T>): void;
put<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | descriptors.SyncDescriptor<T>): void;
forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void;
has(id: ServiceIdentifier<any>): boolean;
}
......@@ -237,11 +237,11 @@ export class ServiceCollection implements IServiceCollection {
constructor(...entries:[ServiceIdentifier<any>, any][]) {
for (let entry of entries) {
this.add(entry[0], entry[1]);
this.put(entry[0], entry[1]);
}
}
add<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | descriptors.SyncDescriptor<T>): void {
put<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) {
......@@ -261,6 +261,13 @@ export class ServiceCollection implements IServiceCollection {
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]);
......
......@@ -126,16 +126,16 @@ suite('Instantiation Service', () => {
test('service collection, cannot overwrite', function () {
let collection = new instantiation.ServiceCollection();
collection.add(IService1, null);
assert.throws(() => collection.add(IService1, null));
collection.put(IService1, null);
assert.throws(() => collection.put(IService1, null));
});
test('service collection, add/has', function () {
let collection = new instantiation.ServiceCollection();
collection.add(IService1, null);
collection.put(IService1, null);
assert.ok(collection.has(IService1));
collection.add(IService2, null);
collection.put(IService2, null);
assert.ok(collection.has(IService1));
assert.ok(collection.has(IService2));
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册