Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/services.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { SyncDescriptor } from '../vs/platform/instantiation/common/descriptors';
7
import * as insta from '../vs/platform/instantiation/common/instantiation';
8
import { ServiceIdentifier, createDecorator } from '../vs/platform/instantiation/common/instantiation';
9
import { InstantiationService } from '../vs/platform/instantiation/common/instantiationService';
10
import { ServiceCollection } from '../vs/platform/instantiation/common/serviceCollection';
11
export { ServiceIdentifier, createDecorator as createServiceIdentifier };
12
13
export interface IInstantiationServiceBuilder {
14
15
define<T>(id: insta.ServiceIdentifier<T>, instance: (T & insta.BrandedService) | SyncDescriptor<T>): void;
16
17
seal(): insta.IInstantiationService;
18
}
19
20
export class InstantiationServiceBuilder implements IInstantiationServiceBuilder {
21
22
private _isSealed: boolean = false;
23
private readonly _collection: ServiceCollection;
24
25
constructor(entries?: ServiceCollection | ([insta.ServiceIdentifier<unknown>, unknown][])) {
26
this._collection = Array.isArray(entries) ? new ServiceCollection(...entries) : entries ?? new ServiceCollection();
27
}
28
29
define<T>(id: insta.ServiceIdentifier<T>, instance: (T & insta.BrandedService) | SyncDescriptor<T>): void {
30
if (this._isSealed) {
31
throw new Error('This accessor is sealed and cannot be modified anymore.');
32
}
33
this._collection.set(id, instance);
34
}
35
36
seal(): insta.IInstantiationService {
37
if (this._isSealed) {
38
throw new Error('This accessor is sealed and cannot be seal again anymore.');
39
}
40
this._isSealed = true;
41
return new InstantiationService(this._collection, true);
42
}
43
}
44
45