instantiation.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // ------ internal util
  6. export var _util;
  7. (function (_util) {
  8. _util.serviceIds = new Map();
  9. _util.DI_TARGET = '$di$target';
  10. _util.DI_DEPENDENCIES = '$di$dependencies';
  11. function getServiceDependencies(ctor) {
  12. return ctor[_util.DI_DEPENDENCIES] || [];
  13. }
  14. _util.getServiceDependencies = getServiceDependencies;
  15. })(_util || (_util = {}));
  16. export const IInstantiationService = createDecorator('instantiationService');
  17. function storeServiceDependency(id, target, index, optional) {
  18. if (target[_util.DI_TARGET] === target) {
  19. target[_util.DI_DEPENDENCIES].push({ id, index, optional });
  20. }
  21. else {
  22. target[_util.DI_DEPENDENCIES] = [{ id, index, optional }];
  23. target[_util.DI_TARGET] = target;
  24. }
  25. }
  26. /**
  27. * The *only* valid way to create a {{ServiceIdentifier}}.
  28. */
  29. export function createDecorator(serviceId) {
  30. if (_util.serviceIds.has(serviceId)) {
  31. return _util.serviceIds.get(serviceId);
  32. }
  33. const id = function (target, key, index) {
  34. if (arguments.length !== 3) {
  35. throw new Error('@IServiceName-decorator can only be used to decorate a parameter');
  36. }
  37. storeServiceDependency(id, target, index, false);
  38. };
  39. id.toString = () => serviceId;
  40. _util.serviceIds.set(serviceId, id);
  41. return id;
  42. }