Debian Codegraph @

node/@xterm/xterm/src/common/services/InstantiationService.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 *
5 * This was heavily inspired from microsoft/vscode's dependency injection system (MIT).
6 */
7 /*---------------------------------------------------------------------------------------------
8 * Copyright (c) Microsoft Corporation. All rights reserved.
9 * Licensed under the MIT License. See License.txt in the project root for license information.
10 *--------------------------------------------------------------------------------------------*/
11
12 import { IInstantiationService, IServiceIdentifier } from 'common/services/Services';
13 import { getServiceDependencies } from 'common/services/ServiceRegistry';
14
15 export class ServiceCollection {
16
17 private _entries = new Map<IServiceIdentifier<any>, any>();
18
19 constructor(...entries: [IServiceIdentifier<any>, any][]) {
20 for (const [id, service] of entries) {
21 this.set(id, service);
22 }
23 }
24
25 public set<T>(id: IServiceIdentifier<T>, instance: T): T {
26 const result = this._entries.get(id);
27 this._entries.set(id, instance);
28 return result;
29 }
30
31 public forEach(callback: (id: IServiceIdentifier<any>, instance: any) => any): void {
32 for (const [key, value] of this._entries.entries()) {
33 callback(key, value);
34 }
35 }
36
37 public has(id: IServiceIdentifier<any>): boolean {
38 return this._entries.has(id);
39 }
40
41 public get<T>(id: IServiceIdentifier<T>): T | undefined {
42 return this._entries.get(id);
43 }
44 }
45
46 export class InstantiationService implements IInstantiationService {
47 public serviceBrand: undefined;
48
49 private readonly _services: ServiceCollection = new ServiceCollection();
50
51 constructor() {
52 this._services.set(IInstantiationService, this);
53 }
54
55 public setService<T>(id: IServiceIdentifier<T>, instance: T): void {
56 this._services.set(id, instance);
57 }
58
59 public getService<T>(id: IServiceIdentifier<T>): T | undefined {
60 return this._services.get(id);
61 }
62
63 public createInstance<T>(ctor: any, ...args: any[]): T {
64 const serviceDependencies = getServiceDependencies(ctor).sort((a, b) => a.index - b.index);
65
66 const serviceArgs: any[] = [];
67 for (const dependency of serviceDependencies) {
68 const service = this._services.get(dependency.id);
69 if (!service) {
70 throw new Error(`[createInstance] ${ctor.name} depends on UNKNOWN service ${dependency.id._id}.`);
71 }
72 serviceArgs.push(service);
73 }
74
75 const firstServiceArgPos = serviceDependencies.length > 0 ? serviceDependencies[0].index : args.length;
76
77 // check for argument mismatches, adjust static args if needed
78 if (args.length !== firstServiceArgPos) {
79 throw new Error(`[createInstance] First service dependency of ${ctor.name} at position ${firstServiceArgPos + 1} conflicts with ${args.length} static arguments`);
80 }
81
82 // now create the instance
83 return new ctor(...[...args, ...serviceArgs]);
84 }
85 }