|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | | -import { binarySearch } from 'vs/base/common/arrays'; |
8 | 7 | import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation'; |
9 | 8 | import { SyncDescriptor } from './descriptors'; |
10 | 9 |
|
11 | 10 | type Entry = [ServiceIdentifier<any>, any]; |
12 | 11 |
|
13 | 12 | export class ServiceCollection { |
14 | 13 |
|
15 | | - private _entries: Entry[] = []; |
| 14 | + private _entries = new Map<ServiceIdentifier<any>, any>(); |
16 | 15 |
|
17 | 16 | constructor(...entries: [ServiceIdentifier<any>, any][]) { |
18 | | - for (let entry of entries) { |
19 | | - this.set(entry[0], entry[1]); |
| 17 | + for (let [id, service] of entries) { |
| 18 | + this.set(id, service); |
20 | 19 | } |
21 | 20 | } |
22 | 21 |
|
23 | 22 | set<T>(id: ServiceIdentifier<T>, instanceOrDescriptor: T | SyncDescriptor<T>): T | SyncDescriptor<T> { |
24 | | - const entry: Entry = [id, instanceOrDescriptor]; |
25 | | - const idx = binarySearch(this._entries, entry, ServiceCollection._entryCompare); |
26 | | - if (idx < 0) { |
27 | | - // new element |
28 | | - this._entries.splice(~idx, 0, entry); |
29 | | - } else { |
30 | | - const old = this._entries[idx]; |
31 | | - this._entries[idx] = entry; |
32 | | - return old[1]; |
33 | | - } |
| 23 | + const result = this._entries.get(id); |
| 24 | + this._entries.set(id, instanceOrDescriptor); |
| 25 | + return result; |
34 | 26 | } |
35 | 27 |
|
36 | 28 | forEach(callback: (id: ServiceIdentifier<any>, instanceOrDescriptor: any) => any): void { |
37 | | - for (let entry of this._entries) { |
38 | | - let [id, instanceOrDescriptor] = entry; |
39 | | - callback(id, instanceOrDescriptor); |
40 | | - } |
| 29 | + this._entries.forEach((value, key) => callback(key, value)); |
41 | 30 | } |
42 | 31 |
|
43 | 32 | has(id: ServiceIdentifier<any>): boolean { |
44 | | - return binarySearch(this._entries, ServiceCollection._searchEntry(id), ServiceCollection._entryCompare) >= 0; |
| 33 | + return this._entries.has(id); |
45 | 34 | } |
46 | 35 |
|
47 | 36 | get<T>(id: ServiceIdentifier<T>): T | SyncDescriptor<T> { |
48 | | - const idx = binarySearch(this._entries, ServiceCollection._searchEntry(id), ServiceCollection._entryCompare); |
49 | | - if (idx >= 0) { |
50 | | - return this._entries[idx][1]; |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - private static _dummy: Entry = [undefined, undefined]; |
55 | | - |
56 | | - private static _searchEntry(id: ServiceIdentifier<any>): Entry { |
57 | | - ServiceCollection._dummy[0] = id; |
58 | | - return ServiceCollection._dummy; |
59 | | - } |
60 | | - |
61 | | - private static _entryCompare(a: Entry, b: Entry): number { |
62 | | - const _a = a[0].toString(); |
63 | | - const _b = b[0].toString(); |
64 | | - if (_a < _b) { |
65 | | - return -1; |
66 | | - } else if (_a > _b) { |
67 | | - return 1; |
68 | | - } else { |
69 | | - return 0; |
70 | | - } |
| 37 | + return this._entries.get(id); |
71 | 38 | } |
72 | 39 | } |
0 commit comments