Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

InstantiationService.ts 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. import { IInstantiationService, IServiceIdentifier } from 'common/services/Services';
  12. import { getServiceDependencies } from 'common/services/ServiceRegistry';
  13. export class ServiceCollection {
  14. private _entries = new Map<IServiceIdentifier<any>, any>();
  15. constructor(...entries: [IServiceIdentifier<any>, any][]) {
  16. for (const [id, service] of entries) {
  17. this.set(id, service);
  18. }
  19. }
  20. set<T>(id: IServiceIdentifier<T>, instance: T): T {
  21. const result = this._entries.get(id);
  22. this._entries.set(id, instance);
  23. return result;
  24. }
  25. forEach(callback: (id: IServiceIdentifier<any>, instance: any) => any): void {
  26. this._entries.forEach((value, key) => callback(key, value));
  27. }
  28. has(id: IServiceIdentifier<any>): boolean {
  29. return this._entries.has(id);
  30. }
  31. get<T>(id: IServiceIdentifier<T>): T {
  32. return this._entries.get(id);
  33. }
  34. }
  35. export class InstantiationService implements IInstantiationService {
  36. private readonly _services: ServiceCollection = new ServiceCollection();
  37. constructor() {
  38. this._services.set(IInstantiationService, this);
  39. }
  40. public setService<T>(id: IServiceIdentifier<T>, instance: T): void {
  41. this._services.set(id, instance);
  42. }
  43. public createInstance<T>(ctor: any, ...args: any[]): any {
  44. const serviceDependencies = getServiceDependencies(ctor).sort((a, b) => a.index - b.index);
  45. const serviceArgs: any[] = [];
  46. for (const dependency of serviceDependencies) {
  47. const service = this._services.get(dependency.id);
  48. if (!service) {
  49. throw new Error(`[createInstance] ${ctor.name} depends on UNKNOWN service ${dependency.id}.`);
  50. }
  51. serviceArgs.push(service);
  52. }
  53. const firstServiceArgPos = serviceDependencies.length > 0 ? serviceDependencies[0].index : args.length;
  54. // check for argument mismatches, adjust static args if needed
  55. if (args.length !== firstServiceArgPos) {
  56. throw new Error(`[createInstance] First service dependency of ${ctor.name} at position ${firstServiceArgPos + 1} conflicts with ${args.length} static arguments`);
  57. }
  58. // now create the instance
  59. return <T>new ctor(...[...args, ...serviceArgs]);
  60. }
  61. }